wkhtmltopdf with full page background

Lorentz picture Lorentz · May 19, 2011 · Viewed 52.2k times · Source

I am using wkhtmltopdf to generate a PDF file that is going to a printer and have some troubles with making the content fill up an entire page in the resulting PDF.

In the CSS I've set the width and height to 2480 X 3508 pixels (a4 300 dpi) and when creating the PDF I use 0 for margins but still end up with a small white border to the right and bottom. Also tried to use mm and percentage but with the same result.

I'd need someone to please provide an example on how to style the HTML and what options to use at command line so that the resulting PDF pages fill out the entire background. One way might be to include bleeding (this might be necessary anyway) but any tips are welcome. At the moment I am creating one big HTML page (without CSS page breaks - might help?) but if needed it would be fine to generate each page separately and then feed them all to wkhtmltopdf.

Answer

Ryan Q picture Ryan Q · Jan 10, 2014

wkhtmltopdf v 0.11.0 rc2

What ended up working:

wkhtmltopdf --margin-top 0 --margin-bottom 0 --margin-left 0 --margin-right 0 <url> <output>

shortens to

wkhtmltopdf -T 0 -B 0 -L 0 -R 0 <url> <output>

Using html from stdin (Note dash)

echo "<h1>Testing Some Html</h2>" | wkhtmltopdf -T 0 -B 0 -L 0 -R 0 - <output>

Using html from stdin to stdout

echo "Testing Some Html" | wkhtmltopdf -T 0 -B 0 -L 0 -R 0 - test.pdf

echo "Testing Some Html" | wkhtmltopdf -T 0 -B 0 -L 0 -R 0 - - > test.pdf

What did not work:

  • Using --dpi
  • Using --page-width and --page-height
  • Using --zoom