首页 文章

PDFKit(wkhtmltopdf)断管,OS X和Rails 3

提问于
浏览
16

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

PDFKit渲染 Errno::EPIPE (Broken pipe) 指向我的控制器show action中的 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不是这个错误的来源,因为 Rails.root 内的 wkhtmltopdf public/404.html tmp/404.pdf 按预期工作 .

在使用中间件失败后,我使用example from 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 request来纠正拼写错误(总是一个被忽视的愚蠢错误) . 希望没有人介意我发帖 .

2 回答

  • 6

    Regarding changing the quiet argument to quit.

    此更改仅在您使用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 >
    

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

    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 .

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

  • 2

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

    args << '--quit'
    

相关问题