mPDF自动打印问题

时间:2011-08-24 00:19:05

标签: php mpdf

我正在使用一个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 个答案:

答案 0 :(得分:19)

这适用于我打印生成的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();

答案 1 :(得分: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);
      }
    }

信用:Create an Auto-Print 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();

答案 2 :(得分:0)

我把它写成外部文件并通过javascript请求打印。

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

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

答案 3 :(得分:0)

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

在FireFox和IE中工作 - 对chrome不起作用:(

$ mpdf-&GT; SetJS( 'this.print(假);');