I'm trying to create a PDF file using mPDF class and I need it to auto-height my document and not create blank spaces at bottom.
Here's two images of two different generated PDF with different contents. The left image has more content than the right image, therefore it creates a larger space at the bottom.
I want it to have no space at all. So far, this is what I have tried.
public function __construct()
{
/*
* Encoding
* Size (Array(Xmm, Ymm))
* Font-size
* Font-type
* margin_left
* margin_right
* margin_top
* margin_bottom
* margin_header
* margin_footer
* Orientation
*/
$this->mPDF = new mPDF('utf-8', array(56, 1000), 9, 'freesans', 2, 2, 2, 0, 0, 0, 'P');
}
It starts the document with 1000 height in order to be longer than required at first.
public function write($html, $url)
{
/*
* Writing and remove the content, allows the setAutoTopMargin to work
*
* http://www.mpdf1.com/forum/discussion/621/margin-top-problems/p1
*/
$this->mPDF->WriteHTML($html[0]);
$pageSizeHeight = $this->mPDF->y;
$this->mPDF->page = 0;
$this->mPDF->state = 0;
unset($this->mPDF->pages[0]);
foreach($html as $content)
{
$this->mPDF->addPage('P', '', '', '', '', 2, 2, 2, 0, 0, 0, '', '', '', '', '', '', '', '', '', array(56, $pageSizeHeight));
$this->mPDF->WriteHTML($content);
}
$this->mPDF->Output($url);
}
So, as you can see, while calling the function write()
at some point I grab the Y
value so I can use it to set up the document height. Unfortunately, it does not do what I expect it to do, which is to completely fill the document without any white space.
Playing with the $pageSizeHeight
won't help either because it might work on one document but not on another, like so:
$pageSizeHeight = $this->mPDF->y - 20;