XOOPS  2.6.0
HtmlToPdfProvider.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
14 
27 {
29  private $moddir = 'pdf';
30 
32  protected $pdfEngine;
33 
35  protected $pageOrientation = 'P';
36 
38  protected $pageSize = 'A4';
39 
41  protected $unit = 'mm';
42 
44  protected $leftMargin;
45 
47  protected $topMargin;
48 
50  protected $rightMargin;
51 
53  protected $bottomMargin;
54 
56  protected $fontFamily;
57 
59  protected $fontStyle;
60 
62  protected $fontSize;
63 
65  protected $monoFontFamily;
66 
68  protected $pdfAuthor;
69 
71  protected $pdfTitle;
72 
74  protected $pdfSubject;
75 
77  protected $pdfKeywords;
78 
80  protected $pdfCreator;
81 
83  protected $moduleConfigs;
84 
88  public function __construct()
89  {
90  $this->resetPdf();
91  }
92 
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  }
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 
149  public function startPdf($response)
150  {
151  $this->resetPdf();
152  }
153 
160  public function getName()
161  {
162  return 'pdf';
163  }
164 
170  public function getDescription()
171  {
172  return 'Simple HTML to PDF using TCPDF.';
173  }
174 
184  {
185  $this->pageOrientation = $pageOrientation;
186  if (isset($this->pdfEngine)) {
187  $this->pdfEngine->setPageOrientation($this->pageOrientation, true, $this->bottomMargin);
188  }
189  }
190 
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 
216  public function setBaseUnit($response, $unit)
217  {
218  $this->unit = $unit;
219  if (isset($this->pdfEngine)) {
220  $this->pdfEngine->setPageUnit($unit);
221  }
222  }
223 
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 
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 
279  {
280  $this->monoFontFamily = $monoFontFamily;
281  if (isset($this->pdfEngine)) {
282  $this->pdfEngine->SetDefaultMonospacedFont($this->monoFontFamily);
283  }
284  }
285 
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 
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 
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 
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 
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 
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 
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 
411  public function fetchPdf($response)
412  {
413  $this->initPdf();
414  $this->pdfEngine->lastPage();
415  $response->seValue($this->pdfEngine->Output('', 'S'));
416  }
417 
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 
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  ); // decimal notation
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  ); //hex notation
484  return $text;
485  }
486 }
static getInstance()
Definition: Xoops.php:160
setSubject($response, $pdfSubject)
$text
Definition: qrrender.php:27
setPageOrientation($response, $pageOrientation)
if(!$xoops->security() ->validateToken(@$_POST['token'], false)) $html
setDefaultMonospacedFont($response, $monoFontFamily)
addHtml($response, $html)
setBaseUnit($response, $unit)
outputPdfInline($response, $name)
setPageSize($response, $pageSize)
setTitle($response, $pdfTitle)
setMargins($response, $leftMargin, $topMargin, $rightMargin, $bottomMargin)
setFromConfigs($name, $property, $default)
setAuthor($response, $pdfAuthor)
if($xoops->isUser()&&$isAdmin) $response
Definition: userinfo.php:83
setKeywords($response, $pdfKeywords)
outputPdfDownload($response, $name)
setBaseFont($response, $fontFamily, $fontStyle= '', $fontSize=null)