开发者

Wicked pdf doesn't work at server. but worked locally.(Seems like can not find template)

RuntimeError (PDF could not be generated! Error: Failed loading page http://works

This is production.log. I've ensure wkhtmltopdf(0.9.5) installed at server. And specify wkhtmltopdf path to the wicked_pdf initialize file.

I had try two different way to implement the logic.

开发者_高级运维

render a pdf format. like this

  respond_to do |format|
    format.pdf do
      render :pdf         => "pdf_file_name",
             :template    => 'retailers/scorecard.pdf.erb'
    end
  end  

Or save the template to a file and send this file to user

  file_name = "pdffile.pdf"
  pdf = render_to_string :pdf => file_name, :template    => 'retailers/scorecard.pdf.erb'

  file_path = Rails.root.join("public", "pdfs", file_name) 
  File.open(file_path, 'wb') do |file|
    file << pdf
  end

  send_file file_path

Both of them are work locally but doesn't work at server. and log are same as above. The only different I can think is My OS is mac but server is Ubuntu.

btw I've use wkhtmltopdf in command line at server. It works.


Do you have a X-server installed on the ubuntu box? Wkhtmltopdf depends on it, or at least on xvfb. Xvfb replaces the graphical environment for this particular case.

If you want to go with xvfb you can do the following:

apt-get install xvfb    
Xvfb :1 -ac -screen 0 640x480x16 &

Line 2 starts a a xvfb frame buffer in the background that can be used by wkhtmltopdf.

Make sure you are using the correct version (32/64 bit) depending on your system's architecture!


Solution by OP.

Use another way

file_name = "example.pdf"
pdf = WickedPdf.new.pdf_from_string(render_to_string('example.pdf.erb'))

  file_path = Rails.root.join("public", "pdfs", file_name) 
  File.open(file_path, 'wb') do |file|
    file << pdf
  end

  send_file file_path
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜