1

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!

Arne Cordes
  • 581
  • 1
  • 6
  • 22

1 Answers1

1

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.

Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
  • I already use the wicked_pdf_image-helper and it outputs file://-links – but that makes no difference at all.The server is fine while generating the pdf. The master version didn´t help. I found out that the problem is related to the wicked_pdf-gem, when i use wkhtmltopdf via command line with the same options, it works! – Arne Cordes Nov 22 '12 at 11:13
  • I use system(wkhtmltopdf ...) now and it works as expected, even with multiple large images. But a solution for wicked_pdf would be great, although. – Arne Cordes Nov 22 '12 at 11:37
  • @ArneCordes If you could I would be really interested in more detail. Can you post the options you are calling render with and the flags you are passing to command-line wkhtmltopdf? – Unixmonkey Nov 22 '12 at 17:01
  • 1
    I finally figured out that generating of my PDF worked fine… from the beginning. The problem lies in sending the generated file to the client. After trying to do it on my own and generate the PDF via command line – system(wkhtml ...) – and using send_file(), i had the same result like using wicked_pdf – a broken file. This seems to be related to nginx, but hey, a new problem is better than the same problem all day long ;-) Thank you for your responses! I still like wkhtmltopdf and wicked_pdf. – Arne Cordes Nov 23 '12 at 11:18