1: <?php
2:
3: AlphaAutoLoader::loadLib('tcpdf.php');
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: class AlphaTCPDF extends TCPDF {
50: 51: 52: 53: 54: 55:
56: private static $logger = null;
57:
58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77:
78: public function Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0) {
79: if(self::$logger == null)
80: self::$logger = new Logger('AlphaTCPDF');
81:
82: global $config;
83:
84: self::$logger->debug('Processing image file URL ['.$file.']');
85:
86: try {
87: if(strpos($file, '/tk/') !== false) {
88: $start = strpos($file, '/tk/')+3;
89: $end = strlen($file);
90: $tk = substr($file, $start+1, $end-($start+1));
91: $decoded = FrontController::getDecodeQueryParams($tk);
92:
93: parent::Image($decoded['s'], $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
94: }else {
95:
96: if(AlphaValidator::isURL($file))
97: parent::Image($config->get('app.root').'/'.Image::convertImageURLToPath($file), $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
98: else
99: parent::Image($file, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
100: }
101: }catch(Exception $e) {
102: self::$logger->error('Error processing image file URL ['.$file.'], error ['.$e->getMessage().']');
103: throw $e;
104: }
105: }
106:
107: 108: 109: 110: 111: 112: 113: 114:
115: protected function openHTMLTagHandler(&$dom, $key, $cell=false) {
116: $tag = $dom[$key];
117: $parent = $dom[($dom[$key]['parent'])];
118: $firstorlast = ($key == 1);
119:
120: if (isset($tag['attribute']['dir'])) {
121: $this->tmprtl = $tag['attribute']['dir'] == 'rtl' ? 'R' : 'L';
122: } else {
123: $this->tmprtl = false;
124: }
125:
126: switch($tag['value']) {
127: case 'table': {
128: $cp = 0;
129: $cs = 0;
130: $dom[$key]['rowspans'] = array();
131: if (!$this->empty_string($dom[$key]['thead'])) {
132:
133: $this->thead = $dom[$key]['thead'];
134: }
135: if (isset($tag['attribute']['cellpadding'])) {
136: $cp = $this->getHTMLUnitToUnits($tag['attribute']['cellpadding'], 1, 'px');
137: $this->oldcMargin = $this->cMargin;
138: $this->cMargin = $cp;
139: }
140: if (isset($tag['attribute']['cellspacing'])) {
141: $cs = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
142: }
143: $this->checkPageBreak((2 * $cp) + (2 * $cs) + $this->lasth);
144: break;
145: }
146: case 'tr': {
147:
148: $dom[$key]['cellpos'] = array();
149: break;
150: }
151: case 'hr': {
152: $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
153: $this->htmlvspace = 0;
154: $wtmp = $this->w - $this->lMargin - $this->rMargin;
155: if ((isset($tag['attribute']['width'])) AND ($tag['attribute']['width'] != '')) {
156: $hrWidth = $this->getHTMLUnitToUnits($tag['attribute']['width'], $wtmp, 'px');
157: } else {
158: $hrWidth = $wtmp;
159: }
160: $x = $this->GetX();
161: $y = $this->GetY();
162: $prevlinewidth = $this->GetLineWidth();
163: $this->Line($x, $y, $x + $hrWidth, $y);
164: $this->SetLineWidth($prevlinewidth);
165: $this->addHTMLVertSpace(1, $cell, '', !isset($dom[($key + 1)]), $tag['value'], false);
166: break;
167: }
168: case 'a': {
169: if (array_key_exists('href', $tag['attribute'])) {
170: $this->HREF['url'] = $tag['attribute']['href'];
171: }
172: $this->HREF['color'] = $this->htmlLinkColorArray;
173: $this->HREF['style'] = $this->htmlLinkFontStyle;
174: if (array_key_exists('style', $tag['attribute'])) {
175:
176: preg_match_all('/([^;:\s]*):([^;]*)/', $tag['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
177: $astyle = array();
178: while (list($id, $name) = each($style_array[1])) {
179: $name = strtolower($name);
180: $astyle[$name] = trim($style_array[2][$id]);
181: }
182: if (isset($astyle['color'])) {
183: $this->HREF['color'] = $this->convertHTMLColorToDec($astyle['color']);
184: }
185: if (isset($astyle['text-decoration'])) {
186: $this->HREF['style'] = '';
187: $decors = explode(' ', strtolower($astyle['text-decoration']));
188: foreach ($decors as $dec) {
189: $dec = trim($dec);
190: if (!$this->empty_string($dec)) {
191: if ($dec{0} == 'u') {
192: $this->HREF['style'] .= 'U';
193: } elseif ($dec{0} == 'l') {
194: $this->HREF['style'] .= 'D';
195: }
196: }
197: }
198: }
199: }
200: break;
201: }
202: case 'img': {
203: if (isset($tag['attribute']['src'])) {
204:
205: if ($tag['attribute']['src'][0] == '/') {
206: $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
207: }
208:
209: $tag['attribute']['src'] = urldecode($tag['attribute']['src']);
210:
211: if (!isset($tag['attribute']['width'])) {
212: $tag['attribute']['width'] = 0;
213: }
214: if (!isset($tag['attribute']['height'])) {
215: $tag['attribute']['height'] = 0;
216: }
217:
218: $tag['attribute']['align'] = 'bottom';
219:
220: switch($tag['attribute']['align']) {
221: case 'top': {
222: $align = 'T';
223: break;
224: }
225: case 'middle': {
226: $align = 'M';
227: break;
228: }
229: case 'bottom': {
230: $align = 'B';
231: break;
232: }
233: default: {
234: $align = 'B';
235: break;
236: }
237: }
238: $fileinfo = pathinfo($tag['attribute']['src']);
239: if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
240: $type = strtolower($fileinfo['extension']);
241: }else{
242: $type = '';
243: }
244: $prevy = $this->y;
245: $xpos = $this->GetX();
246: if (isset($dom[($key - 1)]) AND ($dom[($key - 1)]['value'] == ' ')) {
247: if ($this->rtl) {
248: $xpos += $this->GetStringWidth(' ');
249: } else {
250: $xpos -= $this->GetStringWidth(' ');
251: }
252: }
253: $imglink = '';
254: if (isset($this->HREF['url']) AND !$this->empty_string($this->HREF['url'])) {
255: $imglink = $this->HREF['url'];
256: if ($imglink{0} == '#') {
257:
258: $page = intval(substr($imglink, 1));
259: $imglink = $this->AddLink();
260: $this->SetLink($imglink, 0, $page);
261: }
262: }
263: $border = 0;
264: if (isset($tag['attribute']['border']) AND !empty($tag['attribute']['border'])) {
265:
266: $border = $tag['attribute']['border'];
267: }
268: if (isset($tag['attribute']['width'])) {
269: $iw = $this->getHTMLUnitToUnits($tag['attribute']['width'], 1, 'px', false);
270: }
271: if (isset($tag['attribute']['height'])) {
272: $ih = $this->getHTMLUnitToUnits($tag['attribute']['height'], 1, 'px', false);
273: }
274: if (($type == 'eps') OR ($type == 'ai')) {
275: $this->ImageEps($tag['attribute']['src'], $xpos, $this->GetY(), $iw, $ih, $imglink, true, $align, '', $border);
276: } else {
277: $this->Image($tag['attribute']['src'], $xpos, $this->GetY(), $iw, $ih, '', $imglink, $align, false, 300, '', false, false, $border);
278: }
279: switch($align) {
280: case 'T': {
281: $this->y = $prevy;
282: break;
283: }
284: case 'M': {
285: $this->y = (($this->img_rb_y + $prevy - ($tag['fontsize'] / $this->k)) / 2) ;
286: break;
287: }
288: case 'B': {
289: $this->y = $this->img_rb_y - ($tag['fontsize'] / $this->k);
290: break;
291: }
292: }
293: }
294: break;
295: }
296: case 'dl': {
297: ++$this->listnum;
298: $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
299: break;
300: }
301: case 'dt': {
302: $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
303: break;
304: }
305: case 'dd': {
306: if ($this->rtl) {
307: $this->rMargin += $this->listindent;
308: } else {
309: $this->lMargin += $this->listindent;
310: }
311: $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
312: break;
313: }
314: case 'ul':
315: case 'ol': {
316: $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
317: $this->htmlvspace = 0;
318: ++$this->listnum;
319: if ($tag['value'] == 'ol') {
320: $this->listordered[$this->listnum] = true;
321: } else {
322: $this->listordered[$this->listnum] = false;
323: }
324: if (isset($tag['attribute']['start'])) {
325: $this->listcount[$this->listnum] = intval($tag['attribute']['start']) - 1;
326: } else {
327: $this->listcount[$this->listnum] = 0;
328: }
329: if ($this->rtl) {
330: $this->rMargin += $this->listindent;
331: } else {
332: $this->lMargin += $this->listindent;
333: }
334: $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
335: $this->htmlvspace = 0;
336: break;
337: }
338: case 'li': {
339: $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
340: if ($this->listordered[$this->listnum]) {
341:
342: if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
343: $this->lispacer = $parent['attribute']['type'];
344: } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
345: $this->lispacer = $parent['listtype'];
346: } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
347: $this->lispacer = $this->lisymbol;
348: } else {
349: $this->lispacer = '#';
350: }
351: ++$this->listcount[$this->listnum];
352: if (isset($tag['attribute']['value'])) {
353: $this->listcount[$this->listnum] = intval($tag['attribute']['value']);
354: }
355: } else {
356:
357: if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
358: $this->lispacer = $parent['attribute']['type'];
359: } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
360: $this->lispacer = $parent['listtype'];
361: } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
362: $this->lispacer = $this->lisymbol;
363: } else {
364: $this->lispacer = '!';
365: }
366: }
367: break;
368: }
369: case 'blockquote': {
370: if ($this->rtl) {
371: $this->rMargin += $this->listindent;
372: } else {
373: $this->lMargin += $this->listindent;
374: }
375: $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], false);
376: break;
377: }
378: case 'br': {
379: $this->Ln('', $cell);
380: break;
381: }
382: case 'div': {
383: $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
384: break;
385: }
386: case 'p': {
387: $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], false);
388: break;
389: }
390: case 'pre': {
391: $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
392: $this->premode = true;
393: break;
394: }
395: case 'sup': {
396: $this->SetXY($this->GetX(), $this->GetY() - ((0.7 * $this->FontSizePt) / $this->k));
397: break;
398: }
399: case 'sub': {
400: $this->SetXY($this->GetX(), $this->GetY() + ((0.3 * $this->FontSizePt) / $this->k));
401: break;
402: }
403: case 'h1':
404: case 'h2':
405: case 'h3':
406: case 'h4':
407: case 'h5':
408: case 'h6': {
409: $this->addHTMLVertSpace(1, $cell, ($tag['fontsize'] * 1.5) / $this->k, $firstorlast, $tag['value'], false);
410: break;
411: }
412: case 'tcpdf': {
413:
414: if (isset($tag['attribute']['method'])) {
415: $tcpdf_method = $tag['attribute']['method'];
416: if (method_exists($this, $tcpdf_method)) {
417: if (isset($tag['attribute']['params']) AND (!empty($tag['attribute']['params']))) {
418: eval('$params = array('.$tag['attribute']['params'].');');
419: call_user_func_array(array($this, $tcpdf_method), $params);
420: } else {
421: $this->$tcpdf_method();
422: }
423: $this->newline = true;
424: }
425: }
426: }
427: default: {
428: break;
429: }
430: }
431: }
432: }
433:
434: ?>