我正在尝试将PDFKIT用作Rails 3应用程序中的中间件。

我可以从命令行中使用wkhtmltopdf,但是我的应用程序不断给我这个错误

command failed: "/Users/bobby/.rvm/gems/ruby-1.9.2-p0/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" "-" "-"

如果我在终端运行此操作,它会等待我的输入,因此我键入一些HTML,然后按Ctrl-D,然后吐出似乎是PDF的东西...但是Rails中没有运气。

这是我所拥有的:

application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'pdfkit'
Bundler.require(:default, Rails.env) if defined?(Bundler)

module Mpr
  class Application < Rails::Application

    YEARS_ARRAY =  (2006..2012).map {|y| [y,y]}.unshift(["Year",nil])
    MONTHS_ARRAY = (1..12).map{|m| [ Date::MONTHNAMES[m], m]}.unshift(["All months",nil])
    config.middleware.use "PDFKit::Middleware", :print_media_type => true
    PDFKit.configure do |config|
      config.wkhtmltopdf = '/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf'
    end
  end
end

在我的控制器(第一行)中

respond_to :html, :pdf

我认为我已经经历了So,Github和Google的所有线程,但没有运气。

谁能帮我或指向正确的方向?

谢谢,P。

有帮助吗?

解决方案

有人解决了问题, 请发布他们的解决方案.

其他提示

运行并复制路径

哪个wkhtmltopdf

创造 config/initializers/pdfkit.rb:

 PDFKit.configure do |config|
   config.wkhtmltopdf = '/path/to/wkhtmltopdf'
 end

wkhtmltopdf 这是一颗宝石很旧的。卸载此宝石并尝试以下 wkhtmltopdf二进制文件. 。下载,解压缩并将其移至 /usr/local/bin/. 。它应该有帮助。

为什么错误以

/Users/bobby/... 
但是您的配置以
/Users/pierrelapree/... 

编辑:

您的代码和读书中的示例之间的另一个区别: https://github.com/pdfkit/pdfkit 他们表现出来 config.middleware.use 进行类或模块参数,而不是字符串。

尝试更改此

config.middleware.use "PDFKit::Middleware", :print_media_type => true

对此

config.middleware.use PDFKit::Middleware, :print_media_type => true
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top