Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

$mpdf->setAutoBottomMargin = 'stretch';

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

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.

'setAutoBottomMargin' => 'stretch' 

That's attribute works for me.

Here is my code

$mpdf = new \Mpdf\Mpdf([
  'setAutoBottomMargin' => 'stretch'
]);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top