简体   繁体   English

使用PDFKit创建PDF文档时,Rails 3 - wkhtmltopdf命令失败

[英]Rails 3 - wkhtmltopdf command failed when using PDFKit to create PDF documents

I have setup PDFKit in my Rails 3 application, using RVM (had to manually copy the wkhtmltopdf binary). 我在Rails 3应用程序中使用RVM设置了PDFKit(必须手动复制wkhtmltopdf二进制文件)。 When I try to render the PDF version of a page, I get this error: 当我尝试渲染页面的PDF版本时,我收到此错误:

RuntimeError in AgenciesController#show

command failed: ["lib/wkhtmltopdf/wkhtmltopdf", "--disable-smart-shrinking", "--page-size", "Letter", "--margin-top", "0.75in", "--margin-right", "0.75in", "--margin-bottom", "0.75in", "--margin-left", "0.75in", "--encoding", "UTF-8", "--quiet", "\n.......\n", "-"]

The following is in my applicaition.rb: 以下是我的应用程序.rb:

config.middleware.use "PDFKit::Middleware"
    PDFKit.configure do |config|
    config.wkhtmltopdf = 'lib/wkhtmltopdf/wkhtmltopdf' 

    end

An ideas why this is happening? 想法为什么会这样? how can I fix it? 我该怎么解决?

In the console, I noticed this message: 在控制台中,我注意到了这条消息:

(sometimes it will work just to ignore this error with --ignore-load-errors)

Where do I invoke that switch? 我在哪里调用那个开关? wkhtmltopdf seems to be working fine on the command line, I can do something like "./wkhtmltopdf http://www.google.com google.pdf" and generate a PDF. wkhtmltopdf似乎在命令行上工作正常,我可以做类似“./wkhtmltopdf http://www.google.com google.pdf”的内容并生成PDF。

Thanks for your help, 谢谢你的帮助,

Peter 彼得

Judging from the source code, you can set options on pdfkit. 从源代码来看,您可以在pdfkit上设置选项。 I think the following will work: 我认为以下内容可行:

PDFKit.configure do |config|
  config.default_options[:ignore_load_errors] = true
end

(I didn't test it though) (虽然我没试过)

I searched it on google, and found the answer on a blog. 我在谷歌搜索它,并在博客上找到答案。

Solution is here: 解决方案在这里:

Installing dependencies 安装依赖项

$sudo aptitude install openssl build-essential xorg libssl-dev

For 64bits OS Run one by one following commands: 对于64位操作系统按照以下命令逐个运行:

$ sudo wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
$ sudo tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
$ sudo mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf 
$ sudo chmod +x /usr/local/bin/wkhtmltopdf

Finally, go to your rails app/config/initializer folder and create new file pdfkit.rb and paste the following code into it: 最后,转到rails app/config/initializer文件夹并创建新文件pdfkit.rb并将以下代码粘贴到其中:

PDFKit.configure do |config|
   config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf' if Rails.env.production?
end

That's it. 而已。 Now your pdf file will be downloaded. 现在将下载您的pdf文件。 Also visit for further information: http://www.stormconsultancy.co.uk/blog/development/generating-pdfs-in-rails-with-pdfkit-and-deploying-to-a-server/ 另请访问以获取更多信息: http//www.stormconsultancy.co.uk/blog/development/generating-pdfs-in-rails-with-pdfkit-and-deploying-to-a-server/

Thanks. 谢谢。

你可能想查看这个插件wicked pdf

I was facing the similar issue on my ubuntu os. 我在我的ubuntu操作系统上遇到了类似的问题。 But then re-installed wkhtmltopdf using instruction in https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF . 但随后使用https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF中的说明重新安装了wkhtmltopdf。

Downloaded binary file from http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-static-i386.tar.bz2&can=2&q= and put it in the /usr/local/bin/ directory which solved an issue for me. http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-static-i386.tar.bz2&can=2&q=下载二进制文件并将其放在/ usr / local / bin /目录为我解决了一个问题。

I used this hack. 我用过这个黑客。

config.wkhtmltopdf = `which wkhtmltopdf`.gsub(/\n/, '')

the which command returns a new line at the end. which命令在结尾处返回一个新行。

Since you already have wkhtmltopdf installed and it appears to be working, maybe give wicked_pdf a shot. 既然你已经安装了wkhtmltopdf,它似乎正在工作,也许可以给wicked_pdf一个机会。 It is working great for me in my Ruby 1.9 Rails 3 app. 它在我的Ruby 1.9 Rails 3应用程序中对我很有用。 It is just as easy, but gives you a little more control over what can get rendered as a pdf by having you explicitly use render :pdf => 'my_template' when you want a pdf. 它同样简单,但是当你想要一个pdf时,通过明确地使用render :pdf => 'my_template' ,你可以更好地控制可以作为pdf render :pdf => 'my_template' Of course, you can place that inside a responds_to block if you wish. 当然,如果您愿意,可以将其放在responds_to块中。

ps Incase I wasn't clear, wicked_pdf also uses wkhtmltopdf. ps Incase我不清楚,wicked_pdf也使用wkhtmltopdf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM