Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS to PDF, background image on all pages

I'm generating PDFs from user submitted HTML/CSS. The client has requested that the PDFs have a background image to add some branding. The PDF library is wkhtmltopdf11RC1.

I can make the images print in the background using the PDF conversion library but the real problem is that the height of the body is not guaranteed to be a multiple of the paper height, and so the last page usually get's cut off and the background image isn't shown.

The PDF library does have javascript support but the element.offsetHeight doesn't seem accurate, and so I can't check how much to pad the body by in order to make a full page.

Has anyone had a similar experience or does anyone have a bright idea?

like image 659
Josh Avatar asked Sep 14 '12 15:09

Josh


People also ask

How do I make the background image print on every page?

Under Print Options, select the Print background colors and images check box. Close the Print dialog box, and go to File > Print.

How do I put different background images on different pages in HTML?

Use the CSS line that you have already ( body {background-image:url(greentea. jpg);} ) on each page in it's <head><style></style></head> tags and just change the background image on every page. Be aware that it is advised to have a separate CSS file to avoid any inline styling.

How do you put a background on a whole page in HTML?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.


1 Answers

Unfortunately var currentBodyHeight = $('body').height(); doesn't give an accurate indication of the height of the body for some reason, as mentioned in the original post. I've used PDFtk as suggested by Nenotlep.

It wasn't too tricky, just had to ensure that files were being saved and erased properly and I always kept a reference to the temporary files.

like image 76
Josh Avatar answered Nov 05 '22 07:11

Josh