【问题标题】:Pdfkit with Wkhtmltopdf hang on Mac带有 Wkhtmltopdf 的 Pdfkit 在 Mac 上挂起
【发布时间】:2018-03-31 10:54:58
【问题描述】:

我正在尝试在 mac 上将 PDFkit 与 wkhtmltopdf 一起使用,但不幸的是,它被卡住或挂起并继续加载。

当从终端 wkhtmltopdf http://google.com google.pdf 手动运行时,这会成功生成 pdf

我已经尝试了 StackOverflow 上所有可能的解决方案,但我不确定如何调试此问题。

我在 Ubuntu 上使用 wkhtmltopdf 和 pdfkit gem 成功生成报告,但不幸的是,我无法在 Mac 上使用 pdfkit 生成 pdf。

请给点建议

⇒  ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin17.0]

⇒  which ruby
/Users/mac/.rvm/rubies/ruby-2.1.1/bin/ruby

⇒  which wkhtmltopdf
/usr/local/bin/wkhtmltopdf

⇒  wkhtmltopdf --version
wkhtmltopdf 0.12.4 (with patched qt)

⇒  brew config

HOMEBREW_VERSION: 1.5.13
ORIGIN: https://github.com/Homebrew/brew
HEAD: 0f4e22e28d3b65a6c8dbb0f270bb6d262a2429d6
Last commit: 3 days ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: dc57a79d6c422c2052df8b33a1782a43cc1cfd53
Core tap last commit: 7 hours ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_AWS_ACCESS_KEY_ID: set
HOMEBREW_AWS_SECRET_ACCESS_KEY: set
CPU: quad-core 64-bit haswell
Homebrew Ruby: 2.3.3 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.3/bin/ruby
GCC-4.2: build 5666
Clang: 9.0 build 900
Git: 2.11.0 => /usr/local/bin/git
Curl: 7.54.0 => /usr/bin/curl
Perl: /usr/local/bin/perl => /usr/local/Cellar/perl/5.24.0_1/bin/perl
Python: /usr/local/bin/python => /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/bin/python2.7
Ruby: /Users/mac/.rvm/rubies/ruby-2.1.1/bin/ruby
Java: N/A
macOS: 10.13.1-x86_64
Xcode: 9.2
CLT: 9.2.0.0.1.1510905681
X11: N/A

初始化程序中的 PDFkit 配置

PDFKit.configure do |config|
  config.default_options = {
    :encoding      => "UTF-8",
    #:page_width    => '14mm',
    #:page_height    => '14mm',
    :page_size     => 'Letter',
    :zoom          => 0.75,
    :margin_top    => '0.5in',
    :margin_right  => '0.5in',
    :margin_bottom => '0.7in',
    :margin_left   => '0.5in',
    :load_error_handling => 'ignore'
  }
end

在 Application.rb 中

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

谢谢

【问题讨论】:

  • 您有任何错误信息吗?你是如何从控制台运行这个的?在脚本中?
  • @lacostenycoder 我希望有错误消息或日志。它只是卡住了。
  • 你能用 rvm 测试新版本的 ruby​​ 吗?另外,您是否有可能正在运行任何可能阻止执行的防火墙?
  • 没有防火墙在运行——也用 ruby​​-2.3.3 对其进行了测试,但没有运气

标签: ruby-on-rails ruby wkhtmltopdf pdfkit


【解决方案1】:

在 MacOS 上对我来说很好用。您能否发布以下输出:

ruby -v
which ruby
which wkhtmltopdf
wkhtmltopdf --version

希望你使用的是自制软件,如果是这样的话输出

brew config

让我们看看这里发生了什么。

确保没有卡住的进程

ps aux | grep wkhtmltopdf

试试这个

PDFKit.configure do |config|
  config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf'
  config.default_options = {
    :encoding      => "UTF-8",
    :page_size     => 'Letter',
    :zoom          => 0.75,
    :margin_top    => '0.5in',
    :margin_right  => '0.5in',
    :margin_bottom => '0.7in',
    :margin_left   => '0.5in',
    :load_error_handling => 'ignore'
  }
end

https://github.com/pdfkit/pdfkit#configuration

# in application.rb(Rails3+) or environment.rb(Rails2)
require 'pdfkit'
config.middleware.use PDFKit::Middleware

同样在开发中,您可能需要运行多线程服务器,即 unicorn

https://github.com/pdfkit/pdfkit#troubleshooting

【讨论】:

  • 没有卡住的进程,但这是正在尝试运行的命令/usr/local/bin/wkhtmltopdf --encoding UTF-8 --page-size Letter --zoom 0.75 --margin-top 0.5in --margin-right 0.5in --margin-bottom 0.7in --margin-left 0.5in --print-media-type --footer-html http://localhost:3000/pdf/footer --quiet - -
  • 你是怎么得到这个输出的?
  • 尝试注释掉:load_error_handling => 'ignore'
【解决方案2】:

我只是为像我这样的其他菜鸟发布它很糟糕——在生产中我使用的是 Unicorn,但在本地我试图用 Webrick 运行它,当 unicorn 处理它时它一次有 2 个请求时 webrick 卡住了。

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 2015-01-22
    • 2013-07-25
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多