2

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?

Kerstomaat
  • 673
  • 4
  • 13
Martin
  • 1,066
  • 3
  • 20
  • 36
  • 1
    Can I ask what you're using the remaining space between the last line of text and the bottom margin for? If it's a background image or color or something of the like could you re-lay your elements to have the background there always (100% height) and another box laid on top? (covering the background box) – csilk Apr 14 '15 at 04:00
  • it's for a background image (a series of lines that repeat) so your idea sounds the best solution. I will give it a try, hopefully I manage to get it working in CSS – Martin Apr 14 '15 at 21:20

1 Answers1

7

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();
voter
  • 348
  • 1
  • 2
  • 8
  • Seems like this is the most accurate way to get the vertical position. The problem is that all of my output is generated using the HTML method and sometimes content can go on more than one page. I need to be able to fill in the remaining white space of the last page. – Martin Apr 15 '15 at 10:05
  • It is working for multiple pages. In this example it is two pages document. You see mpdf tag. – voter Apr 15 '15 at 11:02
  • But there is no way I can use $mpdf->y within the HTML code that is contained in the WriteHTML method. All of my HTML is contained in this method and I cannot split it in different parts as all of the HTML is rendered by a function of my MVC framework. Thanks anyway – Martin Apr 15 '15 at 18:20
  • You can use writeHTML method more than once: $mpdf->WriteHTML('Line1Line2
    Line3'); $unusedSpaceH = $mpdf->h - $mpdf->y - $mpdf->bMargin - 0.01; $unusedSpaceW = $mpdf->w - $mpdf->lMargin - $mpdf->rMargin ; $mpdf->WriteHTML("
    " );
    – voter Apr 15 '15 at 21:08
  • @voter Sorry to bring up an old thread but I find the `$mpdf->y` isn't updating properly and `$mpdf->lasth` also shows the wrong values. How do I get it to update? – Don Rhummy Oct 01 '15 at 01:58
  • @DonRhummy This approach is working for me in 5.7.3. Maybe downgrade will help in your case. If not, open new question with you case details. – voter Oct 02 '15 at 08:55
  • @voter they only updated properly if I fully closed and completed the document (close body/html tags and had full header). I think it's due to the way the code handles `float` objects – Don Rhummy Oct 02 '15 at 18:11