首页 文章

带有cut命令的PHP mPDF - 打印太多纸张

提问于
浏览
1

我有一个名为“Start STP 100”的打印机收据,我的网站使用mPDF库创建一个带收据格式的PDF .

以下代码是我生成PDF的类:

class printdoc
{
    private $width;

    public function __construct()
    {
        require('mPDF/mpdf.php');

        $this->width = 80;

        // Default values
        $this->mPDF = new mPDF('utf-8', array($this->width, 1000), 9, 'Segoe UI', 5, 5, 0, 0, 5, 5, 'P');
        $this->mPDF->setAutoTopMargin = 'pad';
    }

    public function write($html, $url)
    {
        $this->mPDF->WriteHTML($html[0]);
        $this->mPDF->page   = 0;
        $this->mPDF->state  = 0;
        unset($this->mPDF->pages[0]);

        $p = 'P';
        // At this point the Y size is set according to the dimensions of the PDF
        // So the value '1000' set in the __construct() has no effect.
        $this->mPDF->_setPageSize(array($this->width, $this->mPDF->y), $p);

        foreach($html as $content)
        {
            $this->mPDF->addPage();
            $this->mPDF->WriteHTML($content);
        }

        $this->mPDF->Output($url);
    }

    public function create($data)
    {
        $html  = '<html>';
        $html .= '  <head></head>';
        $html .= '  <body>';
        $html .=        $this->header($data);
        $html .=        $this->body($data);
        $html .=        $this->footer($data);
        $html .= '  </body>';
        $html .= '</html>';

        return $html;
    }
}

现在为了创建收据并保存它,我有这个功能:

function printreceipt($data)
{
    require('printdoc.php');

    $file = 'test-receipt.pdf';
    $html = $this->printdoc->create($data);

    $this->printdoc->write($html, $file);
}

使用Chrome PDF Viewer时生成的文件具有正确的尺寸(宽度和高度),但是当发送到打印机时,它会打印太多纸张 .

基本上这是PDF的大小(在浏览器上查看时):

---
|   |
|   |
|   |
 ---

这是打印时纸张的大小:

---
|   |
|   |
|   |
|   |
|   |
|   |
|   |
|   |
|   |
|   |
 ---

是否有任何方法强制mPDF切割文件或在我的 html 之后写任何命令以便打印机解释和剪切?

1 回答

相关问题