2011-08-24 18 views
9

Sto usando una classe php, mpdf, che genera molto bene i PDF. Sto provando a far stampare automaticamente il file (ad es., Apri la finestra di dialogo di stampa) quando viene eseguito il rendering. Ho esteso il funzionamento principale con il codice qui sotto per aggiungere javascript al pdf. Il pdf è reso ma senza auto-stampa. Qualsiasi aiuto sarebbe grande. Grazie!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(); 

risposta

5

Hai provato (frammento):

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

credito: Create an Auto-Print PDF

Oppure, prendendo il codice dal secondo esempio in quell'articolo:

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

Grazie mille ... Ho provato entrambi suggerimenti, ma ancora nessuna fortuna però. – mozgras

+0

@mozgras: Potrei avere un gioco con questo me stesso durante il fine settimana. Come ho detto, questo codice è stato adattato da qualcosa che ho letto online, ma non avevo provato me stesso, quindi vedrò se –

16

questo funziona per me per stampare il file PDF generato, l'ho usato per stampare il contenuto della pagina del sito web senza menu, banner ecc. basta contenere t con propria intestazione e 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(); 
+0

Questo funziona perfettamente per me – compcentral

+0

La chiave qui sta usando '$ mpdf-> SetJS ('this.print();');' prima di inviare l'output. Grazie! –

+0

Questa soluzione non è necessaria per aggiungere qualsiasi file, solo una riga di codice. Grazie v molto – koshin

0

Io uso modo DTukans + aggiunti falsa come parametro.

Opere in Firefox e IE - non ha funzionato per Chrome :(

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