2010-08-12 73 views
5

我已經使用RVM(必須手動複製wkhtmltopdf二進制文件)在我的Rails 3應用程序中設置PDFKit。當我嘗試渲染頁面的PDF版本,我得到這個錯誤:Rails 3 - 使用PDFKit創建PDF文檔時wkhtmltopdf命令失敗

 
RuntimeError in AgenciesController#show 

command failed: ["lib/wkhtmltopdf/wkhtmltopdf", "--disable-smart-shrinking", "--page-size", "Letter", "--margin-top", "0.75in", "--margin-right", "0.75in", "--margin-bottom", "0.75in", "--margin-left", "0.75in", "--encoding", "UTF-8", "--quiet", "\n.......\n", "-"] 

以下是我的applicaition.rb:

 
    config.middleware.use "PDFKit::Middleware" 
    PDFKit.configure do |config| 
    config.wkhtmltopdf = 'lib/wkhtmltopdf/wkhtmltopdf' 

    end 

爲什麼發生這種情況的想法?我該如何解決它?

在控制檯中,我注意到了這個消息:

 
(sometimes it will work just to ignore this error with --ignore-load-errors) 

在哪裏調用該開關? wkhtmltopdf似乎在命令行上工作正常,我可以做類似「./wkhtmltopdf http://www.google.com google.pdf」並生成PDF。

感謝您的幫助,

彼得

+0

我試着config.wkhtmltopdf絕對路徑,重新啓動服務器,但得到了同樣的錯誤。 – futureshocked 2010-08-12 13:38:46

+0

你有沒有設法解決它?我在'my_app_path/lib/wkhtmltopdf'上存儲了可執行文件就像你說的那樣從終端工作。但在生產模式下運行Webrick服務器時不起作用。任何幫助,將不勝感激。謝謝 – TheMouseMan 2014-01-13 11:41:31

+0

不,我放棄了這種方法,因爲它不適合我正在嘗試做的事情,而是去了Prawn(https://github.com/prawnpdf/prawn)。 – futureshocked 2014-01-14 02:00:42

回答

5

從源代碼來看,你可以設置pdfkit選項。我想下面的工作:

PDFKit.configure do |config| 
    config.default_options[:ignore_load_errors] = true 
end 

(我沒有,雖然測試)

+1

這工作,以便應用程序不會崩潰時,我用.pdf加載頁面。但是,PDF頁面變得空白。可能是什麼原因造成的? – futureshocked 2010-08-13 10:57:39

+0

我猜''wkhtmltopdf'失敗。不知道該怎麼做,但。 – troelskn 2010-09-02 09:26:38

1

我使用這個技巧。

config.wkhtmltopdf = `which wkhtmltopdf`.gsub(/\n/, '') 

which which command returns a new line at end。

0

既然你已經安裝了wkhtmltopdf,它似乎工作,也許給wicked_pdf一槍。它在我的Ruby 1.9 Rails 3應用程序中非常適合我。它同樣簡單,但通過讓您在需要pdf時明確使用render :pdf => 'my_template',您可以更好地控制可作爲pdf格式呈現的內容。當然,如果你願意,你可以把它放在一個responds_to塊中。

ps Incase我不清楚,wicked_pdf也使用wkhtmltopdf。

4

我在谷歌搜索它,並在博客上找到答案。

解決方案是在這裏:

安裝依賴

$sudo aptitude install openssl build-essential xorg libssl-dev 

對於64位操作系統上運行一個個以下命令:

$ sudo wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2 
$ sudo tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2 
$ sudo mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf 
$ sudo chmod +x /usr/local/bin/wkhtmltopdf 

最後,回到Rails app/config/initializer文件夾中,並創建新的文件pdfkit.rb並粘貼以下代碼:

PDFKit.configure do |config| 
    config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf' if Rails.env.production? 
end 

就是這樣。現在您的pdf文件將被下載。另請訪問以獲取更多信息:http://www.stormconsultancy.co.uk/blog/development/generating-pdfs-in-rails-with-pdfkit-and-deploying-to-a-server/

謝謝。