Rails 3 + PDFKit: How to convert a view to PDF?

假装没事ソ 提交于 2019-12-01 12:59:17

first you need to tell the application to use pdfkit as a middleware.

So somewhere in an initializer you need to put:

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

PDFKit.configure do |config|
  config.wkhtmltopdf = 'windows_path_to_wkhtmltopdf'
end

After this if you call

http://localhost:3001/jobs/45/invoice.pdf

a pdf should be generated for you.

PDFkit is a middleware that intercepts the pdf format rendering the page accordingly.

If you want you can also restrict pdfable routes in the config through regexes or string.

Just a caveat, if you have images in the page they must be called with an absolute path.

We are also finding some problems with pdfkit 0.5.0 but things are working fine with version 0.4.6 it is something to do with paths so maybe it can solve your issues.

Ashish

With the help of PDFKit middle-ware you can only view the html page in pdf format in the browser. If want to show the download prompt then you have to set the header in your action.

headers["Content-Disposition"] = "attachment; filename=export"

Further, If you want to save the pdf file in server then you can try this link.

Save PDF file shown by PDFKit middleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!