1<?php
2namespace GO\Base\Util;
3
4class Pdf extends Fpdi  {
5
6	public function __construct($orientation = 'P') {
7
8		parent::__construct($orientation,'pt');
9
10		$this->init();
11	}
12
13	public $font = 'freesans';
14	public $font_size=10;
15
16	public $title="";
17	public $subtitle="";
18
19
20	public $style = '
21td.head{
22	font-weight:bold;
23	border-bottom:2px solid #000;
24	font-size:110%;
25	line-height:200%;
26}
27td.total{
28
29border-top:1px solid #000;
30	background-color:#f1f1f1;
31}
32
33td.noborder{
34border-color:white;
35}
36
37td.normal{
38	border-top:1px solid #ccc;
39}
40
41td.group{
42	border-bottom:1px solid #ccc;
43	font-size:14px;
44	line-height:200%;
45}
46
47h2{
48color:#000;
49}
50';
51
52
53	public function getStyle(){
54		return '<style>'.$this->style.'</style>';
55	}
56
57
58
59	/**
60	 * Set this to a closure with the PDF object as only parameter
61	 * @see Header()
62	 * @var Closure(TCPDF)
63	 */
64	private $headerFunction;
65
66	/**
67	 * Set this to a closure with the PDF object as only parameter
68	 * @see Footer()
69	 * @var Closure(TCPDF)
70	 */
71	private $footerFunction;
72
73	public function setHeaderFunction($val) {
74		if(!is_callable($val))
75			throw new Exception('TCPDF header ender function should be callable');
76		$this->headerFunction = $val;
77	}
78
79	public function setFooterFunction($val) {
80		if(!is_callable($val))
81			throw new Exception('TCPDF header ender function should be callable');
82		$this->footerFunction = $val;
83	}
84
85	protected function init() {
86
87		//set image scale factor
88		$this->setImageScale(PDF_IMAGE_SCALE_RATIO);
89
90		$this->SetDrawColor(125,165, 65);
91		$this->SetFillColor(248, 248, 248);
92		$this->SetTextColor(0,0,0);
93
94		$this->getAliasNbPages();
95
96		$this->setJPEGQuality(100);
97		$this->SetMargins(30,60,30);
98
99		if (!empty(\GO::config()->tcpdf_font)) {
100			$this->font = \GO::config()->tcpdf_font;
101		}
102
103		if(!empty(\GO::config()->tcpdf_ttf_font)){
104			$this->font = \TCPDF_FONTS::addTTFfont(\GO::config()->tcpdf_ttf_font);
105			//$this->font=  TCPDF_FONTS::addTTFfont(\GO::config()->tcpdf_ttf_font,'TrueType'); // 2nd parameter is normally autodetected but sometimes this goes wrong.
106		}
107
108		if (!empty(\GO::config()->tcpdf_font_size)) {
109			$this->font_size = \GO::config()->tcpdf_font_size;
110		}
111
112		$this->SetFont($this->font,'',$this->font_size);
113
114		$this->pageWidth =$this->getPageWidth()-$this->lMargin-$this->rMargin;
115
116		$this->SetAutoPageBreak(true, 30);
117
118				// set font
119		$this->SetFont($this->font, '', $this->font_size);
120	}
121
122
123	public function Footer() {
124
125		if(is_callable($this->footerFunction)) {
126			call_user_func($this->footerFunction);
127			return;
128		}
129
130		$this->setDefaultTextColor();
131		$this->SetFont($this->font,'',$this->font_size);
132		$this->SetY(-20);
133		$pW=$this->getPageWidth();
134		$this->Cell($pW/2, 10, \GO::config()->product_name.' '.\GO::config()->version, 0, 0, 'L');
135		$this->Cell(($pW/2), 10, sprintf(\GO::t("Page %s of %s"), $this->getAliasNumPage(), $this->getAliasNbPages()), 0, 0, 'R');
136	}
137
138	public function Header() {
139
140		if(is_callable($this->headerFunction)) {
141			call_user_func($this->headerFunction);
142			return;
143		}
144
145		$this->SetY(10); // DEZE WAS T
146
147//		$this->SetTextColor(50,135,172);
148		$this->SetFont($this->font,'B',16);
149
150		if(!empty($this->title))
151		{
152			$this->Write(16,$this->title);
153		}
154
155		if(!empty($this->subtitle))
156		{
157			$this->SetTextColor(125,162,180);
158			$this->SetFont($this->font,'',12);
159			$this->setXY($this->getX()+5, $this->getY()+3.5);
160			$this->Write(12, $this->subtitle);
161		}
162
163
164		$this->setY($this->getY()+2.5, false);
165
166		$this->SetFont($this->font,'',$this->font_size);
167		$this->setDefaultTextColor();
168
169		$this->Cell($this->getPageWidth()-$this->getX()-$this->rMargin,12,  Date::get_timestamp(time()),0,0,'R');
170
171		if(!empty($_REQUEST['text']))
172		{
173			$this->SetFont($this->font,'',$this->font_size);
174			$this->Ln(20);
175			$this->MultiCell($this->getPageWidth(), 12, $_REQUEST['text']);
176		}
177
178		if(!empty($_REQUEST['html']))
179		{
180			$this->SetFont($this->font,'',$this->font_size);
181			$this->Ln(20);
182
183			$this->writeHTML($_REQUEST['html']);
184		}
185
186		if(empty($_REQUEST['text']) && empty($_REQUEST['html']))
187		{
188			$this->Ln();
189		}
190
191		$this->SetTopMargin($this->getY()+10);
192
193	}
194
195	function calcMultiCellHeight($w, $h, $text)
196	{
197		$text = str_replace("\r",'', $text);
198		$lines = explode("\n",$text);
199		$height = count($lines)*$h;
200
201		foreach($lines as $line)
202		{
203			$width = $this->GetStringWidth($line);
204
205			$extra_lines = ceil($width/$w)-1;
206			$height += $extra_lines*$h;
207		}
208		return $height;
209	}
210
211	function H1($title)
212	{
213		$this->SetFont($this->font,'B',16);
214//		$this->SetTextColor(50,135,172);
215		//$this->Cell($this->getPageWidth()-$this->lMargin-$this->rMargin,20, $title,0,1);
216		$this->Write(20, $title,'', false,'', true);
217//		$this->MultiCell($this->getPageWidth()-$this->lMargin-$this->rMargin,20, $title, 0, 'L', false, '1');
218//		$this->setDefaultTextColor();
219		$this->SetFont($this->font,'',$this->font_size);
220	}
221
222	function H2($title)
223	{
224
225		$this->SetFont($this->font,'',14);
226//		$this->SetTextColor(125,165, 65);
227		$this->SetTextColor(50,135,172);
228		//$this->Cell($this->getPageWidth()-$this->lMargin-$this->rMargin,24, $title,0,1);
229
230		$this->Write(24, $title,'', false,'', true);
231//		$this->MultiCell($this->getPageWidth()-$this->lMargin-$this->rMargin,24, $title, 0, 'L', false, '1');
232		$this->setDefaultTextColor();
233		$this->SetFont($this->font,'',$this->font_size);
234	}
235
236	function H3($title)
237	{
238//		$this->SetTextColor(102,102, 102);
239		$this->SetFont($this->font,'B',11);
240//		$this->Cell($this->getPageWidth()-$this->lMargin-$this->rMargin,14, $title,'',1);
241		$this->Write(14, $title,'', false,'', true);
242//		$this->MultiCell($this->getPageWidth()-$this->lMargin-$this->rMargin,14, $title, '', 'L', false, '1');
243		$this->SetFont($this->font,'',$this->font_size);
244//		$this->setDefaultTextColor();
245		$this->ln(4);
246	}
247
248	function H4($title)
249	{
250		$this->SetFont($this->font,'B',$this->font_size);
251		//	$this->SetDrawColor(90, 90, 90);
252		//$this->SetDrawColor(128, 128, 128);
253
254//		$this->Cell($this->getPageWidth()-$this->lMargin-$this->rMargin,14, $title,'',1);
255		$this->Write(14, $title,'', false,'', true);
256//		$this->MultiCell($this->getPageWidth()-$this->lMargin-$this->rMargin,14, $title, '', 'L', false, '1');
257
258		//$this->SetDrawColor(0,0,0);
259		$this->SetFont($this->font,'',$this->font_size);
260
261
262	}
263
264	private $_headers;
265
266	public function tableHeaders($columns){
267		$this->_headers=$columns;
268
269		return $this->tableRow($columns);
270	}
271
272
273	public function tableRow($columns){
274		$html = '<tr style="border-top:1px solid black">';
275
276		$headerIndex=0;
277		for($i=0;$i<count($columns);$i++){
278
279			if(isset($this->_headers[$headerIndex])){
280				if(!isset($columns[$i]->width))
281					$columns[$i]->width=$this->_headers[$headerIndex]->width;
282
283				if(!isset($columns[$i]->align))
284					$columns[$i]->align=$this->_headers[$headerIndex]->align;
285
286				$headerIndex++;
287				if(isset($columns[$i]->colspan)){
288					for($n=1;$n<$columns[$i]->colspan;$n++){
289						if(isset($this->_headers[$headerIndex]))
290							$columns[$i]->width+=$this->_headers[$headerIndex]->width;
291
292						$headerIndex++;
293					}
294				}
295			}
296
297			$html .= $columns[$i]->render();
298		}
299
300		$html .= '</tr>';
301
302		return $html;
303	}
304
305
306	function setDefaultTextColor()
307	{
308		$this->SetTextColor(40,40,40);
309	}
310
311
312}
313
314
315class PdfTableColumn{
316
317	public $width;
318	public $text="";
319	public $align;
320	public $bgcolor;
321	public $colspan;
322	public $class;
323	public $extraStyle="";
324
325	public $isHeader=false;
326
327	public function __construct($config) {
328
329		foreach($config as $prop=>$value)
330			$this->$prop = $value;
331
332//		if($this->isHeader && !isset($this->bgcolor)){
333//			$this->bgcolor='rgb(248, 248, 248)';
334//		}
335	}
336
337	public function render(){
338
339		$tag = $this->isHeader ? 'th' : 'td';
340
341
342
343		$html = '<'.$tag.' style="';
344
345		if(isset($this->width))
346			$html .='width:'.$this->width.'px;';
347
348		if(isset($this->bgcolor))
349			$html .='background-color:'.$this->bgcolor.';';
350
351		if(isset($this->align))
352			$html .='text-align:'.$this->align.';';
353
354		if(isset($this->color))
355			$html .='color:'.$this->color.';';
356
357
358
359		$html .= $this->extraStyle.'"';
360
361		if(isset($this->colspan))
362			$html .= ' colspan="'.$this->colspan.'"';
363
364		if(!isset($this->class))
365			$this->class="normal";
366
367		$html .=' class="'.$this->class.'"';
368
369		$html .='>'.$this->text.'</'.$tag.'>';
370
371		return $html;
372	}
373
374}
375