PDFKit (wkhtmltopdf) 断管、OS X 和 Rails 3 [英] PDFKit (wkhtmltopdf) Broken Pipe, OS X and Rails 3

查看:44
本文介绍了PDFKit (wkhtmltopdf) 断管、OS X 和 Rails 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PDFKit(使用 wkhtmltopdf) 以尝试在 Rails 3 应用程序中将视图呈现为 pdf.

PDFKit 渲染时 Errno::EPIPE(断管) 指向 send_data(kit.to_pdf, :filename => "generated.pdf", :type => 'application/pdf') 在我的控制器中显示动作:

# 控制器高清秀response_to do |格式|format.html { 渲染}格式.pdf做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')返回 # 以避免双重渲染调用结尾结尾结尾# 宝石文件...宝石'pdfkit'宝石'wkhtmltopdf'...

我知道 wkhtmltopdf 不是此错误的根源,因为 Rails.root 中的 wkhtmltopdf public/404.html tmp/404.pdf 按预期工作.>

我正在使用来自 jonathanspies.com 的 示例使用中间件同样失败.

# config/application.rb需要'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.75在"--margin-top"0.75in"--margin-bottom"0.75in"--encoding"UTF-8"--margin-left"0.75in"--quiet"——"——"

手动运行命令并显示使用屏幕,查看--quiet选项很容易看到它应该是--quit

更改/lib/pdfkit/pdfkit.rb:35到以下内容,一切都按预期工作(也包括中间件).

args <<' - 放弃'

所以,再一次,我在编写问题以获得帮助的过程中解决了我的问题(总是需要支付详细信息).我提交了一个 pull request 来纠正拼写错误(总是一个容易被忽视的愚蠢错误).希望没人介意我发帖.

解决方案

关于将 quiet 参数更改为退出.

此更改仅在您使用 wkhtmltopdf gem 时有效,该 gem 使用了非常旧版本的 wkhtmltopdf 二进制文件.

使用 wkhtmltopdf gem

10:32:15 wkhtml >wkhtmltopdf --versionwkhtmltopdf 0.8.3 使用 wkhtmltopdf 修补 qt版权所有 (C) 2008,2009 Jakob Truelsen,许可证 GPLv3+:GNU GPL 版本 3 或更高版本 这是免费软件:您可以自由更改和重新分发它.在法律允许的范围内,不提供任何保证.雅各布·特鲁尔森编剧的作品Mário Silva 和 Emmanuel Bouthenot 的补丁10:32:16 wkhtml >wkhtmltopdf --help |grep 退出-q, --quit 不那么冗长.10:32:16 wkhtml >wkhtmltopdf --help |grep 相当10:32:19 wkhtml >

使用我安装的最新二进制文件

10:33:40 tmp >wkhtmltopdf --version名称:wkhtmltopdf 0.9.9执照:版权所有 (C) 2008,2009 Wkhtmltopdf 作者.许可证 GPLv3+:GNU GPL 版本 3 或更高版本 .这是免费软件:您可以自由更改和重新分发它.没有保证,在法律允许的范围内.作者:由雅各布·特鲁尔森 (Jakob Truelsen) 撰写.Mário Silva、Benoit Garret 和 Emmanuel 的补丁布特诺.10:33:50 tmp >wkhtmltopdf --help |grep 退出10:34:02 tmp >wkhtmltopdf --help |安静-q, --quiet 不那么冗长10:34:07 tmp >

wkhtmltopdf gem 附带的旧二进制文件中存在拼写错误.我建议您根据是否包含 wkhtmltopdf gem 使用初始化或其他内容来修补 pdfkit.

我还会接受一个拉取请求,该请求使 pdfkit 知道它正在运行的 wkthtmltopdf 版本并有条件地切换该参数.

I'm using PDFKit (which uses wkhtmltopdf) in an attempt to render a view as pdf within a Rails 3 app.

PDFKit renders with Errno::EPIPE (Broken pipe) pointing to send_data(kit.to_pdf, :filename => "generated.pdf", :type => 'application/pdf') in my controller show action:

# 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'
...

I know wkhtmltopdf is not the source of this error as wkhtmltopdf public/404.html tmp/404.pdf from within Rails.root works as expected.

I'm using an example from jonathanspies.com after using the middleware failed in the same manner.

# config/application.rb
require 'pdfkit'
config.middleware.use PDFKit::Middleware

After trying it in a fresh Rails 3 app, I get the following error:

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" "-" "-"

Running the command manually and usage screen is displayed, looking at the --quiet option it is easy to see its supposed to be --quit

Change /lib/pdfkit/pdfkit.rb:35 to the following and everything works as expected (with middleware too).

args << '--quit'

So, once again, I've solved my problem in the act of writing the question to get help (always pays to include detail). I submitted a pull request which corrects the spelling mistake (always a silly mistake which gets unnoticed). Hope nobody minds me posting anyways.

解决方案

Regarding changing the quiet argument to quit.

This change is only valid if you are using the wkhtmltopdf gem which uses a very old version of the wkhtmltopdf binary.

With the 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 > 

With the latest binary I have installed

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 > 

The spelling mistake exists in the old binary that comes with the wkhtmltopdf gem. I would suggest you monkey patch pdfkit with an initialize or something based on if you included the wkhtmltopdf gem or not.

I would also accept a pull request that made pdfkit aware of the version of wkthtmltopdf it was running against and conditionally switched that argument.

这篇关于PDFKit (wkhtmltopdf) 断管、OS X 和 Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆