我正在尝试在rails 3 app中使用PDFKit作为中间件。
我可以从命令行使用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。
答案 0 :(得分:6)
运行并复制路径
哪个wkhtmltopdf
创建config/initializers/pdfkit.rb
:
PDFKit.configure do |config|
config.wkhtmltopdf = '/path/to/wkhtmltopdf'
end
答案 1 :(得分:3)
有人解决了这个问题并kindly posted their solution。
答案 2 :(得分:1)
为什么错误以
/Users/bobby/...开头,但您的配置以
/Users/pierrelapree/...
开头
编辑:
您的代码与README中的示例之间的另一个区别: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
答案 3 :(得分:1)
作为宝石出现的wkhtmltopdf
已经过时了。卸载此gem并尝试以下wkhtmltopdf binary file。下载,解压缩并将其移至/usr/local/bin/
。它应该有所帮助。