Php mPDF自动打印问题

Php mPDF自动打印问题,php,mpdf,Php,Mpdf,我正在使用一个php类mpdf,它可以很好地生成PDF。我试图让文件在渲染时自动打印(即打开打印对话框)。我用下面的代码扩展了核心功能,将javascript添加到pdf中。pdf已呈现,但未自动打印。任何帮助都会很好。谢谢 require('mpdf.php'); class PDF_JavaScript extends mPDF { var $javascript; var $n_js; function IncludeJS(

我正在使用一个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();
require('mpdf.php');
类PDF_JavaScript扩展了mPDF{
var$javascript;
var$n_js;
函数IncludeJS($script){
$this->javascript=$script;
}
函数_putjavascript(){
$this->_newobj();
$this->n_js=$this->n;
$this->_out('>');
$this->_out('endobj');
$this->_newobj();
$this->_out('>');
$this->_out('endobj');
}
函数_putresources(){
父项::\u putresources();
如果(!empty($this->javascript)){
$this->_putjavascript();
}
}
函数_putcatalog(){
父项::_putcatalog();
如果(!empty($this->javascript)){
$this->_out('/Names>');
}
}
}
类PDF_AutoPrint扩展了PDF_Javascript{
函数AutoPrint($dialog=false){//嵌入一些JavaScript以显示打印对话框或立即开始打印
$param=($dialog?'true':'false');
$script=“打印($param);”;
$this->IncludeJS($script);}
$mpdf=新的PDF_自动打印(“‘字母’、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->自动打印(真);
$mpdf->Output();
您是否尝试过(代码片段):

学分:

或者,以该文章第二个示例中的代码为例:

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();
require('mpdf.php');
类PDF_AutoPrint扩展了PDF_Javascript{
函数自动打印($dialog=false){
如果($dialog){
$this->_newobj();
$this->n_js=$this->n;
$this->_out('>');
$this->_out('endobj');
$this->_newobj();
$this->_out('>');
$this->_out('endobj');
}
}
}
$mpdf=新的PDF_自动打印(“‘字母’、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->自动打印(真);
$mpdf->Output();

我可以打印生成的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();
$header='documentheader';
$html='您的文档内容在此处';
$footer='打印日期:'。日期('d.m.Y H:i:s')。'
第{PAGENO}页,共{nb}'; $mpdf=新的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();
我将其作为外部文件编写,并通过javascript请求打印

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

我使用DTukans way+添加false作为参数

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


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

非常感谢……我尝试了这两个建议,但仍然没有成功。@mozgras:我可能在周末自己也会玩一玩。正如我所说,这段代码是根据我在网上读到的一些东西改编的,但我自己没有尝试过,所以我会看看我是否能让它工作。这里的关键是使用
$mpdf->->SetJS('this.print();”);
发送输出之前。谢谢!此解决方案是rox,无需添加任何文件,只需一行代码。非常感谢
post_to_url("pdf.export.php", {htmlForPdf:pdf})