问题描述
我正在尝试使用 mPDF类创建PDF文件,我需要将其自动-高度我的文档,而不在底部创建空白.
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.
这是两个具有不同内容的不同生成的PDF的两个图像.左图比右图具有更多的内容,因此在底部创建了更大的空间.
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');
}
它以1000高度的高度开始文档,以使其比起初所需的时间更长.
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);
}
因此,如您所见,在某个时候调用函数write()
时,我抓住了Y
值,以便可以使用它来设置文档高度.不幸的是,它没有执行我期望的操作,即完全填充文档而没有任何空白.
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.
玩$pageSizeHeight
都无济于事,因为它可能对一个文档有效,而对另一个文档无效,就像这样:
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;
推荐答案
已解决.
我的代码中有一个问题是创建了这么多的空间,它在CSS结构上.
I had one issue in my code that was creating that amount of space and it was on CSS structure.
body { font-size: 80% }
更改为100%可以解决空白,但是我也查看了mPDF类,发现了_setPageSize()
函数.
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);
}
这篇关于mPDF文档自动高度(POS打印机)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!