Wicked_pdf: Works in prod but not dev: `Error: PDF could not be generated! Command Error:`

Created on 19 Jul 2018  路  6Comments  路  Source: mileszs/wicked_pdf

Here's the full error I'm getting in the Rails debugging mode rendering:

Failed to execute: "/Users/nhinchey/.rvm/gems/ruby-2.2.4/bin/wkhtmltopdf" -q "file:////var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf20180719-13653-79d82d.html" "/var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf_generated_file20180719-13653-qaf41r.pdf" Error: PDF could not be generated! Command Error:

And here's the console output:

  Rendered confidentiality_agreements/ca_template_for_rs.html.erb (1.3ms)
"***************\"/Users/nhinchey/.rvm/gems/ruby-2.2.4/bin/wkhtmltopdf\" -q         \"file:////var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf20180719-13653-79d82d.html\" \"/var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf_generated_file20180719-13653-qaf41r.pdf\" ***************"
Completed 500 Internal Server Error in 778ms

RuntimeError (Failed to execute:
"/Users/nhinchey/.rvm/gems/ruby-2.2.4/bin/wkhtmltopdf" -q         "file:////var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf20180719-13653-79d82d.html" "/var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf_generated_file20180719-13653-qaf41r.pdf" 
Error: PDF could not be generated!
 Command Error: ):
  app/controllers/confidentiality_agreements_controller.rb:43:in `create'

The line in the create method it's talking about is this:
raw_pdf = WickedPdf.new.pdf_from_string(html)

And html there is a string that is well formed HTML.

I'm confused, because there is strangely little messaging around what's going wrong.

needs-more-info request-for-help

Most helpful comment

That error message comes from this line, meaning it somehow created a PDF, but the file was empty.

A few things I might check to debug this:

  • Does executing wkhtmltopdf on the command-line work for you?
    /Users/nhinchey/.rvm/gems/ruby-2.2.4/bin/wkhtmltopdf https://google.com google.pdf

  • Does a simpler html document work for you in the Rails console?
    raw_pdf = WickedPdf.new.pdf_from_string('<!doctype html><html><head><title>Hello</title></head><body><h1>Hello, world!</h1></body></html>')

  • Is the directory the tempfiles are written to and read from have permission to write and save files from your app?

If all else fails, you can gem install pry and bundle open wicked_pdf, and drop a require 'pry'; binding.pry around this line, restart your rails server (to pick up the change in the gem source), then when you hit pry's debugging breakpoint inspect the contents of the html tempfiles in the command to see if they have issues.

You can also execute the command from the command-line yourself here.

Don't forget to remove the require 'pry'; binding.pry line when you are done, or you can bundle pristine wicked_pdf if you tried making other edits, to re-download and reinstall the gem to overwrite any edits you might have made.

Please let me know how it goes and report your findings, if any!

All 6 comments

That error message comes from this line, meaning it somehow created a PDF, but the file was empty.

A few things I might check to debug this:

  • Does executing wkhtmltopdf on the command-line work for you?
    /Users/nhinchey/.rvm/gems/ruby-2.2.4/bin/wkhtmltopdf https://google.com google.pdf

  • Does a simpler html document work for you in the Rails console?
    raw_pdf = WickedPdf.new.pdf_from_string('<!doctype html><html><head><title>Hello</title></head><body><h1>Hello, world!</h1></body></html>')

  • Is the directory the tempfiles are written to and read from have permission to write and save files from your app?

If all else fails, you can gem install pry and bundle open wicked_pdf, and drop a require 'pry'; binding.pry around this line, restart your rails server (to pick up the change in the gem source), then when you hit pry's debugging breakpoint inspect the contents of the html tempfiles in the command to see if they have issues.

You can also execute the command from the command-line yourself here.

Don't forget to remove the require 'pry'; binding.pry line when you are done, or you can bundle pristine wicked_pdf if you tried making other edits, to re-download and reinstall the gem to overwrite any edits you might have made.

Please let me know how it goes and report your findings, if any!

  • Running /Users/nhinchey/.rvm/gems/ruby-2.2.4/bin/wkhtmltopdf https://google.com google.pdf on the command line seems to do nothing at all.
  • Running raw_pdf = WickedPdf.new.pdf_from_string('<!doctype html><html><head><title>Hello</title></head><body><h1>Hello, world!</h1></body></html>') gives a similar error:
"***************\"/Users/nhinchey/.rvm/gems/ruby-2.2.4/bin/wkhtmltopdf\" -q         \"file:////var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf20180719-29666-1qlhegv.html\" \"/var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf_generated_file20180719-29666-1crhyj1.pdf\" ***************"
RuntimeError: Failed to execute:
"/Users/nhinchey/.rvm/gems/ruby-2.2.4/bin/wkhtmltopdf" -q         "file:////var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf20180719-29666-1qlhegv.html" "/var/folders/hq/xgy6hpf56xb00g8clcmgg__m0000gn/T/wicked_pdf_generated_file20180719-29666-1crhyj1.pdf" 
Error: PDF could not be generated!
 Command Error: 
from /Users/nhinchey/.rvm/gems/ruby-2.2.4/gems/wicked_pdf-0.10.2/lib/wicked_pdf.rb:79:in `rescue in pdf_from_string'
  • The app appears to have permission to write to those places, since it writes empty PDFs there.

Looking at that last output I posted I noticed that this app is using a terribly old version of wicked_pdf. I updated and it works.

Thanks!

For reference, what version of wicked_pdf, wkhtmltopdf, and rails were you running that caused the issues? I'd like to check if there's perhaps a compatibility issue.

|gem|version|
|-|-|
|Ruby|2.2.4|
|Rails|4.0.13|
|wicked_pdf|0.10.2|
|wkhtmltopdf-binary|0.9.9.1|

But it's working on the heroku production server with those same values -- I don't know if maybe we have something else that's different there or what.

Who wants to use Linux Alpine, should install wkhtmltopdf by apk add wkhtmltopdf command

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thomasbalsloev picture thomasbalsloev  路  3Comments

douglasep picture douglasep  路  4Comments

chaudard picture chaudard  路  5Comments

kavitakanojiya picture kavitakanojiya  路  5Comments

VladFarion picture VladFarion  路  4Comments