rails 3 and PDFkit
Asked Answered
T

2

2

I'm trying to follow this tutorial.

When I'm adding .pdf to my url it does nothing. My controller has:

respond_to :html, :pdf.

My mime type has been declared.

I tried this too:

respond_to do |format|
  format.html
  format.pdf {
    html = render_to_string(:layout => false , :action => "www.google.fr")
    kit = PDFKit.new(html)
    send_data(kit.to_pdf, :filename => "candidats.pdf", :type => 'application/pdf')
    return # to avoid double render call
  }
end

but it does not work and I don't get errors. My browser keep waiting for localhost, but nothing happens.

So how should I try to use pdfkit ?

edit 2 :

According to my rails' logs, rails render successfully the HTML. I saw this in the .log, rails doesn't send it to webrick nor to my browser. And my browser keeps waiting, and waiting and nothing happen. I only have little pictures here.

edit 3 : My webrick server seem unable to respond to other request, once he starts getting a .pdf version of my url, any ideas ?

edit 4 :

I am using rails 3.1, wkhtmltopdf 0.9.5 (windows installer) and pdfkit 0.5.2

Tegan answered 23/9, 2011 at 14:56 Comment(2)
sorry, can't help you with your problem, but thanks for introducing pdfkit to me! always thought prawn was as 'good' as it gets.Vasquez
There's a rather huge mismatch between the code you posted and the one that's shown on the tutorial you linked to. Are you sure you're following the tutorial?Misguidance
T
6

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.

Tegan answered 28/9, 2011 at 9:16 Comment(2)
Putting this setting in development mode does the job, but how do you get it to work in Production (do you use heroku?). If I set the config.threadsafe! flag in production my job times out.Slumlord
I'm not using heroku. It works perfectly for me in production. (But i'm in local still). This might help you : jguimont.com/post/2627758108/… . i havn't tryied.Tegan
T
3

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

Tegan answered 26/9, 2011 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.