rails 3 and PDFkit

狂风中的少年 提交于 2019-11-29 08:39:49

I found a better way to access my .pdf urls even in developpement mode.

# Enable threaded mode
config.threadsafe!

# Code is not reloaded between requests
#config.cache_classes = true

Config.cache_classes is a comment, cause i had some problems when it wasn't. This way pdfkit works wonder even with rails 3.1. But, you don't reload code between requests.

That's not really a problem, cause you first work on your html, and you switch configuration, in order to check the pdf result. This way you don't have to bother about your production database.

Thanks to another stack overflow answer I got part of solution.

This works:

html = '<html><body>Toto de combats</body></html>'
@pdf = PDFKit.new(html)

send_data @pdf.to_pdf, :filename => "whatever.pdf",
                  :type => "application/pdf",
                  :disposition  => "attachement"

You can replace attachement by inline, so the pdf is displayed in your browser.

In the stack overflow answer I spoke about (I don't remember the link), the .to_pdf was missing, but is mandatory. Otherwise PDF reader doesn't recognize it.

I'm trying to get this work with a .pdf url.

Edit 3:

My problem with .pdf urls is solved. known issue with rails 3.1, but google was unable to find them.

explanation : explanation

Workaround (hadn't tryied yet). workaround

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