====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 < '; //Nested table --- >
'. "\n"; //Nested table 482c487 < echo ''; --- > echo ''. "\n"; 484c489 < echo ''; --- > echo ''. "\n"; 503c508,509 < decimalplaces --- > decimalplaces, > appendfile 512a519,521 > // add PDF attachment code > $_POST['ItemPDF'] = $myrow['appendfile']; > // end PDF attachment code 526c535 < echo ''; --- > echo ''. "\n"; 530,531c539,540 < echo ''; < echo ""; --- > echo ''. "\n"; > echo "". "\n"; 535c544,546 < echo ''; --- > echo ''. "\n"; > > echo ''. "\n"; 537c548,584 < echo ''; --- > // Add PDF attachment upload for Items - by Emdx > if ($_POST['ItemPDF']=='' or !isset($_POST['ItemPDF'])){ > $_POST['ItemPDF']="No PDF set"; > } > // Generate selection drop down from pdf_append directory - by emdx, developed with examples from http://au2.php.net/manual/en/function.opendir.php > function select_files($dir, $label = "", $select_name = "", $curr_val = "", $char_length = 60) { > $teller = 0; > if ($handle = opendir($dir)) { > $mydir = ($label != "") ? "\n" : ""; > $mydir .= ""; > } > if ($teller == 0) { > $mydir = "No files!"; > } else { > return $mydir; > } > } > > echo ''. "\n"; > //echo ''. "\n"; > // EOR Add Image upload for New Item - by Emdx 540c587 < echo ''; --- > echo ''. "\n"; 543c590 < echo ''; --- > echo ''. "\n"; 587c634 < echo ''; --- > echo ''. "\n"; 589c636 < echo ''; --- > echo ''. "\n"; 591c638 < echo ''; --- > echo '' . "\n" . ''. "\n"; 607c654 < echo ''; --- > echo '' . _('Note') . ', ' . _('this has no effect if the item is not Controlled') . ''. "\n"; 678c725 < echo ''; --- > echo ''. "\n"; 680c727 < echo ''; --- > echo ''. "\n"; 682c729 < echo ''; --- > echo ''. "\n"; 684c731 < echo '
'. _('Item Code'). ':
'. _('Item Code'). ':
' . _('Item Code') . ':'.$StockID.'
' . _('Item Code') . ':'.$StockID.'
' . _('Item Code') . ':'.$StockID.'
' . _('Item Code') . ':'.$StockID.'
' . _('Part Description') . ' (' . _('short') . '):
' . _('Part Description') . ' (' . _('short') . '):
' . _('Part Description') . ' (' . _('long') . '):
' . _('Part Description') . ' (' . _('long') . '):
' . _('PDF attachment (.pdf)') . ':' . "\n" . select_files("companies/' . $_SESSION['DatabaseName'] .'/pdf_append/", "", "ItemPDF", $_POST['ItemPDF'], "60") . '
' . _('PDF attachment (.pdf) orig') . ': ' . $_POST['ItemPDF'] . '
'. _('Image File (.jpg)') . ':
'. _('Image File (.jpg)') . ':
' . _('Category') . ':
' . _('Category') . ':
' . _('Economic Order Quantity') . ':
' . _('Economic Order Quantity') . ':
' . _('Packaged Volume (metres cubed)') . ':
' . _('Packaged Volume (metres cubed)') . ':
' . _('Packaged Weight (KGs)') . ':
' . _('Packaged Weight (KGs)') . ':
' . _('Units of Measure') . ':
' . _('Units of Measure') . ':
' . _('Make, Buy, Kit, Assembly or Service Part') . ':
' . _('Make, Buy, Kit, Assembly or Service Part') . ':
' . _('Current or Obsolete') . ':
' . _('Current or Obsolete') . ':
' . _('Batch, Serial or Lot Control') . ':
' . _('Batch, Serial or Lot Control') . ':
' . _('Serialised') . ':
' . _('Serialised') . ':' . _('Note') . ', ' . _('this has no effect if the item is not Controlled') . '
' . _('Decimal Places to Display') . ':
' . _('Decimal Places to Display') . ':
' . _('Bar Code') . ':
' . _('Bar Code') . ':
' . _('Discount Category') . ':
' . _('Discount Category') . ':
' . _('Tax Category') . ':
' . _('Tax Category') . ':
' . _('Image') . '
'.$StockImgLink . '
'; --- > echo '
' . _('Image') . '
'.$StockImgLink . '
'. "\n"; 721c768 < echo ''; --- > echo ''. "\n"; %%