mPDF自动打印问题

发布于 2024-12-01 02:45:24 字数 1914 浏览 0 评论 0原文

我正在使用 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();

I'm using a php class, mpdf, which generates PDF's very nicely. I'm trying to get the file to automatically print (i.e., open the print dialog) when rendered. I've extended the core functioning with the code below to add javascript to the pdf. The pdf is rendered but without auto-printing. Any help would be great. Thanks!

    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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左耳近心 2024-12-08 02:45:24

这对我来说可以打印生成的 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();

This works for me to print generated PDF file, i used it to print website page contents without menus, banners etc just content with own header and footer

$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();
染墨丶若流云 2024-12-08 02:45:24

您是否尝试过(片段):

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();

Have you tried (snippet):

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);
      }
    }

Credit: Create an Auto-Print PDF

Or, taking the code from the second example in that article:

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();
随梦而飞# 2024-12-08 02:45:24

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

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

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

I use DTukans way + added false as a parameter.

Works in FireFox and IE - did not work for chrome :(

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

青萝楚歌 2024-12-08 02:45:24

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

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

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

I wrote this as an external file and requested a print via javascript.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文