2 namespace Utility\Service;
8 #-> Fix tcpdf bad configuration.
9 define('K_TCPDF_EXTERNAL_CONFIG', true);
10 $_SERVER['DOCUMENT_ROOT'] = '/var/www/';
11 $kPathMain = getcwd() . '/vendor/tecnickcom/tcpdf/';
12 define('K_PATH_MAIN', $kPathMain);
13 $kPathUrl = $kPathMain;
14 if (isset($_SERVER['HTTP_HOST']) AND (!empty($_SERVER['HTTP_HOST']))) {
15 if (isset($_SERVER['HTTPS']) AND (!empty($_SERVER['HTTPS'])) AND strtolower($_SERVER['HTTPS'])!='off') {
16 $kPathUrl = 'https://';
18 $kPathUrl = 'http://';
20 $kPathUrl .= $_SERVER['HTTP_HOST'];
21 $kPathUrl .= str_replace('\\', '/', substr(K_PATH_MAIN, (strlen($_SERVER['DOCUMENT_ROOT']) - 1)));
23 define('K_PATH_URL', $kPathUrl);
24 define('K_PATH_FONTS', K_PATH_MAIN.'fonts/');
25 define('K_PATH_CACHE', K_PATH_MAIN.'cache/');
26 define('K_PATH_URL_CACHE', K_PATH_URL.'cache/');
27 define('K_PATH_IMAGES', K_PATH_MAIN.'images/');
28 define('K_BLANK_IMAGE', K_PATH_IMAGES.'_blank.png');
29 define('PDF_PAGE_FORMAT', 'A4');
30 define('PDF_PAGE_ORIENTATION', 'P');
31 define('PDF_CREATOR', 'HTML2PDF - TCPDF');
32 define('PDF_AUTHOR', 'HTML2PDF - TCPDF');
33 define('PDF_HEADER_TITLE', null);
34 define('PDF_HEADER_STRING', null);
35 define('PDF_HEADER_LOGO', null);
36 define('PDF_HEADER_LOGO_WIDTH', null);
37 define('PDF_UNIT', 'mm');
38 define('PDF_MARGIN_HEADER', 0);
39 define('PDF_MARGIN_FOOTER', 0);
40 define('PDF_MARGIN_TOP', 0);
41 define('PDF_MARGIN_BOTTOM', 0);
42 define('PDF_MARGIN_LEFT', 0);
43 define('PDF_MARGIN_RIGHT', 0);
44 define('PDF_FONT_NAME_MAIN', 'helvetica');
45 define('PDF_FONT_SIZE_MAIN', 10);
46 define('PDF_FONT_NAME_DATA', 'helvetica');
47 define('PDF_FONT_SIZE_DATA', 8);
48 define('PDF_FONT_MONOSPACED', 'courier');
49 define('PDF_IMAGE_SCALE_RATIO', 1);
50 define('HEAD_MAGNIFICATION', 1);
51 define('K_CELL_HEIGHT_RATIO', 1);
52 define('K_TITLE_MAGNIFICATION', 1);
53 define('K_SMALL_RATIO', 2/3);
54 define('K_THAI_TOPCHARS', true);
55 define('K_TCPDF_CALLS_IN_HTML', false);
61 * Base reporting functionality.
62 * @author andre.fourie
64 abstract class PdfTemplate
68 * @var \Doctrine\ORM\EntityManager
75 protected $_title = null;
80 protected $_logo = '<h1 style="font-size:24px; margin:0 auto; text-align:center">EVALUATION SHEET</h1>
81 <div style="width:100%"></div>';
86 private $_html = array();
88 * Current page number.
96 protected $_options = array();
101 protected $_input = array();
104 /* ---------------------------------------------------------------------- *\
106 \* ---------------------------------------------------------------------- */
108 * Process standard format request.
109 * @param array $input
110 * @param array|object $options
112 public function process(array $input, $options)
114 $this->em = \Utility\Registry::getEntityManager();
115 $this->_input = $input;
116 $this->_options = $options;
124 public function build() {}
127 /* ---------------------------------------------------------------------- *\
128 * Construction utilities.
129 \* ---------------------------------------------------------------------- */
132 * @return \Utility\Service\PdfTemplate
134 public function nextPage()
141 * Append HTML to template.
142 * @param string $html
143 * @return \Utility\Service\PdfTemplate
145 public function append($html)
147 if (!isset($this->_html[$this->_page]))
149 $this->_html[$this->_page] = '';
151 $this->_html[$this->_page] .= $html;
156 * Retrieve new PDF data table.
157 * @param array $cellWidths
158 * @param string $tableStyle
159 * @param string $rowStyle
160 * @param string $cellStyle
161 * @return \Utility\Service\PdfElementTable
163 public function newTable(array $cellWidths, $tableStyle, $rowStyle, $cellStyle)
165 return new PdfElementTable($cellWidths, $tableStyle, $rowStyle, $cellStyle);
169 * Retrieve new PDF data table.
170 * Setup new signature section for PDF template.
171 * @param string|null $cellStyle
172 * @return \Utility\Service\PdfElementSignature
174 public function newSignatureLine($cellStyle = null)
176 return new PdfElementSignature($cellStyle);
179 /* ---------------------------------------------------------------------- *\
180 * Specifically cater for PDF Template requirements.
181 \* ---------------------------------------------------------------------- */
183 * Retrieve PDF title.
186 public function getTitle()
188 return $this->_title;
194 * Retrieve PDF content.
197 public function getHtml()
201 //$logo = '<img src="' . __DIR__ . '/../../../../../public/img/app/logos/logo-pdf.jpg" style="padding:0;margin:0;width:100%;" />';
202 foreach ($this->_html as $page)
204 $html .= '<page style="padding:0;margin:0;width:100%;font-size:12px;">' . $this->_logo . $page . '</page>';
216 /* ---------------------------------------------------------------------- *\
217 * PDF element handlers.
218 \* ---------------------------------------------------------------------- */
221 * Helper utility to build PDF signature line(s).
222 * @author andre.fourie
224 class PdfElementSignature
234 protected $_cellStyle = 'border-bottom: solid 1px #333;font-size:11px;font-weight:bold;';
239 * Setup new signature section for PDF template.
240 * @param string|null $cellStyle
242 public function __construct($cellStyle = null)
245 && $this->_cellStyle = $cellStyle;
248 public function addSignatureLine($signatureLeft = null, $signatureMiddle = null, $signatureRight = null)
250 $this->_data[] = array(
251 'Left' => $signatureLeft,
253 'Middle' => $signatureMiddle,
255 'Right' => $signatureRight
260 public function addSignatureText($id, $text)
262 $this->_data[$id] = $text;
270 public function publish()
276 $html[] = '<div style="position:absolute;bottom:50px;left:0;padding:0;margin:0;width:100%;">';
277 $html[] = '<table cellpadding="0px" cellspacing="0px" style="border:0px;width:100%;">';
280 foreach ($this->_data as $rowId => $signatures)
282 if (is_numeric($rowId))
286 foreach ($signatures as $signature)
289 $width = (2 == $i || 4 == $i)
292 if (is_null($signature))
294 $html[] = '<td style="padding:50px 0 0 0;width:' . $width . '"> </td>';
299 && $signature = ' ';
300 $html[] = '<td style="padding:50px 0 0 0;width:' . $width . ';' . $this->_cellStyle. '"> </td>';
306 foreach ($signatures as $signature)
309 $width = (2 == $i || 4 == $i)
312 $value = is_null($signature)
315 $html[] = '<td style="width:' . $width . ';font-size:11px;font-weight:bold;"><b>' . $value . '</b></td>';
322 $html[] = '<td colspan="5" style="padding:50px 0 0 0;width:100%">' . $signatures . '</td>';
328 $html[] = '</table>';
332 return implode("\n", $html);
343 * Helper utility to build PDF data table.
344 * @author andre.fourie
346 class PdfElementTable
353 protected $_titles = array();
357 protected $_columnHeaders;
361 protected $_titleStyle;
365 protected $_titleRowStyle;
369 protected $_tableStyle;
373 protected $_rowStyle;
377 protected $_cellStyle;
381 protected $_cellWidths;
390 * Setup new data table for PDF template.
391 * @param array $cellWidths
392 * @param string $tableStyle
393 * @param string $rowStyle
394 * @param string $cellStyle
396 public function __construct(array $cellWidths, $tableStyle, $rowStyle, $cellStyle)
398 $this->_tableStyle = $tableStyle;
399 $this->_rowStyle = $rowStyle;
400 $this->_cellStyle = $cellStyle;
401 $this->_cellWidths = $cellWidths;
406 * @param string $title
407 * @param string|null $titleCellStyle
408 * @param string|null $titleRowStyle
409 * @return \Utility\Service\PdfElementTable
411 public function addTitle($title, $titleCellStyle = null, $from = null, $to = null)
413 $this->_titles[] = array(
415 'Style' => $titleCellStyle,
424 * @param array $columnHeaders
425 * @param string|null $cellStyle
426 * @return \Utility\Service\PdfElementTable
428 public function setColumnHeaders(array $columnHeaders, array $columnSizes, $cellStyle = null)
430 $this->_columnHeaders = array(
431 'Columns' => $columnHeaders,
432 'Sizes' => $columnSizes,
433 'Style' => $cellStyle
439 * Set value for specific cell.
440 * @param integer $row
441 * @param integer $column
442 * @param unknown $value
443 * @return \Utility\Service\PdfElementTable
445 public function setCellValue($row, $column, $value)
447 $this->_data[$row][$column] = is_null($value) || empty($value)
455 * @param array $dataSet
456 * @return \Utility\Service\PdfElementTable
458 public function setDataSet(array $dataSet)
460 $this->_data = $dataSet;
465 * Add a key => value dataset to the table.
466 * @param array $dataset
467 * @param number $startAtColumn
468 * @param string $keyStyle
469 * @param string $valueStyle
470 * @return \Utility\Service\PdfElementTable
472 public function addKeyValueDataSet(array $dataset, $startAtColumn = 0, $keyStyle = '', $valueStyle = '')
475 $column = $startAtColumn;
476 foreach ($dataset as $key => $value)
478 $this->_data[$row][$column] = '<span style="' . $keyStyle . '">' . $key . '</span>';
479 $this->_data[$row][$column + 1] = '<span style="' . $valueStyle . '">' . $value . '</span>';
489 public function publish()
491 #-> Establish table width.
493 $numColumns = count($this->_cellWidths);
494 foreach ($this->_cellWidths as $columnWidth)
496 $tableWidth += $columnWidth;
503 $html[] = '<table cellpadding="0px" cellspacing="0px" style="width:' . $tableWidth . '%;' . $this->_tableStyle . '">';
506 if (!empty($this->_titles))
510 foreach ($this->_titles as $title)
512 if ($title['From'] > $column)
514 $html[] = '<td colspan="' . ($title['From'] - $column) . '"> </td>';
516 $cellStyle = !is_null($title['Style'])
519 $html[] = '<td colspan="' . ($title['To'] - $title['From'] + 1) . '" style="' . $cellStyle . '">';
520 $html[] = $title['Title'];
522 $column = $title['To'] + 1;
528 if (!is_null($this->_columnHeaders))
530 $cellStyle = !is_null($this->_columnHeaders['Style'])
531 ? $this->_columnHeaders['Style']
534 foreach ($this->_columnHeaders['Columns'] as $key => $title)
536 $width = $this->_columnHeaders['Sizes'][$key];
537 $html[] = '<td style="width:' . $width . '%;' . $cellStyle . '">' . $title . '</td>';
543 foreach ($this->_data as $rowId => $rowData)
545 $html[] = '<tr style="' . $this->_rowStyle. '">';
547 for ($column = 0; $column < $numColumns; $column++)
549 $value = isset($rowData[$column])
552 $width = $this->_cellWidths[$column];
553 $html[] = '<td style="width:' . $width . '%;' . $this->_cellStyle. '">' . $value . '</td>';
559 $html[] = '</table>';
562 return implode("\n", $html);