Skip to content

Instantly share code, notes, and snippets.

@Llewellynvdm
Last active April 2, 2022 17:52
Show Gist options
  • Save Llewellynvdm/91677f36a06f5278d586bfc25bd5a290 to your computer and use it in GitHub Desktop.
Save Llewellynvdm/91677f36a06f5278d586bfc25bd5a290 to your computer and use it in GitHub Desktop.
pdf table templete
/**
* @return bool
*/
protected function buildPDF()
{
if(class_exists('TCPDF'))
{
$template = $this->getTemplate('pdf.html'); // your function to get the html table
// convert product object to placeholders
$placeholders = array();
foreach ($this->product as $key => $value)
{
$placeholders['[' . $key . ']'] = $value;
}
// update the template
$html = ###Component###Helper::setDynamicData($template, $placeholders); // helper method see below
// now create the PDF
$this->pdf = new ComponentWarrantyTCPDF("P", "mm", "A4", true, 'UTF-8', false);
// remove margins
$this->pdf->SetHeaderMargin(0);
$this->pdf->SetFooterMargin(0);
// remove footer
$this->pdf->setPrintFooter(false);
// set image scale factor
$this->pdf->setImageScale(1.25);
// add the meta data
$this->pdf->SetCreator('Company Name');
$this->pdf->SetAuthor('Company Name');
$this->pdf->SetTitle('Company Name Product Warranty Certificate');
$this->pdf->SetSubject('Company Name Product Warranty Certificate for ' . $this->product->product_name);
$this->pdf->SetKeywords('Company Name, Warranty Certificate');
// add the content to the PDF
$this->pdf->AddPage();
$this->pdf->writeHTML($html, true, false, true, false, '');
// success PDF was build
return true;
}
return false;
}
// Extend the TCPDF class to create background image
class ComponentWarrantyTCPDF extends TCPDF {
//Page header
public function Header() {
// get the current page break margin
$bMargin = $this->getBreakMargin();
// get current auto-page-break mode
$auto_page_break = $this->AutoPageBreak;
// disable auto-page-break
$this->SetAutoPageBreak(false, 0);
// warranty template path
$img_file = JPATH_COMPONENT_SITE .'/assets/images/YOURBACKGROUND.jpg';
$this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
// restore auto-page-break status
$this->SetAutoPageBreak($auto_page_break, $bMargin);
// set the starting point for the page content
$this->setPageMark();
}
}
<table width="800">
<tbody><tr style="line-height: 152px;"><td width="150"></td><td></td></tr>
<tr style="font-size:15px; line-height: 33px; vertical-align:bottom;">
<td></td>
<td>
<b>[full_name]</b>
</td>
</tr>
<tr style="font-size:15px; line-height: 33px; vertical-align:bottom;">
<td></td>
<td>
<b>[phone]</b>
</td>
</tr>
<tr style="font-size:15px; line-height: 33px; vertical-align:bottom;">
<td></td>
<td>
<b>[email]</b>
</td>
</tr>
</tbody></table>
<table width="800">
<tbody><tr style="line-height: 13px;"><td width="150"></td><td></td></tr>
<tr style="font-size:15px; line-height: 32px; vertical-align:bottom;">
<td></td>
<td>
<b>[dealer_name]</b>
</td>
</tr>
<tr style="font-size:15px; line-height: 32px; vertical-align:bottom;">
<td></td>
<td>
<b>[invoice_number]</b>
</td>
</tr>
<tr style="font-size:15px; line-height: 32px; vertical-align:bottom;">
<td></td>
<td>
<b>[invoice_date]</b>
</td>
</tr>
</tbody></table>
<table width="720">
<tbody><tr style="line-height: 28px;"><td width="350"></td><td></td></tr>
<tr style="font-size:15px; line-height:33px; vertical-align:bottom;">
<td></td>
<td>
<b>[product_code]</b>
</td>
</tr>
<tr style="font-size:15px; line-height: 33px; vertical-align:bottom;">
<td></td>
<td>
<b>[serial]</b>
</td>
</tr>
<tr style="font-size:15px; line-height: 33px; vertical-align:bottom;">
<td></td>
<td>
<b>[product_name]</b>
</td>
</tr>
</tbody></table>
<table width="720">
<tbody><tr style="line-height: 20px;"><td width="350"></td><td></td></tr>
<tr style="font-size:15px; line-height:33px; vertical-align:bottom;">
<td></td>
<td>
<b>[warranty_number]</b>
</td>
</tr>
<tr style="font-size:15px; line-height: 33px; vertical-align:bottom;">
<td></td>
<td>
<b>[invoice_date]</b>
</td>
</tr>
<tr style="font-size:15px; line-height: 33px; vertical-align:bottom;">
<td></td>
<td>
<b>[warranty_expire_date]</b>
</td>
</tr>
</tbody></table>
/**
* The Dynamic Data Array
*
* @var array
*/
protected static $dynamicData = array();
/**
* Set the Dynamic Data
*
* @param string $data The data to update
* @param array $placeholders The placeholders to use to update data
*
* @return string of updated data
*
*/
public static function setDynamicData($data, $placeholders)
{
// make sure data is a string & placeholders is an array
if (self::checkString($data) && self::checkArray($placeholders))
{
// store in memory in case it is build multiple times
$keyMD5 = md5($data.json_encode($placeholders));
if (!isset(self::$dynamicData[$keyMD5]))
{
// remove all values that are not strings (just to be safe)
$placeholders = array_filter($placeholders, function ($val){ if (self::checkArray($val) || self::checkObject($val)) { return false; } return true; });
// model (basic) based on logic
self::setTheIF($data, $placeholders);
// update the string and store in memory
self::$dynamicData[$keyMD5] = str_replace(array_keys($placeholders), array_values($placeholders), $data);
}
// return updated string
return self::$dynamicData[$keyMD5];
}
return $data;
}
/**
* Set the IF statements
*
* @param string $string The string to update
* @param array $placeholders The placeholders to use to update string
*
* @return void
*
*/
protected static function setTheIF(&$string, $placeholders)
{
// only normal if endif
$condition = '[a-z0-9\_\-]+';
$inner = '((?:(?!\[\/?IF)(?!\[\/?ELSE)(?!\[\/?ELSEIF).)*?)';
$if = '\[IF\s?('.$condition.')\]';
$elseif = '\[ELSEIF\s?('.$condition.')\]';
$else = '\[ELSE\]';
$endif = '\[ENDIF\]';
// set the patterns
$patterns = array();
// normal if endif
$patterns[] = '#'.$if.$inner.$endif.'#is';
// normal if else endif
$patterns[] = '#'.$if.$inner.$else.$inner.$endif.'#is';
// dynamic if elseif's endif
$patterns[] = '#'.$if.$inner.$elseif.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$endif.'#is';
// dynamic if elseif's else endif
$patterns[] = '#'.$if.$inner.$elseif.$inner.$else.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$else.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$else.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$else.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$else.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$else.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$else.$inner.$endif.'#is';
$patterns[] = '#'.$if.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$elseif.$inner.$else.$inner.$endif.'#is';
// run the patterns to setup the string
foreach ($patterns as $pattern)
{
while (preg_match($pattern, $string, $match))
{
$keep = self::remainderIF($match, $placeholders);
$string = preg_replace($pattern, $keep, $string, 1);
}
}
}
/**
* Set the remainder IF
*
* @param array $match The match search
* @param array $placeholders The placeholders to use to match
*
* @return string of remainder
*
*/
protected static function remainderIF(&$match, &$placeholders)
{
// default we keep nothing
$keep = '';
$found = false;
// get match lenght
$length = count($match);
// ranges to check
$ii = range(2,30,2); // even numbers (content)
$iii = range(1, 25, 2); // odd numbers (placeholder)
// if empty value remove whole line else show line but remove all [CODE]
foreach ($iii as $content => $placeholder)
{
if (isset($match[$placeholder]) && empty($placeholders['['.$match[$placeholder].']']))
{
// keep nothing or next option
$keep = '';
}
elseif (isset($match[$ii[$content]]))
{
$keep = addcslashes($match[$ii[$content]], '$');
$found = true;
break;
}
}
// if not found load else if set
if (!$found && in_array($length, $ii))
{
$keep = addcslashes($match[$length - 1], '$');
}
return $keep;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment