Happy to share it with you and others. In this example, I am using Purchase Order as sample.
1.Download and Save the TCPDF library in the main folder (I renamed it to pdf_library)
2. Create a PHP file in the
(example purchase_order_pdf.php) (not in the hooks folder) with the following codes
Code: Select all
<?php
date_default_timezone_set('Asia/Kuala_Lumpur');
// standard Appgini file headings
$currDir = dirname(__FILE__);
include "$currDir/defaultLang.php";
include "$currDir/language.php";
include "$currDir/lib.php";
// specify the location of the pdf library folder
require 'pdf_library/tcpdf.php';
// grant access to all users who have access to the purchase_order table
$purchase_order_from = get_sql_from('purchase_order');
if (!$purchase_order_from) {
exit(error_message('Access denied!', false));
}
// check if purchase_order_id valid or not
$purchase_order_id = intval($_REQUEST['id']);
if (!$purchase_order_id) {
exit(error_message('Invalid Purchase Order ID!', false));
}
// START DEFINING PURCHASE ORDER SECTIONS
class myPDF extends TCPDF {
// START OF PURCHASE ORDER TOP SECTION >>>>>
public function purchase_order_top() {
// set color for background
$this->SetFillColor(224, 224, 224);
// retrieve purchase_order id
$purchase_order_id = intval($_REQUEST['id']);
// retrieve purchase_order info
$res = sql("SELECT `purchase_order`.*, `supplier`.* FROM `purchase_order` LEFT JOIN `supplier` ON `purchase_order`.`supplier_id` = `supplier`.`id` WHERE `purchase_order`.`id` = {$purchase_order_id}", $eo);
$purchase_order = db_fetch_assoc($res);
$this->Image('images/logo.png', 10, 10, 48, 0, '', '', '', true, 300, 'L');
$this->SetFont('', 'B', 10);
$this->Cell(180, 0, 'Your Company Name', 0, 0, 'R');
$this->Ln();
$this->SetFont('', '', 8);
$this->Cell(180, 0, 'Address Line 1', 0, 0, 'R');
$this->Ln();
$this->Cell(180, 0, 'Address Line 2', 0, 0, 'R');
$this->Ln();
$this->Cell(120, 0, '', 0, 0, 'L');
$this->Cell(60, 0, 'Postcode, City', 0, 0, 'R');
$this->Ln();
$this->Cell(120, 0, '', 0, 0, 'L');
$this->Cell(60, 0, 'State, Country', 0, 0, 'R');
$this->Ln();
$this->SetFont('', 'I', 9);
$this->Cell(120, 0, 'Supplier or Customer Info', 0, 0, 'L');
$this->Ln(8);
$this->SetFont('', 'B', 9);
$this->Cell(120, 0, $purchase_order['supplier_name'], 0, 0, 'L');
$this->SetFont('', 'B', 12);
$this->Cell(60, 0, ' PURCHASE ORDER ', 1, 0, 'J', 1);
$this->Ln(7);
$this->SetFont('', '', 8);
$this->Cell(120, 0, $purchase_order['address_1'], 0, 0, 'L');
$this->Cell(40, 0, 'Purchase Order No : ', 0, 0, 'R');
$this->SetFont('', 'B');
$this->Cell(20, 0, $purchase_order_id, 0, 0, 'R');
$this->Ln();
$this->SetFont('', '', 8);
$this->Cell(120, 0, $purchase_order['address_2'], 0, 0, 'L');
$this->Cell(40, 0, 'Date : ', 0, 0, 'R');
$this->Cell(20, 0, date('d-m-Y', strtotime($purchase_order['date'])), 0, 0, 'R');
$this->Ln();
$this->Cell(120, 0, $purchase_order['postcode'] . ', ' . $purchase_order['city'], 0, 0, 'L');
$this->Cell(40, 0, '', 0, 0, 'R');
$this->Cell(20, 0, '', 0, 0, 'R');
$this->Ln();
$this->Cell(120, 0, $purchase_order['state'] . ', ' . $purchase_order['country'], 0, 0, 'L');
$this->Cell(40, 0, '', 0, 0, 'R');
$this->Cell(20, 0, '', 0, 0, 'R');
$this->Ln(9);
}
// END OF PURCHASE ORDER TOP SECTION <<<<<<<
// START OF PURCHASE ORDER ITEMS SECTION >>>>>>>
public function purchase_orderItems() {
// create some HTML content
$html = '
<table border="1" cellspacing="0" cellpadding="4">
<tr>
<th style="width:30px" align="center">#</th>
<th style="width:270px" align="left">Description</th>
<th style="width:60px" align="right">Unit Price</th>
<th style="width:50px" align="right">Quantity</th>
<th style="width:30px" align="right">UOM</th>
<th style="width:70px" align="right">Line Total</th>
</tr>
';
// retrieve purchase_order_id
$purchase_order_id = intval($_REQUEST['id']);
// retrieve purchase_order_item info
$counter = 0;
$sub_total = 0;
$item = array();
$res = sql("SELECT *, `purchase_order_item`.`product_description` AS `description_1` FROM `purchase_order_item` LEFT JOIN `product` ON `purchase_order_item`.`product_id` = `product`.`id` WHERE `purchase_order_id` = {$purchase_order_id} ORDER BY `position` ASC", $eo);
while ($row = db_fetch_assoc($res)) {
$item[] = $row;
$html .= '
<tr>
<td style="width:30px" align="center">' . ($counter + 1) . '</td>
<td style="width:270px" align="left">' . '<strong>' . $item[$counter]['product_name'] . '</strong><br>' . nl2br($item[$counter]['description_1']) . '</td>
<td style="width:60px" align="right">' . $item[$counter]['unit_price'] . '</td>
<td style="width:50px" align="right">' . $item[$counter]['quantity'] . '</td>
<td style="width:30px" align="right">' . $item[$counter]['uom'] . '</td>
<td style="width:70px" align="right">' . $item[$counter]['line_total'] . '</td>
</tr>
';
$counter++;
$sub_total += $row['line_total'];
}
// retrieve purchase_order info
$res = sql("SELECT * FROM `purchase_order` WHERE `purchase_order`.`id` = {$purchase_order_id}", $eo);
$purchase_order = db_fetch_assoc($res);
// purchase_order subtotal, tax and total
$html .= '
<tr>
<td colspan="5" align="right" style="font-weight:bold">SUBTOTAL</td>
<td style="width:70px" align="right" style="font-weight:bold">' . number_format($sub_total, 2, '.', '') . '</td>
</tr>
<tr>
<td colspan="5" align="right" style="font-weight:bold">TAX</td>
<td style="width:70px" align="right" style="font-weight:bold">' . number_format($sub_total * $purchase_order['tax'] / 100, 2, '.', '') . '</td>
</tr>
<tr>
<td colspan="5" align="right" style="font-weight:bold" > (MYR) TOTAL</td>
<td style="width:70px" align="right" style="font-weight:bold">' . number_format($sub_total + ($sub_total * $purchase_order['tax'] / 100), 2, '.', '') . '</td>
</tr>
</table>
<style>
table {
border-collapse:collapse;
}
th,td {
border:1px solid #CCCCCC;
}
table tr th {
border:1px solid #888;
background-color:#CCCCCC;
color:#000;
font-weight:bold;
}
</style>
';
// output the items HTML content
$this->writeHTML($html, true, false, false, false, '');
// print purchase_order remark if have data
if (($purchase_order['purchase_order_message'])) {
$html = '
<p><strong>Remarks :</strong></p>
<p>' . nl2br($purchase_order['purchase_order_message']) . '</p>
';
// output the HTML content
$this->writeHTML($html, true, false, false, false, '');
}
$this->Ln(15);
// ending caption
$this->Cell(180, 5, '------------- Thank you and have a nice day -------------', 0, 0, 'C');
}
// END OF PURCHASE ORDER ITEMS SECTION <<<<<<<
// START OF FOOTER SECTION >>>>>>>
public function Footer() {
// print the page number
$this->SetY(-15);
$this->Cell(0, 10, 'Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, 0, 'C');
}
// END OF FOOTER SECTION <<<<<<<
}
// GENERATING THE PDF SECTION
$pdf = new myPDF();
// remove default Header and Footer
$pdf->setPrintHeader(false);
// set header and footer fonts
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP - 10, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
// set the page (paper) layout and size
$pdf->AddPage('P', 'A4', '0');
// get the content (calling functions)
$pdf->purchase_order_top();
$pdf->purchase_orderItems();
// set the filename for saving
$filename = date("Ymd_His") . '_purchase_order_' . $purchase_order_id . '.pdf';
// output in specific folder & on screen
// $pdf->Output($_SERVER['DOCUMENT_ROOT'] . 'FOLDER_NAME/'. $filename, 'FI');
// output on screen
$pdf->Output($filename, 'I');
Notes : You may need to change the tablename and fieldnames according to your environment and also the format or layout of your pdf document.
3. Add the button to open the file (in my case, i put it in purchase order DV using Appgini Helper js library)
Hope this will help you and others as well.
Cheers and Happy New Year.