新手问题 请问谁用过 gem 'pdfkit'呢?wkhtmltopdf 出错了。

QueXuQ · 2012年12月11日 · 最后由 liker 回复于 2013年08月21日 · 4771 次阅读

错误如下:

RuntimeError in ProductsController#show

command failed: "/home/que/.rvm/gems/ruby-1.9.3-p125/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" "--quiet" "-" "-"

我的 gem:

gem 'pdfkit'
gem 'wkhtmltopdf-binary'

config/application.rb

require 'pdfkit'
config.middleware.use PDFKit::Middleware, :print_media_type => true

config/initializers/pdfkit.rb

PDFKit.configure do |config|
  config.wkhtmltopdf = '/home/que/.rvm/gems/ruby-1.9.3-p125/bin/wkhtmltopdf'
  # config.default_options = {
  #   :page_size => 'Legal',
  #   :print_media_type => true
  # }
  # config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
end

终端查看 wkhtmltopdf:

$ which wkhtmltopdf
/home/que/.rvm/gems/ruby-1.9.3-p125/bin/wkhtmltopdf

wkhtmltopdf 版本:

$ wkhtmltopdf --version
Name:
  wkhtmltopdf 0.9.6

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.

请问大家有没有出现过这样的问题呢?最后如何解决的呢?我网上找了好多的方法都是弄不好,也搞不懂是什么回事。

参考:http://railscasts.com/episodes/220-pdfkit

是在开发模式下吗?开发模式下单线程的 server 可能会不行哦,用 unicorn 试下。生产模式下一般都没问题的。 看下这个链接 https://github.com/pdfkit/PDFKit#troubleshooting 下的 Single thread issue 小节

#1 楼 @KoALa Thanks.一开始一眼扫过去以为是 unicorn 出问题才看那。没留意。后面找了好多相关文章都没有说到 server 问题。谢谢。

wkhtmltopdf 用起来方便,但是分页断行的问题比较头痛,所以我改用了 prawn

#3 楼 @pzgz 这样的吗。我正在找着页脚,页眉,页码怎么用 wkhtmltopdf 实现呢。请问实现的了的吗?

页眉页码我记得是可以做到的,prawn 都可以做

#3 楼 @pzgz 分页断行可以通过 css 来控制的,楼主给的那个链接里好像就有事例

#5 楼 @pzgz 分页断页好像这个 CSS 可以做:

page-break-before: always;

CSS 打印属性(Print)

  • orphans:设置当元素内部发生分页时必须在页面底部保留的最少行数。
  • page-break-after:设置元素后的分页行为。参考:page-break-after
  • page-break-before:设置元素前的分页行为。参考:page-break-before
  • page-break-inside:设置元素内部的分页行为。参考:page-break-inside

#4 楼 @QueXuQ 页脚页眉可以通过下面方法做:

--footer-center*    <text>  Centered footer text
--footer-font-name* <name>  Set footer font name (default Arial)
--footer-font-size* <size>  Set footer font size (default 11)
--footer-html*  <url>   Adds a html footer
--footer-left*  <text>  Left aligned footer text
--footer-line*      Display line above the footer
--footer-right* <text>  Right aligned footer text
--footer-spacing*   <real>  Spacing between footer and content in mm (default 0)
--header-center*    <text>  Centered header text
--header-font-name* <name>  Set header font name (default Arial)
--header-font-size* <size>  Set header font size (default 11)
--header-html*  <url>   Adds a html header
--header-left*  <text>  Left aligned header text
--header-line*      Display line below the header
--header-right* <text>  Right aligned header text
--header-spacing*   <real>  Spacing between header and content in mm (default 0)

简单来说,就是在下面的文件下加东西就可以了:

# config/initializers/pdfkit.rb
PDFKit.configure do |config|
  # config.wkhtmltopdf = '/path/to/wkhtmltopdf'
  config.default_options = {
    :page_size => 'Legal',
    :print_media_type => true,
    :header_left => "XXX"   #在页眉顶端左边加入XXX
  }
  # config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
end

详情参考:wkhtmltopdf-0.9.9-doc

分页这个,用 CSS 控制的,我用过的,不方便,我忘了当时具体的问题是什么了,也许是表格内无法分页,或者是无法整体控制,具体什么我记不清楚了,没有写笔记,可惜了

大哥们,我想问问知道 wkhtmltopdf 怎么加密生成的 pdf 咯?

请问大伙都没有碰到中文编码问题吗?

我现在也用到了 PDFKit 遇到了些许问题 which wkhtmltopdf

/usr/local/bin/wkhtmltopdf

config/initializers/pdfkit.rb

config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf'

剩余的和你一样 在路径加上".pdf"后不能显示任何东西。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号