Search code examples
phpmpdf

Prevent body overlapping footer in mPDF


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?


Solution

  • 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);