2011-08-24 30 views
9

Tôi đang sử dụng lớp php, mpdf, tạo PDF rất độc đáo. Tôi đang cố gắng để tập tin tự động in (tức là, mở hộp thoại in) khi kết xuất. Tôi đã mở rộng chức năng cốt lõi với mã bên dưới để thêm javascript vào pdf. Pdf được hiển thị nhưng không tự động in. Bất kỳ sự trợ giúp nào đều sẽ là tuyệt vời. Cảm ơn!mPDF vấn đề in tự động

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

Trả lời

5

Các bạn đã thử (đoạn):

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

Hoặc, lấy mã từ ví dụ thứ hai trong bài viết rằng:

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

Cảm ơn rất nhiều ... Tôi đã thử cả nhưng tôi vẫn không may mắn, – mozgras

+0

@mozgras: Tôi có thể chơi với bản thân mình vào cuối tuần, như tôi đã nói, đoạn mã này đã được chuyển thể từ thứ tôi đọc trực tuyến, nhưng chưa thử, nên tôi sẽ xem –

16

tác phẩm này để tôi in tệp PDF được tạo, tôi đã sử dụng nó để in nội dung trang web mà không cần menu, biểu ngữ, v.v. t với tiêu đề và chân trang riêng

$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

Điều này làm việc tuyệt vời cho tôi – compcentral

+0

Điều quan trọng ở đây là sử dụng '$ mpdf-> SetJS ('this.print();');' trước khi gửi đầu ra. Cảm ơn! –

+0

Giải pháp này không cần, không cần để thêm bất kỳ tệp nào, chỉ một dòng mã. ery nhiều – koshin

0

tôi sử dụng DTukans cách + thêm sai như một tham số.

trình trong FireFox và IE - không làm việc cho chrome :(

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

Các vấn đề liên quan