我正在使用PDFKit(它使用wkhtmltopdf)试图在Rails 3应用程序中呈现视图为pdf。
PDFKit渲染与Errno::EPIPE (Broken pipe)
指向send_data(kit.to_pdf, :filename => "generated.pdf", :type => 'application/pdf')
在我的控制器显示动作:
# Controller
def show
respond_to do |format|
format.html { render }
format.pdf do
html = render_to_string(:layout => false , :action => "show.html.haml")
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "invoice.pdf", :type => 'application/pdf')
return # to avoid double render call
end
end
end
# Gemfile
...
gem 'pdfkit'
gem 'wkhtmltopdf'
...
我知道wkhtmltopdf不是这个错误的来源,因为wkhtmltopdf public/404.html tmp/404.pdf
从Rails.root
内按预期工作。
我使用jonathanspies.com的一个例子,在使用中间件以同样的方式失败后。
# config/application.rb
require 'pdfkit'
config.middleware.use PDFKit::Middleware
在新的Rails 3应用程序中尝试后,我得到以下错误:
command failed: "~/.rvm/gems/ree-1.8.7-2011.01@blog/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-right" "0.75in" "--margin-top" "0.75in" "--margin-bottom" "0.75in" "--encoding" "UTF-8" "--margin-left" "0.75in" "--quiet" "-" "-"
手动运行命令并显示使用屏幕,查看——quiet选项很容易看出它应该是——quit
/lib/pdfkit/pdfkit.变化从Rb:35到以下,一切都像预期的那样工作(中间件也是如此)。
args << '--quit'
所以,再一次,我在写问题以获得帮助的过程中解决了我的问题(包含细节总是值得的)。我提交了一个pull请求,它纠正了拼写错误(总是一个被忽视的愚蠢错误)。
关于将quiet参数更改为退出
此更改仅在使用wkhtmltopdf gem时有效,该gem使用的是旧版本的wkhtmltopdf二进制文件
使用wkhtmltopdf gem
10:32:15 wkhtml > wkhtmltopdf --version
wkhtmltopdf 0.8.3 using wkhtmltopdf patched qt
Copyright (C) 2008,2009 Jakob Truelsen,
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jakob Truelsen
Patches by Mário Silva and Emmanuel Bouthenot
10:32:16 wkhtml > wkhtmltopdf --help | grep quit
-q, --quit Be less verbose.
10:32:16 wkhtml > wkhtmltopdf --help | grep quite
10:32:19 wkhtml >
我已经安装了最新的二进制文件
10:33:40 tmp > wkhtmltopdf --version
Name:
wkhtmltopdf 0.9.9
License:
Copyright (C) 2008,2009 Wkhtmltopdf Authors.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO
WARRANTY, to the extent permitted by law.
Authors:
Written by Jakob Truelsen. Patches by Mário Silva, Benoit Garret and Emmanuel
Bouthenot.
10:33:50 tmp > wkhtmltopdf --help | grep quit
10:34:02 tmp > wkhtmltopdf --help | grep quiet
-q, --quiet Be less verbose
10:34:07 tmp >
在wkhtmltopdf gem附带的旧二进制文件中存在拼写错误。我建议您使用初始化或基于是否包含wkhtmltopdf gem的东西来修补pdfkit。
我也会接受拉请求,使pdfkit知道wkthtmltopdf的版本,并有条件地切换该参数。
更改/lib/pdfkit/pdfkit。从Rb:35到以下,一切都像预期的那样工作(中间件也是如此)。
args << '--quit'