2011-08-24 58 views
9

我正在使用一個php類,mpdf,它非常好地生成PDF。我試圖讓文件在渲染時自動打印(即打開打印對話框)。我已經使用下面的代碼擴展了核心功能,以便將pdf添加到PDF中。 pdf已呈現,但沒有自動打印。任何幫助都會很棒。謝謝!mPDF自動打印問題

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

回答

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

非常感謝......我都嘗試建議,但仍然沒有運氣 – mozgras

+0

@mozgras:我可能會在週末玩這個遊戲,正如我所說,這段代碼是根據我在網上閱讀的內容改編的,但沒有嘗試過,所以我會看看是否我可以讓它工作 –

16

該作品對於我打印生成的PDF文件,我用它來打印網站頁面內容沒有菜單,橫幅等只是conten t與自己的頁眉和頁腳

$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(); 
+0

這對我很好用 – compcentral

+0

這裏的關鍵是使用'$ mpdf-> SetJS('this.print();');'在發送輸出之前。謝謝! –

+0

這個解決方案rox,不需要添加任何文件,只需一行代碼。謝謝v很多 – koshin

0

我用DTukans方式+添加假作爲參數。

在Firefox和IE - 鉻:(

$ mpdf-沒有工作> SetJS( 'this.print(假);');