2011-08-24 39 views
9

Estoy usando una clase php, mpdf, que genera archivos PDF muy bien. Estoy intentando que el archivo se imprima automáticamente (es decir, abra el cuadro de diálogo de impresión) cuando se represente. Amplié el funcionamiento del núcleo con el siguiente código para agregar Javascript al pdf. El pdf se muestra pero sin autoimpresión. Cualquier ayuda sería genial. ¡Gracias!mPDF auto print issue

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

Respuesta

5

Ha intentado (fragmento):

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

Crédito: Create an Auto-Print PDF

O, tomando el código del segundo ejemplo en ese artículo:

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

Muchas gracias ... He intentado tanto sugerencias, pero aún no hay suerte. – mozgras

+0

@mozgras: Tal vez tenga un juego con esto el fin de semana. Como dije, este código fue adaptado de algo que leí en línea, pero no lo había intentado, así que veré si Puedo hacer que funcione. –

16

Estos trabajos para mí, para imprimir el archivo PDF generado, lo usé para imprimir los contenidos de la página web sin menús, pancartas, etc. t con propio encabezado y pie de página

$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

Esto funciona genial para mí – compcentral

+0

La clave aquí es usar '$ mpdf-> SetJS ('this.print();');' antes de enviar la salida. ¡Gracias! –

+0

Esta solución rox, no es necesario para agregar cualquier archivo, solo una línea de código. Gracias v mucho más – koshin

0

I utilizar manera de DTukans + Añadido falsa como un parámetro.

funciona en Firefox e IE - no funcionó para el cromo :(

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