====Using FPDI to append third-party PDF documents to webERP system output PDF files====
====22nd Feb 2008 ** note this mod is now standard part of 3.09 **====
25th Jan 08 - updated to reflect changes required from webERP v3.08.
This modification allows an option to append a pre-prepared PDF file, to the dynamically produced Invoice and Credit Note PDF files from webERP. For example, we can now append a //materials handling// sheet, or a //product safety// sheet, or a //terms and conditions// sheet, to an Invoice and Credit Note.
I'm not sure if the document to append should be determined by reference to the customer or perhaps by the items on the invoice. - this implementation has a document selected per customer and it is sent with every customer This implementation actually has a PDF document per stock item, its attached only if the stock item is on the invoice.
An appended PDF file can be either Landscape, or Portrait orientation, the resulting PDF can contain both a Landscape invoice, and a Portrait information sheet.
====Installation====
We require at least FPDF version 1.53. Make sure your includes/fpdf.php file is up-to-date. The current 3.08 weberp package has an older version of FPDF, you need to overwrite with the latest versions.
To retain compatibility with the other PDF documents, make this change to the new fpdf.php file,
line 994, replace:
function Output($name='',$dest='')
with
function Output($name='pdf',$dest='S')
We require at least FPDI version 1.2. Download FPDI package, unzip and move to fpdi directory under webERP installation root.
http://www.fpdf.org/
http://www.setasign.de/products/pdf-php-solutions/fpdi/
%%
ALTER TABLE `stockmaster` ADD `appendfile` varchar(40) NOT NULL ;
%%
The code below will check if a stockitem needs a PDF appended.
%%
diff PrintCustTrans.php.3.08 PrintCustTrans.php.new
6a7,15
> error_reporting (E_ALL);
>
>
>
>
>
>
>
>
44c53,55
<
---
> define('FPDF_FONTPATH','font/');
> require('fpdi/fpdi.php');
>
69a81
>
342a355
>
419,420c432,433
< $FontSize=6;
< $LeftOvers = $pdf->addTextWrap($Left_Margin+300,$YPos-2,245,$FontSize,$_SESSION['RomalpaClause']);
---
> $FontSize=9;
> $LeftOvers = $pdf->addTextWrap($Left_Margin+300,$YPos-4,245,$FontSize,$_SESSION['RomalpaClause']);
422c435
< $YPos -=7;
---
> $YPos-=14;
424a438,442
> /* Add Images for Visa / Mastercard / Paypal */
> $pdf->addJpegFromFile('companies/' . $_SESSION['DatabaseName'] . '/payment.jpg',$Page_Width/2 -280,$YPos-20,0,40);
> $pdf->addText($Page_Width-$Right_Margin-472, $YPos - ($line_height*3)+32,$FontSize, _('Bank Code: *** Bank Account: ***'));
>
>
435,436c453
<
< $pdfcode = $pdf->output();
---
> $pdfcode = $pdf->output("invoice.pdf", "F");
437a455,532
>
>
> // Start FPDI concatination to append PDF files conditionally to the invoice
> // This part taken from FPDI example page
> class concat_pdf extends FPDI {
>
> var $files = array();
>
> function setFiles($files) {
> $this->files = $files;
> }
>
> function concat() {
> foreach($this->files AS $file) {
> $pagecount = $this->setSourceFile($file);
> for ($i = 1; $i <= $pagecount; $i++) {
> $tplidx = $this->ImportPage($i);
> $s = $this->getTemplatesize($tplidx);
> $this->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
> $this->useTemplate($tplidx);
> }
> }
> }
>
> }
>
> $pdf =& new concat_pdf();
> // Have to get the TransNo again, not sure what happens if we have a series of trans nos
> if (isset($_GET['FromTransNo'])){
> $FromTransNo = trim($_GET['FromTransNo']);
> } elseif (isset($_POST['FromTransNo'])){
> $FromTransNo = trim($_POST['FromTransNo']);
> }
> // Check its an Invoice type again, then select appendfile filename
> //if ($InvOrCredit=='Invoice'){
> //$sql = 'SELECT stockmoves.stockid, stockmaster.appendfile
> // FROM stockmoves,
> //stockmaster
> //WHERE stockmoves.stockid = stockmaster.stockid
> //AND stockmoves.type=10
> //AND stockmoves.transno=' . $FromTransNo . '
> //AND stockmoves.show_on_inv_crds=1';
> //};
>
> $sql = 'SELECT stockmoves.stockid, stockmaster.appendfile
> FROM stockmoves,
> stockmaster
> WHERE stockmoves.stockid = stockmaster.stockid
> AND stockmoves.type=10
> AND stockmoves.transno=' . $FromTransNo . '
> AND stockmoves.show_on_inv_crds=1';
>
>
>
>
>
>
>
>
>
>
> $result=DB_query($sql,$db);
> // Loop the result set and add appendfile if the field is not 0
> while ($row=DB_fetch_array($result)){
> if ($row['appendfile'] !='0') {
> $pdf->setFiles(array('invoice.pdf','pdf_append/' . $row['appendfile']));
> $pdf->concat();
> $pdf->Output('newpdf.pdf','I');
> exit;
> // If the appendfile field is empty, just print the invoice without any appended pages
> } else {
> $pdf->setFiles(array('invoice.pdf'));
> $pdf->concat();
> $pdf->Output('newpdf.pdf','D');
> exit;
> }
> }
> //End FPDI Concat
1013c1108
< ?>
\ No newline at end of file
---
> ?>
%%
Then we modify Stocks.php, so that an item can have a PDF file attached to it.
You have to add the PDF files to a directory named "pdf_append" under your root weberp directory.
The drop down list is populated with all the PDF files within that directory. Under Item Maintenance, you can now choose which PDF file to attach, or choose 'none'.
NB I have added several "\n" line breaks to Stocks.php to aid HTML debugging.
%%
diff Stocks.php.3.08 Stocks.php.new
30c30
< echo "" . _('Back to Items') . ' ';
---
> echo "" . _('Back to Items') . ' ' . "\n";
242c242,243
< decimalplaces=" . $_POST['DecimalPlaces'] . "
---
> decimalplaces=" . $_POST['DecimalPlaces'] . ",
> appendfile='" . $_POST['ItemPDF'] . "'
300c301,302
< decimalplaces)
---
> decimalplaces,
> appendfile)
316c318,319
< " . $_POST['DecimalPlaces']. "
---
> " . $_POST['DecimalPlaces']. ",
> '" . $_POST['ItemPDF']. "'
350a354
> unset($_POST['ItemPDF']);
358c362
< echo ' ';
---
> echo ' '. "\n";
464a469
> unset($_POST['ItemPDF']);
475c480
<