PDFkit Gem不适用于Rails

时间:2012-06-27 12:16:13

标签: ruby-on-rails ruby-on-rails-3 pdfkit

我已经安装了PDFKit和wkhtmltopdf。我使用brew install for wkhtmltopdf,对于PDFkit,我将它添加到我的gem文件中并创建了以下内容:

# config/initializers/pdfkit.rb
PDFKit.configure do |config|
  config.wkhtmltopdf = '/usr/local/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

以及将以下内容添加到application.rb

require 'pdfkit'
config.middleware.use PDFKit::Middleware

当我运行which wkhtmltopdf时,我得到:/usr/local/bin/wkhtmltopdf

我也可以运行wkhtmltopdf google.com google.pdf并正确拉下文件。

我甚至可以做wkhtmltopdf http://localhost:3000/about google.pdf这样的事情,但效果很好。

但是,如果我尝试:http://localhost:3000/about.pdf并且只是在我的浏览器中访问整个网站只是尝试加载并且永远不会完成。 但是,我得到以下内容:

RuntimeError (command failed: "/usr/local/bin/wkhtmltopdf" "--quiet" "-" "-"):
  pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:71:in `to_pdf'
  pdfkit (0.5.2) lib/pdfkit/middleware.rb:21:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.1) lib/rack/etag.rb:23:in `call'
  rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/head.rb:14:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
  rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/cookies.rb:338:in `call'
  activerecord (3.2.3) lib/active_record/query_cache.rb:64:in `call'
  activerecord (3.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:467:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `_run__1600846312864942852__call__3683691454379641054__callbacks'
  activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.3) lib/rails/engine.rb:479:in `call'
  railties (3.2.3) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/local/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

并确认它是在中间件中。

rake middleware
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fc11b9c4a30>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use ActionDispatch::Head
use Rack::ConditionalGet
use Rack::ETag
use ActionDispatch::BestStandardsSupport
use PDFKit::Middleware
run Teacherjoy::Application.routes

我错过了什么让这个工作?

2 个答案:

答案 0 :(得分:0)

我今天早上尝试了PDFKit并且遇到了无限页面加载的问题。我通过添加“config.threadsafe!”来修复它。到我的application.rb

此外,我更改了由this stackoverflow answer

解决方案引导的代码

答案 1 :(得分:0)

如果您在系统中使用rvm,则pdfkit / usr / local / bin / wkhtmltopdf的路径不正确。 我的本地开发环境系统就是这种情况。

我检查了哪个wkhtmltopdf这个命令在我的应用程序目录中返回2个路径如果我使用了我的gemset然后我得到了这样的路径

  which wkhtmltopdf
/home/re10/.rvm/gems/ruby-1.8.7-p358@lmft/bin/wkhtmltopdf  (path to the gemset in server)

我在根目录中尝试了相同的命令,然后我得到了一个路径

which wkhtmltopdf
/usr/local/bin/wkhtmltopdf

所以尝试在进入项目目录后安装软件包,并在使用gemset之后,命令的路径为wkhtmltopdf应该在你的rvm内部,并且在那里为所有gemset存储所有宝石。

请参阅此stackoverflow以获取示例。这对我有用。 的 https://stackoverflow.com/a/5764290/1304966