Reputation: 581
wicked_pdf (or wkhtmltopdf) behaves strange in production mode. It took a bit time to track down the problem, but seemingly the reason that my PDF is broken are too much images or too large file sizes.
When i try to use a 300dpi pic (4 MB), it´s broken. When i decrease to 72 dpi, it works… sometimes. When i use just a thumbnail, it works always.
I made a test: One coversheet with a small test image works. Two coversheets also, … up to 30 sheets it works. When i attach cover sheet 31, my pdf is broken, always.
In development everything is fine, 300dpi images, 5 MB, 50 MB, no problem.
Does anybody know about this problem? And how to fix it? :-)
PS: when i do wkhtmltopdf via command line it shows no images at all, so it´s hard to test if wkhtmltopdf or wicked_pdf is the issue. I really don´t like generating PDFs!
Upvotes: 1
Views: 1327
Reputation: 18784
wkthmltopdf does a best-effort to gather up and render all the resources in a pdf just like the webkit browser that powers it. If it isn't able to retrieve and render them all in time, it can sometimes output an unfinished pdf.
There are a few things we can check or do to optimize this process.
Make sure all your assets (images, javascript, css) are correctly rendered with a full path like 'http://images/foo.jpg', not a relative one like '/foo.jpg'.
This causes wkhtmltopdf to reach out over the web to get these assets, and sometimes that isn't quick enough, so if you can, try to use filesystem resource links like 'file:///projects/foo/images/foo.jpg'. The wicked_pdf_image and other related helpers attempt to do this for you, but depending on what version of Rails you are on, the result may be a little different.
Wkhtmltopdf uses a lot of memory and cpu (especially with big images), make sure your production server isn't starved for it.
The git master version of wicked_pdf has an optimization to use tempfiles instead of I/O streaming. Point your Gemfile at that git://github.com/mileszs/wicked_pdf.git and see if that improves things for you. A new version of the gem will likely be published in the next few days (I do a lot of the maintenance and release management for wicked_pdf).
Please report back. I'm interested to know if any or all of the above improve the situation.
Upvotes: 1