Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

php - mPDF document auto height (POS printer)

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.

enter image description here

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...