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?

3 Answers 3

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.

3
  • 1
    +1. You can just use a web page as header, you will set margin from the top of the page using <body> margin or padding on the header HTML content
    – lorenzo-s
    Commented Sep 20, 2016 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
    Commented Sep 22, 2016 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
    Commented May 16, 2017 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
2
  • Works, thanks! the other answer is not working for me, no matter margn-top is used or not Commented Jul 19, 2019 at 10:19
  • The rest of them is working except bottom.
    – enisn
    Commented Jun 3, 2022 at 12:40
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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.