mPDF 文档自动高度(POS 打印机)

mPDF document auto height (POS printer)

我正在尝试使用 mPDF class 创建 PDF 文件,我需要它来自动调整我的文档高度而不是在底部创建空白 spaces。

这是两个不同内容的不同生成的 PDF 的两个图像。左图的内容比右图多,因此在底部创建了一个更大的space。

我希望它完全没有 space。到目前为止,这是我尝试过的。

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

它以 1000 高度开始文档,以便比最初要求的更长。

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

因此,如您所见,在某个时候调用函数 write() 时,我获取了 Y 值,因此我可以使用它来设置文档高度。不幸的是,它没有做我期望的事情,即完全填充文档而没有任何白色 space。

使用 $pageSizeHeight 也无济于事,因为它可能适用于一个文档但不适用于另一个文档,例如:

$pageSizeHeight = $this->mPDF->y - 20;

已解决。

我的代码中有一个问题是创建了 space 的数量并且它在 CSS 结构上。

body { font-size: 80% }

并更改为 100% 解决了空白 space,但我也查看了 mPDF class 并找到了 _setPageSize() 函数。

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