Php mPDF文档自动高度(POS打印机)
我正在尝试使用创建PDF文件,我需要它自动调整文档高度,而不是在底部创建空白 这里有两张图片,是两张不同内容的PDF文件。左侧图像的内容比右侧图像多,因此它在底部创建了更大的空间 我希望它没有任何空间。到目前为止,这就是我所尝试的Php mPDF文档自动高度(POS打印机),php,mpdf,Php,Mpdf,我正在尝试使用创建PDF文件,我需要它自动调整文档高度,而不是在底部创建空白 这里有两张图片,是两张不同内容的PDF文件。左侧图像的内容比右侧图像多,因此它在底部创建了更大的空间 我希望它没有任何空间。到目前为止,这就是我所尝试的 public function __construct() { /* * Encoding * Size (Array(Xmm, Ymm)) * Font-size * Font-type * margin_
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;
已解决。 我的代码中有一个问题,就是在CSS结构上创建了那么多的空间
body { font-size: 80% }
并更改为100%,解决了空白空间,但我也查看了MPDF类,发现了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);
}