Hi All,
(This time no question, but some hints (hopeful useful for others))
=> Create pdf file from Template using FPDF and PDFI, offering for download or opening print dialog in browser.
(thx fhumanes for part of the solution!, extended some examples)
Steps of execution marked in (n)
(1) ButtonEvent/OnServer()
PrintKS($Customer,$Order); // Print procedure call supplying some data fields and writing pdf file
$result["file"] = "somesubfolder/".$Custmer."_".$Order.".pdf";
(3) ButtonEvent/OnAfter()
var popup = Runner.displayPopup( {
url: "pdfprint.php?doc=" + result["file"],
width: 700,
height: 500,
header: 'Print Invoice'
});
(2)(Print function)
After Application initialized:
function PrintKS($Customer,$Order)
{
// Retrieve data
// use FPDI to load template pdf
// fill out the template using SetXY()
// add new pages as required
// *** Save and/or output
// output to file
$filename = $Customer."_".$Order.'.pdf';
$filename= "anyfolder/".$filename;
$pdf->Output($filename,'F');
}
(4) plain php file for printing
pdfprint.php
this follows an example of "import all pages from pdf"
(mot elegant to feed data twice through FPDF/PDFI, but found no other way...)
// Autoprint of pdf file
use setasign\Fpdi\Fpdi;
require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');
//************************************************************************************
//* parameters via method GET *
//************************************************************************************
if(isset($_REQUEST['doc']))
{
$in_doc=$_REQUEST['doc'];
}
else
{
$in_doc = "ksp/100000453316_2.pdf"; // test only default document
}
//************************************************************************************
//* JS stuff *
//************************************************************************************
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($in_doc);
//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();
// use the imported page and adjust the page size
$pdf->useTemplate($tplIdx, ['adjustPageSize' => true]); // same size and orientation
// no additional fill out here
}
$pdf->AutoPrint(true);
$pdf->Output();
?>
=> Final thoughts:
- This opens the Firefox Print Dialog (OK and Close after printing)
- On Chrome this requires one click more (print Icon to open up print dialog)
(maybee there is a "Kiosk mode" parameter to tweak that,; not yet tested
Hope this is useful !
Kind Greetings,
ckranich