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

时间:2015-10-08 17:15:10

标签: php mpdf

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

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

enter image description here

我希望它根本没有空间。到目前为止,这是我尝试过的。

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值,以便我可以使用它来设置文档高度。不幸的是,它没有做我期望它做的事情,即完全填充文档而没有任何空白区域。

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

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

1 个答案:

答案 0 :(得分:1)

<强>解决。

我的代码中有一个问题是创建了大量的空间而且它是在CSS结构上。

body { font-size: 80% }

更改为100%解决了空白区域,但我也查看了mPDF类,并找到了_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);
}
相关问题