40867

FPDF+FPDI autoprint

Question:

I need to import pdf file into fpdf and print it silently. I use fpdi extension to load existing pdf, but I have no idea how to autoprint it.

This is how autoprint in fpdf works - 2 additional classes around fpdf (from their examples).

require('lib/fpdf/fpdf.php'); require('lib/fpdi-1.6.1/fpdi.php'); class PDF_JavaScript extends FPDF { protected $javascript; protected $n_js; function IncludeJS($script, $isUTF8=false) { if(!$isUTF8) $script=utf8_encode($script); $this->javascript=$script; } function _putjavascript() { $this->_newobj(); $this->n_js=$this->n; $this->_put('<<'); $this->_put('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]'); $this->_put('>>'); $this->_put('endobj'); $this->_newobj(); $this->_put('<<'); $this->_put('/S /JavaScript'); $this->_put('/JS '.$this->_textstring($this->javascript)); $this->_put('>>'); $this->_put('endobj'); } function _putresources() { parent::_putresources(); if (!empty($this->javascript)) { $this->_putjavascript(); } } function _putcatalog() { parent::_putcatalog(); if (!empty($this->javascript)) { $this->_put('/Names <</JavaScript '.($this->n_js).' 0 R>>'); } } } class PDF_AutoPrint extends PDF_JavaScript { function AutoPrint($printer='') { // Open the print dialog if($printer) { $printer = str_replace('\\', '\\\\', $printer); $script = "var pp = getPrintParams();"; $script .= "pp.interactive = pp.constants.interactionLevel.full;"; $script .= "pp.printerName = '$printer'"; $script .= "print(pp);"; } else $script = 'print(true);'; $this->IncludeJS($script); } } $pdf = new PDF_AutoPrint(); $pdf->AddPage(); $pdf->SetFont('Arial', '', 20); $pdf->Text(90, 50, 'Print me!'); $pdf->AutoPrint(); $pdf->Output();

And it works fine, but I need it to combine with this:

require('lib/fpdf/fpdf.php'); //require('lib/fpdi-2.0.1/src/autoload.php'); require('lib/fpdi-1.6.1/fpdi.php'); $pdf = new FPDI(); $pdf->setSourceFile("BCEB_20171207_100_A_1_51.pdf"); $tplIdx = $pdf->importPage(1, '/MediaBox'); $pdf->addPage(); $pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); $pdf->Output();

I really have no idea how to combine both (with autoload classes aroud fpdi?). Alternative that would help - another way to load ready pdf into fpdf and output it.

Answer1:

based on <a href="https://stackoverflow.com/questions/33254679/print-pdf-in-firefox" rel="nofollow">Print PDF in Firefox</a>

<?php require('lib/fpdf/fpdf.php'); require('lib/fpdi-1.6.1/fpdi.php'); class PDF_JavaScript extends FPDI { 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) { //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) { $script = "document.contentWindow.print();"; $this->IncludeJS($script); } } $pdf=new PDF_AutoPrint(); $pageCount = $pdf->setSourceFile("BCEB_20171207_28_A_1_04_long.pdf"); // $pageCount = $pdf -> setSourceFile("BCEB_20171207_100_A_1_51.pdf"); //Open the print dialog //$tplIdx = $pdf->importPage(1, '/MediaBox'); //this you need to do on every page for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) { $tplIdx = $pdf->importPage($pageNo); $pdf->addPage(); $pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); } $pdf->AutoPrint(true); $pdf->Output(); ?>

Recommend

  • Problems using PHP SoapClient to pass an encrypted value to a .Net SOAP service
  • Regex to strip out everything but words and numbers (and latin chars)
  • Number of variables doesn't match number of parameters - Yes they do
  • Installed module is empty
  • How to use RequestBodyAdvice
  • debug library loaded with ctypes using gdb
  • JQuery Internet Explorer and ajaxstop
  • Very simple C++ DLL that can be called from .net
  • Why does access(2) check for real and not effective UID?
  • Is there a javascript serializer for JSON.Net?
  • Array.prototype.includes - not transformed with babel
  • Fill an image in a square container while keeping aspect ratio
  • Where to put my custom functions in Wordpress?
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • How to limit post in wp_query
  • Websockets service method fails during R startup
  • bootstrap to use multiple ng-app
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to set the response of a form post action to a iframe source?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Running Map reduces the dimensions of the matrices
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?