19

The --margin-top option is for the contents margin, but I would like to set the margin from the top of the page to the header. The project I'm working on allows users to create header and footer themselves, so the height of the header or footer is dynamic.

I don't know how to do it so can anyone help?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
GordonZ
  • 364
  • 1
  • 2
  • 10

3 Answers3

32

The built-in options for top margin are

  1. --margin-top (as you mentioned above) and
  2. --header-spacing Spacing between header and content in mm (refer: http://wkhtmltopdf.org/usage/wkhtmltopdf.txt).

None of them will probably help you as there is no option (at least to my knowledge) that can explicitly set some margin from the top of the page to the header. However, in your case, you could explore --header-html <url> and add a html header. This can take an HTML where you could probably set the custom header and add space/margin accordingly and then the HTML gets displayed in on the header.

mehmet
  • 7,720
  • 5
  • 42
  • 48
Ironluca
  • 3,402
  • 4
  • 25
  • 32
  • 1
    +1. You can just use a web page as header, you will set margin from the top of the page using `` margin or padding on the header HTML content – lorenzo-s Sep 20 '16 at 12:52
  • 3
    Thank you, I did try it but because I forgot to remove the margin-top option it didn't work. After I removed the option it works now. – GordonZ Sep 22 '16 at 12:01
  • 1
    I was struggling with this, too - adding the margin-top style to the header and removing the --margin-top switch for wkhtmltopdf worked for me. Thanks, @lorenzo-s – jenni-mm2 May 16 '17 at 14:41
10

Use -T -B -L and -R for margins.

wkhtmltopdf -B 13 -L 13 -R 13 -T 53  /tmp/e0cb9c4597860b5abfbf2bafc1000d5a.html /tmp/e0cb9c4597860b5abfbf2bafc1000d5a.pdf
tumbudu
  • 699
  • 11
  • 26
1

-T 10 is working by adding an empty HTML.

wkhtmltopdf.exe -T 10 --header-html header.html  content.html generatedpdf.pdf

the empty html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-16">
  </head>
  <body >
  </body>
</html>
Mxooo
  • 11
  • 1