wkhtmltopdf with wicked_pdf issue?

2.8k Views Asked by At

I am using wkhtmltopdf(0.10.0 rc2) and wicked_pdf(0.9.9) for generating pdf report. While running i got the following error.

RuntimeError (Error: Failed to execute:
["/usr/bin/wkhtmltopdf", "-q", "--page-size", "A4", "--margin-top", "5", "--margin-bottom", "10", "--margin-left", "5", "--margin-right", "5", "file:////tmp/[\"wicked_pdf\", \".html\"]21873-0", "/tmp/[\"wicked_pdf_generated_file\", \".pdf\"]21873-0"]
Error: PDF could not be generated!
 Command Error: Error: Failed loading page file:////tmp/["wicked_pdf", ".html"]21873-0 (sometimes it will work just to ignore this error with --load-error-handling ignore)
)

my controller are:

render :pdf => "#{@note_name}",:layout => '/layouts/pdf_template.html.erb', :template=>'pdf/financial/maintenance_exp/maintenance_exp_pdf.html.erb'

Any help would be appreciated.

FYI:when I do :show_as_html=>true its not throwing any error.its rendering the html in browser.

2

There are 2 best solutions below

1
On

I was getting the same error and fixed it by following directions here

https://github.com/mileszs/wicked_pdf/issues/157

Namely I switched to temporary fix of gem by changing gemfile entry to:

gem 'wicked_pdf', github: 'mileszs/wicked_pdf'

But note that in my case the issue was file locations on windows having file:// instead of file:/// -- so it may not help you. On the other hand, it was the same error message so maybe worth a shot.

0
On

Similar error, i was trying to use WickedPdf.new.pdf_from_html_file

I ended up using WickedPdf.new.pdf_from_string with following helper to generate rendered html string:

  def pdf_html
    ApplicationController.renderer.new.render(
      template: 'purchase_orders/pdf.html.erb',
      layout: 'layouts/pdf_layout.html.erb',
      locals: { abc: 'xyz' },
      formats: [:html]
    )
  end

Obviously assumes ApplicationController is present

My rails version: 6.0.1

Might help other searching for similar errors.