What Festus mentioned is correct until a certain point.
For some reason it doesn't always works with my implementation. So based on another post and my own recommendations, you would only need to add the following code to your html file:
body {
background: url("https://via.placeholder.com/700x963?text=DRAFT");
background-position: center;
background-repeat: repeat-y;
background-size: 100%;
background-attachment: fixed;
}
Now, by this time the background should be repeating but out of position. The solution is to fix the size of the image, as 700x963 will not cut it and it depends on your pdf configuration.
Lets remember that you can tell wkhtml two important things: page-size and margins (top, bottom, left, right). Depending on the page-size that you have selected, please consult the following link to get an exact value in inches (e.g. "Letter" is 8.5x11 inches). Now, depending on the margins you set for wkhtml, you will have to substract those margins from the page sizes (e.g. if we still have "Letter" as a page-size, and we set 1 inch in all margins, the result should be 6.5x9 inches). Your image must be this exact size.
// The previous paragraph can be interpreted as:
// Letter page-size
const page_size = {
width: "8.5in",
height: "11in",
}
const image_width = to_px(page_size.width) - to_px(margin_left) - to_px(margin_right)
const image_height = to_px(page_size.height) - to_px(margin_top) - to_px(margin_bottom)
const url = "https://via.placeholder.com/" + image_width + "x" + image_height + "?text=DRAFT"
A couple of recommendations:
- Use these sizes in inches, not in pixels, for wkhtml and your css. (This is how I am doing it, it might work with pixels but I am not sure)
- If one of the margins is
0
, remember to write the unit (e.g. 0in
), or wkhtml will crash.
- Pass the following configuration to wkhtml:
--background
--encoding UTF-8
so it can print symbols ($)
--enable-smart-shrinking
so it doesn't make a mess of the sizes
- The image should not have transparency. wkhtml can corrupt an image that has transparency at any moment and in any page (I have had large documents where the background image is painted only in the first 6 pages correctly). I still don't know if this is only limited to background images.
- Use absolute urls for the images. wkhtml might not report an error but you will not see your image.