1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Service\AbstractContract;
13: use Xoops\Core\Service\Contract\HtmlToPdfInterface;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class HtmlToPdfProvider extends AbstractContract implements HtmlToPdfInterface
27: {
28:
29: private $moddir = 'pdf';
30:
31:
32: protected $pdfEngine;
33:
34:
35: protected $pageOrientation = 'P';
36:
37:
38: protected $pageSize = 'A4';
39:
40:
41: protected $unit = 'mm';
42:
43:
44: protected $leftMargin;
45:
46:
47: protected $topMargin;
48:
49:
50: protected $rightMargin;
51:
52:
53: protected $bottomMargin;
54:
55:
56: protected $fontFamily;
57:
58:
59: protected $fontStyle;
60:
61:
62: protected $fontSize;
63:
64:
65: protected $monoFontFamily;
66:
67:
68: protected $pdfAuthor;
69:
70:
71: protected $pdfTitle;
72:
73:
74: protected $pdfSubject;
75:
76:
77: protected $pdfKeywords;
78:
79:
80: protected $pdfCreator;
81:
82:
83: protected $moduleConfigs;
84:
85: 86: 87:
88: public function __construct()
89: {
90: $this->resetPdf();
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100: 101:
102: private function setFromConfigs($name, $property, $default)
103: {
104: $this->$property = $default;
105: if (isset($this->moduleConfigs[$name])) {
106: $value = $this->moduleConfigs[$name];
107: $this->$property = empty($value) ? $default : $value;
108: }
109: }
110: 111: 112: 113: 114:
115: protected function resetPdf()
116: {
117: unset($this->pdfEngine);
118:
119: unset($this->pdfAuthor);
120: unset($this->pdfTitle);
121: unset($this->pdfSubject);
122: unset($this->pdfKeywords);
123:
124: $this->moduleConfigs = \Xoops::getInstance()->getModuleConfigs($this->moddir);
125:
126: $this->setFromConfigs('page_orientation', 'pageOrientation', 'P');
127: $this->setFromConfigs('page_size', 'pageSize', 'A4');
128: $this->setFromConfigs('pdf_creator', 'pdfCreator', 'XOOPS');
129:
130: $this->setFromConfigs('font_family', 'fontFamily', null);
131: $this->setFromConfigs('font_style', 'fontStyle', '');
132: $this->setFromConfigs('font_style', 'fontStyle', '');
133: $this->setFromConfigs('font_size', 'fontSize', 10);
134: $this->setFromConfigs('monofont_family', 'monoFontFamily', null);
135: $this->setFromConfigs('size_unit', 'unit', 'mm');
136: $this->setFromConfigs('margin_left', 'leftMargin', null);
137: $this->setFromConfigs('margin_top', 'topMargin', null);
138: $this->setFromConfigs('margin_right', 'rightMargin', null);
139: $this->setFromConfigs('margin_bottom', 'bottomMargin', null);
140: }
141:
142: 143: 144: 145: 146: 147: 148:
149: public function startPdf($response)
150: {
151: $this->resetPdf();
152: }
153:
154: 155: 156: 157: 158: 159:
160: public function getName()
161: {
162: return 'pdf';
163: }
164:
165: 166: 167: 168: 169:
170: public function getDescription()
171: {
172: return 'Simple HTML to PDF using TCPDF.';
173: }
174:
175: 176: 177: 178: 179: 180: 181: 182:
183: public function setPageOrientation($response, $pageOrientation)
184: {
185: $this->pageOrientation = $pageOrientation;
186: if (isset($this->pdfEngine)) {
187: $this->pdfEngine->setPageOrientation($this->pageOrientation, true, $this->bottomMargin);
188: }
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198:
199: public function setPageSize($response, $pageSize)
200: {
201: $this->pageSize = $pageSize;
202: if (isset($this->pdfEngine)) {
203: $this->pdfEngine->setPageFormat($this->pageSize, $this->pageOrientation);
204: }
205: }
206:
207: 208: 209: 210: 211: 212: 213: 214: 215:
216: public function setBaseUnit($response, $unit)
217: {
218: $this->unit = $unit;
219: if (isset($this->pdfEngine)) {
220: $this->pdfEngine->setPageUnit($unit);
221: }
222: }
223:
224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234:
235: public function setMargins($response, $leftMargin, $topMargin, $rightMargin, $bottomMargin)
236: {
237: $this->$leftMargin = $leftMargin;
238: $this->$topMargin = $topMargin;
239: $this->$rightMargin = $rightMargin;
240: $this->$bottomMargin = $bottomMargin;
241: if (isset($this->pdfEngine)) {
242: $this->pdfEngine->SetMargins($this->leftMargin, $this->topMargin, $this->rightMargin);
243: if (empty($this->bottomMargin)) {
244: $this->bottomMargin = PDF_MARGIN_BOTTOM;
245: }
246: $this->pdfEngine->SetAutoPageBreak(true, $this->bottomMargin);
247: }
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259:
260: public function setBaseFont($response, $fontFamily, $fontStyle = '', $fontSize = null)
261: {
262: $this->fontFamily = $fontFamily;
263: $this->fontStyle = $fontStyle;
264: $this->fontSize = $fontSize;
265: if (isset($this->pdfEngine)) {
266: $this->pdfEngine->SetFont($this->fontFamily, $this->fontStyle, $this->fontSize);
267: }
268: }
269:
270: 271: 272: 273: 274: 275: 276: 277:
278: public function setDefaultMonospacedFont($response, $monoFontFamily)
279: {
280: $this->monoFontFamily = $monoFontFamily;
281: if (isset($this->pdfEngine)) {
282: $this->pdfEngine->SetDefaultMonospacedFont($this->monoFontFamily);
283: }
284: }
285:
286: 287: 288: 289: 290: 291: 292: 293:
294: public function setAuthor($response, $pdfAuthor)
295: {
296: $this->pdfAuthor = $this->decodeEntities($pdfAuthor);
297: if (isset($this->pdfEngine)) {
298: $this->pdfEngine->SetAuthor($this->pdfAuthor);
299: }
300: }
301:
302: 303: 304: 305: 306: 307: 308: 309:
310: public function setTitle($response, $pdfTitle)
311: {
312: $this->pdfTitle = $this->decodeEntities($pdfTitle);
313: if (isset($this->pdfEngine)) {
314: $this->pdfEngine->SetTitle($this->pdfTitle);
315: }
316: }
317:
318: 319: 320: 321: 322: 323: 324: 325:
326: public function setSubject($response, $pdfSubject)
327: {
328: $this->pdfSubject = $this->decodeEntities($pdfSubject);
329: if (isset($this->pdfEngine)) {
330: $this->pdfEngine->SetSubject($this->pdfSubject);
331: }
332: }
333:
334: 335: 336: 337: 338: 339: 340: 341:
342: public function setKeywords($response, $pdfKeywords)
343: {
344: $this->pdfKeywords = $pdfKeywords;
345: if (isset($this->pdfEngine)) {
346: $keywords =
347: is_array($this->pdfKeywords) ? implode(', ', $this->pdfKeywords) : (string) $this->pdfKeywords;
348: $this->pdfEngine->SetKeywords($keywords);
349: }
350: }
351:
352: 353: 354: 355: 356: 357: 358: 359: 360:
361: public function addHtml($response, $html)
362: {
363: $this->initPdf();
364: $this->pdfEngine->AddPage();
365: $this->pdfEngine->writeHTML($html, true, false, true, false, '');
366: }
367:
368: 369: 370: 371: 372: 373: 374: 375:
376: public function outputPdfInline($response, $name)
377: {
378: $this->initPdf();
379: if (empty($name)) {
380: $name = 'requested.pdf';
381: }
382: $this->pdfEngine->lastPage();
383: $this->pdfEngine->Output($name, 'I');
384: }
385:
386: 387: 388: 389: 390: 391: 392: 393:
394: public function outputPdfDownload($response, $name)
395: {
396: $this->initPdf();
397: if (empty($name)) {
398: $name = 'requested.pdf';
399: }
400: $this->pdfEngine->lastPage();
401: $this->pdfEngine->Output($name, 'D');
402: }
403:
404: 405: 406: 407: 408: 409: 410:
411: public function fetchPdf($response)
412: {
413: $this->initPdf();
414: $this->pdfEngine->lastPage();
415: $response->seValue($this->pdfEngine->Output('', 'S'));
416: }
417:
418: 419: 420: 421: 422:
423: private function initPdf()
424: {
425: if (empty($this->pdfEngine)) {
426: $this->pdfEngine = new TCPDF($this->pageOrientation, $this->unit, $this->pageSize, true, 'UTF-8', false);
427: if (isset($this->pdfAuthor)) {
428: $this->pdfEngine->SetAuthor($this->pdfAuthor);
429: }
430: if (isset($this->pdfTitle)) {
431: $this->pdfEngine->SetTitle($this->pdfTitle);
432: }
433: if (isset($this->pdfSubject)) {
434: $this->pdfEngine->SetSubject($this->pdfSubject);
435: }
436: if (isset($this->pdfKeywords)) {
437: $keywords =
438: is_array($this->pdfKeywords) ? implode(', ', $this->pdfKeywords) : (string) $this->pdfKeywords;
439: $this->pdfEngine->SetKeywords($keywords);
440: }
441: if (!empty($this->pdfCreator)) {
442: $this->pdfEngine->SetCreator($this->pdfCreator);
443: }
444: if (!empty($this->fontFamily)) {
445: $this->pdfEngine->SetFont($this->fontFamily, $this->fontStyle, $this->fontSize);
446: }
447: if (!empty($this->monoFontFamily)) {
448: $this->pdfEngine->SetDefaultMonospacedFont($this->monoFontFamily);
449: }
450: if (!empty($this->leftMargin)) {
451: $this->pdfEngine->SetMargins($this->leftMargin, $this->topMargin, $this->rightMargin);
452: }
453: if (empty($this->bottomMargin)) {
454: $this->bottomMargin = PDF_MARGIN_BOTTOM;
455: }
456: $this->pdfEngine->SetAutoPageBreak(true, $this->bottomMargin);
457: }
458: }
459:
460: 461: 462: 463: 464: 465: 466:
467: private function decodeEntities($text)
468: {
469: $text= html_entity_decode($text, ENT_QUOTES, "UTF-8");
470: $text= preg_replace_callback(
471: '/&#(\d+);/m',
472: function ($m) {
473: return utf8_encode(chr($m[1]));
474: },
475: $text
476: );
477: $text= preg_replace_callback(
478: '/&#x([a-f0-9]+);/mi',
479: function ($m) {
480: return utf8_encode(chr('0x'.$m[1]));
481: },
482: $text
483: );
484: return $text;
485: }
486: }
487: