简体   繁体   English

使用PDFKit和wkhtmltopdf在rails 4 app中生成pdf

[英]Generating pdf in rails 4 app using PDFKit and wkhtmltopdf

I have an existing ruby on rails app where I want to generate pdf of a particular view page. 我在rails应用程序上有一个现有的ruby,我想生成特定视图页面的pdf。 I have installed both wkhtmltopdf and pdfkit in my system. 我在我的系统中安装了wkhtmltopdf和pdfkit。 I am using windows 8.1 64bit version and I am following Ryan Bate's tutorial for pdfkit in rails. 我正在使用Windows 8.1 64位版本,我正在关注Ryan Bate的rails for pdfkit教程。 The tutorial link is here:- 教程链接在这里: -

http://railscasts.com/episodes/220-pdfkit?autoplay=true http://railscasts.com/episodes/220-pdfkit?autoplay=true

My config\\initializers\\pdfkit.rb file is as:- 我的config \\ initializers \\ pdfkit.rb文件如下: -

PDFKit.configure do |config|
  config.wkhtmltopdf = 'C:\RailsInstaller\wkhtmltopdf\bin\wkhtmltopdf.exe'
  config.default_options = {
  :page_size => 'Legal',
  :print_media_type => true
  }
# Use only if your external hostname is unavailable on the server.
config.root_url = "http://localhost"
config.verbose = false
end

Now when I am going to open the view file on the browser by appending .pdf at the end of URL, I am getting the following error: 现在,当我要通过在URL末尾附加.pdf打开浏览器上的视图文件时,我收到以下错误:

command failed (exitstatus=1): C:\\RailsInstaller\\wkhtmltopdf\\bin\\wkhtmltopdf.exe --page-size Legal --print-media-type - - 命令失败(exitstatus = 1):C:\\ RailsInstaller \\ wkhtmltopdf \\ bin \\ wkhtmltopdf.exe --page-size法律--print-media-type - -

For example, The URL of one of my article is : 例如,我的一篇文章的URL是:

http://localhost:3000/articles/9 HTTP://本地主机:3000 /用品/ 9

Now when I am typing http://localhost:3000/articles/9.pdf , I am getting the above error. 现在当我输入http:// localhost:3000 / articles / 9.pdf时 ,我收到上述错误。

Please help me to figure it out. 请帮我搞清楚。

Thanks in advance!!! 提前致谢!!!

You may need to tell Rails to use PDFKit middleware. 您可能需要告诉Rails使用PDFKit中间件。 The config\\initializers\\pdfkit.rb file should look like this: config\\initializers\\pdfkit.rb文件应如下所示:

PDFKit.configure do |config|
  ...
end

require "pdfkit"
Rails.application.config.middleware.use PDFKit::Middleware, print_media_type: true

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

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