24

I'm generating PDFs using mPDF library, and my header and footer vary in size depending on a couple of parameters.

A static solution would be to set the footer margin, which will solve the overlapping - but as the footer may vary in size this is not a solution I feel happy with. Is there a way to get the footer dimensions and apply the margin accordingly?

2
  • 1
    I'm having the same problem atm, if you found a solution to it can you please update your question?
    – Bogdan
    Commented Feb 4, 2013 at 10:09
  • 1
    I've updated my answer below. Hopefully it will help you. Document margins have to be set for margin_header/footer. You might also need to disable the setAutoTopMargin setting in your config file.
    – Daniel
    Commented Feb 4, 2013 at 10:28

4 Answers 4

28

The problem lies in the documentation of mpdf. I think margin_footer and margin_header is the margin between the document body and these. Instead, margin_footer and margin_header is the document margins, as one would think margin_top and margin_bottom would be.

So, changing the bottom and top margin will decide where the document body starts. And changing the header/footer margin will decide the printing margins.

Hope it helps!

Updated answer

mPDF documentation is a bit off for the constructor call, I guess. The margin_top/bottom argument is actually the content margin, and does not apply for margin_header/footer arguments. (If I recall correctly). The margin_top/bottom is the absolute margin from the top of the document, and should include the height of the header/footer.

Here is the correct way of handling the margins:

/**
 * Create a new PDF document
 *
 * @param string $mode
 * @param string $format
 * @param int $font_size
 * @param string $font
 * @param int $margin_left
 * @param int $margin_right
 * @param int $margin_top (Margin between content and header, not to be mixed with margin_header - which is document margin)
 * @param int $margin_bottom (Margin between content and footer, not to be mixed with margin_footer - which is document margin)
 * @param int $margin_header
 * @param int $margin_footer
 * @param string $orientation (P, L)
 */
new mPDF($mode, $format, $font_size, $font, $margin_left, $margin_right, $margin_top, $margin_bottom, $margin_header, $margin_footer, $orientation);
4
  • 17
    thanks for the update, also the "experimental settings" $mpdf->setAutoTopMargin = 'stretch' and $mpdf->setAutoBottomMargin = 'stretch' settings will make sure the header and footer never overlap the main body, without you having to manually compute the header and footer margins. Quite useful when dealing with dynamic, user-created headers and footers
    – Bogdan
    Commented Feb 5, 2013 at 8:27
  • Woah! Didn't know that was possible. Thank you for that information, it will come in handy very soon.
    – Daniel
    Commented Feb 5, 2013 at 9:19
  • 1
    I tried $mpdf->setAutoBottomMargin = 'stretch' with no success, any instructions how to use it please?
    – Aiphee
    Commented Jan 27, 2015 at 10:00
  • 1
    $mpdf->setAutoBottomMargin = 'stretch'; worked for us
    – KnF
    Commented Aug 7, 2015 at 19:09
23
$mpdf->setAutoBottomMargin = 'stretch';

Worked for me. All I had to do was to make sure I included the option before the footer.

3
  • 1
    It didn't work for me as well until I set it before the footer.
    – David Neto
    Commented Jun 28, 2017 at 13:27
  • 2
    You can use ->setAutoTopMargin() for headers. Commented Jul 6, 2018 at 10:13
  • Its working..I'm using footer by <htmlpagefooter>. $mpdf->setAutoBottomMargin = 'stretch'; it solved the overlap problem. Commented Sep 5, 2018 at 13:46
7

It didn't worked for me, but I've managed to find how to solve this. All I had to do was to set the footer before any content. This is because the footer's height calculation is done before any content processing. But if you add a footer later with SetHTMLFooter, mPDF don't recalculate the footer's height, so it will be 0. This way there will be no page break and the content will overlap the body. So the final solution was:

  1. Create the mPDF object with 'setAutoBottomMargin' => 'stretch'
  2. Add the header
  3. Add the footer
  4. Add the content

Hope it helps.

1
  • love you mate, fixed my problem Commented Sep 17, 2021 at 6:33
0
'setAutoBottomMargin' => 'stretch' 

That's attribute works for me.

Here is my code

$mpdf = new \Mpdf\Mpdf([
  'setAutoBottomMargin' => 'stretch'
]);

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.