12

我正在使用一个 php 类 mpdf,它可以很好地生成 PDF。我试图让文件在渲染时自动打印(即打开打印对话框)。我已经使用下面的代码扩展了核心功能,将 javascript 添加到 pdf 中。pdf 被渲染但没有自动打印。任何帮助都会很棒。谢谢!

    require('mpdf.php');
    class PDF_JavaScript extends mPDF {
        var $javascript;
        var $n_js;

        function IncludeJS($script) {
            $this->javascript=$script;
        }
        function _putjavascript() {
            $this->_newobj();
            $this->n_js=$this->n;
            $this->_out('<<');
            $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
            $this->_out('>>');
            $this->_out('endobj');
            $this->_newobj();
            $this->_out('<<');
            $this->_out('/S /JavaScript');
            $this->_out('/JS '.$this->_textstring($this->javascript));
            $this->_out('>>');
            $this->_out('endobj');
        }
        function _putresources() {
            parent::_putresources();
            if (!empty($this->javascript)) {
                $this->_putjavascript();
            }
        }

        function _putcatalog() {
            parent::_putcatalog();
            if (!empty($this->javascript)) {
                $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
            }
        }
    }
    class PDF_AutoPrint extends PDF_Javascript { 
        function AutoPrint($dialog=false) { //Embed some JavaScript to show the print dialog or start printing immediately
        $param=($dialog ? 'true' : 'false');
        $script="print($param);";
        $this->IncludeJS($script); } }


$mpdf = new PDF_AutoPrint('', 'Letter', 0, '', 12.7, 12.7, 14, 12.7, 8, 8);

$stylesheet = file_get_contents('eabill.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($message,2);
$mpdf->AutoPrint(true);

$mpdf->Output();
4

4 回答 4

20

这适用于我打印生成的 PDF 文件,我用它来打印没有菜单、横幅等的网站页面内容,只是带有自己的页眉和页脚的内容

$header = 'Document header';
$html   = 'Your document content goes here';
$footer = 'Print date: ' . date('d.m.Y H:i:s') . '<br />Page {PAGENO} of {nb}';

$mpdf = new mPDF('utf-8', 'A4', 0, '', 12, 12, 25, 15, 12, 12);
$mpdf->SetHTMLHeader($header);
$mpdf->SetHTMLFooter($footer);
$mpdf->SetJS('this.print();');
$mpdf->WriteHTML($html);
$mpdf->Output();
于 2012-07-14T13:46:42.447 回答
5

您是否尝试过(片段):

class PDF_AutoPrint extends PDF_Javascript { 
    function AutoPrint($dialog=false) {
      //Embed some JavaScript to show the print dialog or start printing immediately
      if( $dialog ){
        $script="this.print();";
        $this->IncludeJS($script);
      }
    }

学分:创建自动打印 PDF

或者,从该文章的第二个示例中获取代码:

require('mpdf.php');

class PDF_AutoPrint extends PDF_Javascript { 
  function AutoPrint( $dialog=false ){
    if( $dialog ){
      $this->_newobj();
      $this->n_js=$this->n;
      $this->_out('<<');
      # Not sure whether this line is spot on, may need tweaking
      $this->_out('/OpenAction '.($this->n+2).' 0 R/Type/Catalog/Pages 1 0 R/PageMode/UseNone/PageLayout/OneColumn');
      $this->_out('>>');
      $this->_out('endobj');
      $this->_newobj();
      $this->_out('<<');
      $this->_out('/Type/Action/S/Named/N/Print');
      $this->_out('>>');
      $this->_out('endobj');
    }
  }
}


$mpdf = new PDF_AutoPrint('', 'Letter', 0, '', 12.7, 12.7, 14, 12.7, 8, 8);

$stylesheet = file_get_contents('eabill.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($message,2);
$mpdf->AutoPrint(true);

$mpdf->Output();
于 2011-09-01T06:38:34.503 回答
1

我使用DTukans方式+添加false作为参数。

适用于 FireFox 和 IE - 不适用于 chrome :(

$mpdf->SetJS('this.print(false);');

于 2013-02-20T15:56:04.547 回答
0

我将其写为外部文件并请求通过 javascript 打印。

post_to_url("pdf.export.php", {htmlForPdf:pdf})

https://stackoverflow.com/a/133997/903454

于 2012-12-03T11:10:53.917 回答