mPDF document auto height (POS printer)

牧云@^-^@ 提交于 2019-12-20 04:53:28

问题


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;

回答1:


Solved.

I had one issue in my code that was creating that amount of space and it was on CSS structure.

body { font-size: 80% }

And changing to 100% solved the blank space, but I also look into the mPDF class and I found the _setPageSize() function.

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]);
    $this->mPDF->page   = 0;
    $this->mPDF->state  = 0;
    unset($this->mPDF->pages[0]);

    // The $p needs to be passed by reference
    $p = 'P';
    $this->mPDF->_setPageSize(array(56, $this->mPDF->y), $p);

    foreach($html as $content)
    {
        $this->mPDF->addPage();
        $this->mPDF->WriteHTML($content);
    }

    $this->mPDF->Output($url);
}


来源:https://stackoverflow.com/questions/33022101/mpdf-document-auto-height-pos-printer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!