Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

wkhtmltopdf - rails 3 and PDFkit

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...