2011-01-13 152 views
10

我想在rails 3應用程序中使用PDFKit作爲中間件。Rails PDFKit命令失敗

我可以使用命令行就好wkhtmltopdf,但我的應用程序不斷拋出我這個錯誤

command failed: "/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" "--quiet" "-" "-" 

如果我在終端運行它,它等待我的輸入,所以我輸入一些HTML,然後按Ctrl-d,它吐出似乎是一些PDF ......但在軌道上沒有運氣。

這是我有:

application.rb

require File.expand_path('../boot', __FILE__) 

require 'rails/all' 
require 'pdfkit' 
Bundler.require(:default, Rails.env) if defined?(Bundler) 

module Mpr 
    class Application < Rails::Application 

    YEARS_ARRAY = (2006..2012).map {|y| [y,y]}.unshift(["Year",nil]) 
    MONTHS_ARRAY = (1..12).map{|m| [ Date::MONTHNAMES[m], m]}.unshift(["All months",nil]) 
    config.middleware.use "PDFKit::Middleware", :print_media_type => true 
    PDFKit.configure do |config| 
     config.wkhtmltopdf = '/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf' 
    end 
    end 
end 

在我的控制器(第一行)

respond_to :html, :pdf 

我想我已經通過對SO,Github上和谷歌的所有線程不見了,但沒有運氣。

任何人都可以幫助或指引我在正確的方向嗎?

您的代碼和自述的例子的另一個區別:https://github.com/pdfkit/PDFKit

感謝,P.

回答

1

爲什麼錯誤與

/Users/bobby/...
尚未開始你的配置與
/Users/pierrelapree/...

編輯開始它們顯示config.middleware.use採用類或模塊參數,而不是字符串。

嘗試修改此

config.middleware.use "PDFKit::Middleware", :print_media_type => true

這個

config.middleware.use PDFKit::Middleware, :print_media_type => true
+0

我的不好,我查過了,他們是一樣的。我已經糾正了這個問題 – Pierre 2011-02-15 06:17:52

+0

這個問題看起來還是不一樣的?無論如何,上面的另一個建議。 – jemminger 2011-02-15 16:16:01

1

自帶的寶石是很老的wkhtmltopdf。卸載此Gem並嘗試以下wkhtmltopdf binary file。下載並解壓縮到/usr/local/bin/。它應該有所幫助。

6

運行和複製路徑

這wkhtmltopdf

創建:

PDFKit.configure do |config| 
    config.wkhtmltopdf = '/path/to/wkhtmltopdf' 
end