JavaScript support

This extension allows to embed some JavaScript inside the PDF


Author: Johannes Güntert
License: FPDF


This extension allows to embed some JavaScript inside the PDF. The code is executed on document opening.
The Acrobat JavaScript reference is available here.


<?php require('fpdf.php'); class PDF_JavaScript extends FPDF { 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>>'); } } } ?>


This example shows how to start printing as soon as the document opens. It is possible to open the print dialog (pass true to the AutoPrint() method), or directly print with default parameters (pass false).
<?php require('pdf_js.php'); class PDF_AutoPrint extends PDF_JavaScript { function AutoPrint($dialog=false) { //Open the print dialog or start printing immediately on the standard printer $param=($dialog ? 'true' : 'false'); $script="print($param);"; $this->IncludeJS($script); } function AutoPrintToPrinter($server, $printer, $dialog=false) { //Print on a shared printer (requires at least Acrobat 6) $script = "var pp = getPrintParams();"; if($dialog) $script .= "pp.interactive = pp.constants.interactionLevel.full;"; else $script .= "pp.interactive = pp.constants.interactionLevel.automatic;"; $script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';"; $script .= "print(pp);"; $this->IncludeJS($script); } } $pdf=new PDF_AutoPrint(); $pdf->AddPage(); $pdf->SetFont('Arial', '', 20); $pdf->Text(90, 50, 'Print me!'); //Open the print dialog $pdf->AutoPrint(true); $pdf->Output(); ?>
View the result here.



Quelle, zuletzt abgerufen 02.10.2016 05:59