When generating a PDF sometimes, it hangs and never completes, forcing me to have to shut down the rails server and restart it. I have been unable to figure out a pattern in the HTML file that is causing this to happen because I can run the same command and arguments that Rails run and it works just fine. So I'm not sure if this is an issue within Rails or wkhtmltopdf.
Here's an example of it hanging in the rails console and me having to use ctrl C to shut down the rails server.
Rendered [obfuscated].html.erb (39815.3ms)
Rendered [obfuscated].html.erb (41037.4ms)
Rendering layouts/report_header.html.erb
Rendered layouts/report_header.html.erb (12.0ms)
Rendering layouts/report_footer.html.erb
Rendered layouts/report_footer.html.erb (0.4ms)
[wicked_pdf]: ["/root/myapp/bin/wicked_pdf", "--page-size", "Letter", "--margin-top", "25", "--margin-bottom", "17", "--margin-left", "10", "--margin-right", "10", "--header-html", "file:////tmp/wicked_header_pdf20191205-6854-1k3i748.html", "--footer-html", "file:////tmp/wicked_footer_pdf20191205-6854-cc6ntg.html", "file:////tmp/wicked_pdf20191205-6854-19958ts.html", "/tmp/wicked_pdf_generated_file20191205-6854-133sa6i.pdf"]
^CCompleted 500 Internal Server Error in 295092ms (ActiveRecord: 6927.8ms)
RuntimeError (Failed to execute:
["/root/myapp/bin/wicked_pdf", "--page-size", "Letter", "--margin-top", "25", "--margin-bottom", "17", "--margin-left", "10", "--margin-right", "10", "--header-html", "file:////tmp/wicked_header_pdf20191205-6854-1k3i748.html", "--footer-html", "file:////tmp/wicked_footer_pdf20191205-6854-cc6ntg.html", "file:////tmp/wicked_pdf20191205-6854-19958ts.html", "/tmp/wicked_pdf_generated_file20191205-6854-133sa6i.pdf"]
Error: PDF could not be generated!
Command Error: ):
app/controllers/[obfuscated]/reports_controller.rb:96:in `block (2 levels) in generate_pdf'
app/controllers/[obfuscated]reports_controller.rb:48:in `generate_pdf'
- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 12/05/2019 10:13 ===
- Goodbye!
Exiting
As mentioned in my stackoverflow post, if I comment out the exe_path within my config/initializers/wicked_pdf then it works just fine. However, I am using a custom exe_path to leverage xvfb-run so that I can render charts in my application.
Here's what my exe_path looks like: exe_path: Rails.root.join("bin","wicked_pdf").to_s
and here's what bin/wicked_pdf looks like:
#!/bin/bash
xvfb-run -- /usr/local/bin/wkhtmltopdf "$@"
I am also using this in Rails development, so I'm not sure if the same thing happens in production or not. Either way, I can't make it to production (production doesn't matter) if I can't create what I'm trying to do in development mode.
Generate the PDF document and render it to the user's browser.
Ubuntu 18.04 via Docker
wicked_pdf gem version (output of cat Gemfile.lock | grep wicked_pdf):
wicked_pdf (1.4.0)
wicked_pdf
wkhtmltopdf version (output of wkhtmltopdf --version): wkhtmltopdf 0.12.5 (with patched qt)
I suspect this may have to do with you running a single-threaded web server in development mode, like webrick. This can cause a deadlock if the PDF you are building must make a web request to finish rendering the PDF (like an image or stylesheet). The PDF stops generating midway while it goes to fetch that resource, but the request gets queued (instead of handled), causing wkhtmltopdf to wait forever.
I would attempt to isolate that to see if that is what is happening to you, either by introducing a multi-threaded webserver in development, to more closely match what happens in production (I like Puma), or make it so that your templates never have to load an external dependency from the same webservice (like inlining the CSS or base64-encoding images), which will usually have a side-benefit of speeding up creation.
Please let me know how it goes!
Hey @unixmonkey,
You're right man! It doesn't do this in production at all. I'm OK with it being this way in development now that I realized that it's only isolated to development. I really appreciate the input here. Feel free to close this one out unless there were some other things you wanted me to try.
Thanks again man!
Most helpful comment
I suspect this may have to do with you running a single-threaded web server in development mode, like webrick. This can cause a deadlock if the PDF you are building must make a web request to finish rendering the PDF (like an image or stylesheet). The PDF stops generating midway while it goes to fetch that resource, but the request gets queued (instead of handled), causing
wkhtmltopdfto wait forever.I would attempt to isolate that to see if that is what is happening to you, either by introducing a multi-threaded webserver in development, to more closely match what happens in production (I like Puma), or make it so that your templates never have to load an external dependency from the same webservice (like inlining the CSS or base64-encoding images), which will usually have a side-benefit of speeding up creation.
Please let me know how it goes!