Get position of text in mPDF to determine vertical

2020-03-31 04:02发布

问题:

I am generating PDFs with the mPDF class and wondering if and how would it be possible to determine the position of the last line of text in document that is generated mPDF?

I need an HTML box to be cover in height any remaining space between the last line of text and the bottom margin of the document. By setting the html element to height: 100% that pushes the element to the new page and covering the whole height of the new page.

The content of the page is generated dynamically based on a number of factors, so I can never be sure at which vertical position the last line will be at.

If I knew the vertical position of the last line, I could subtract the value from the total page height and then set by CSS the element to have that height.

Is that possible or are there other solutions?

回答1:

You can use for this purpose "$mpdf->y" (current position in user unit for cell positioning):

$mpdf=new mPDF('', 'A4');
$mpdf->WriteHTML('Line1<pagebreak>Line2<br>Line3');
//
$unusedSpaceH = $mpdf->h - $mpdf->y - $mpdf->bMargin;
$unusedSpaceW = $mpdf->w - $mpdf->lMargin - $mpdf->rMargin;
//
$mpdf->Rect($mpdf->x, $mpdf->y, $unusedSpaceW, $unusedSpaceH);
$mpdf->Output();


标签: php css pdf mpdf