我正在使用PDFKit(使用wkhtmltopdf)尝试在Rails 3应用程序中将视图呈现为pdf.
PDFKit通过Errno::EPIPE (Broken pipe)指向send_data(kit.to_pdf, :filename => "generated.pdf", :type => 'application/pdf')我的控制器show动作呈现:
# 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'
...
Run Code Online (Sandbox Code Playgroud)
我知道wkhtmltopdf不是这个错误的来源,因为wkhtmltopdf public/404.html tmp/404.pdf从Rails.root工作中如预期的那样.
在使用中间件失败后,我正在使用jonathanspies.com中的一个示例.
# config/application.rb
require 'pdfkit'
config.middleware.use PDFKit::Middleware
Run Code Online (Sandbox Code Playgroud)
在新的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" "-" "-"
Run Code Online (Sandbox Code Playgroud)
手动运行命令并显示使用屏幕,查看--quiet选项很容易看到它应该是--quit
将/lib/pdfkit/pdfkit.rb:35更改为以下内容,一切都按预期工作(也使用中间件).
args << '--quit'
Run Code Online (Sandbox Code Playgroud)
所以,再一次,我已经解决了我在编写问题的过程中遇到的问题以获得帮助(总是付费以包含细节).我提交了一个拉取请求来纠正拼写错误(总是一个被忽视的愚蠢错误).希望没有人介意我发帖.
小智 6
关于改变安静的论点以退出.
此更改仅在您使用wkhtmltopdf gem时才有效,该gem使用非常旧版本的wkhtmltopdf二进制文件.
随着wkhtmltopdf宝石
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 >
Run Code Online (Sandbox Code Playgroud)
使用我安装的最新二进制文件
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 >
Run Code Online (Sandbox Code Playgroud)
拼写错误存在于wkhtmltopdf gem附带的旧二进制文件中.如果你包含了wkhtmltopdf gem,我会建议你使用初始化或基于某种东西的猴子补丁pdfkit.
我也接受一个pull请求,使pdfkit知道它正在运行的wkthtmltopdf版本并有条件地切换该参数.