Debugging PDFKit and WKHTMLToPDF
When PDFKit errors out with an unhelpful error like: RuntimeError (command failed (exitstatus=1): /bin/wkhtmltopdf --page-size A4 - -):, try the following:
- Add
config.verbose = trueto its configuration - Add
load_error_handling: 'ignore'to itsconfig.default_options - Save the rendered HTML to a file by modifying pdfkit.rb’s
to_pdfmethod with this just before theinvokedeclaration.
open('/tmp/pdfkit-source.html', 'a') { |f| f.puts @source.to_s }Once you’ve got the html saved, you can inspect it for errors, and manually run wkhtmltopdf on the CLI using it as input like so:
cat /tmp/pdfkit-source.html | <the wkhtmltopdfbin command that is failing> out.pdfHopefully one of these techniques can highlight where your problems lie, and get you to a resolution.
Tweet