Search code examples
htmlcsspdf-generationwkhtmltopdfhtml-to-pdf

Faster background image on PDF generation with wkhtmltopdf


I'm trying to generate a 5-page PDF based on my HTML file with wkhtmltopdf and it is working well. I'm having a problem, however, regarding the time spent for this task, especifically when using a background image for each page.

When I use a bg_image, wkhtmltopdf uses about ~1.7s to generate, but without it (--nobackground option) only ~0.5s are spent. I've tried to convert my bg_image from .jpg to .png to try to make it faster, but it didn't work. Is there any workaround onto it? Maybe a parameter change or a css change? (Right now my html file contains a declaration for a bg_image on each page, and a global declararion at the start setting its size and "no_repeat" option).

Thanks a lot for your attention!


Solution

  • I don't know about internals of wkhtmltopdf, how they handle a background. If they need some measureable time, i would expect, that they decode and encode the image data of the background image.

    You could also use pdftk utility to apply a prepared pdf-background file to all pages of a pdf, which should be fast:

    pdftk in.pdf background back.pdf output out.pdf

    https://linux.die.net/man/1/pdftk