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?
Under Print Options, select the Print background colors and images check box. Close the Print dialog box, and go to File > Print.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With