pdfpage.class.inc.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. require_once(APPROOT.'lib/tcpdf/tcpdf.php');
  3. /**
  4. * Custom class derived from TCPDF for providing custom headers and footers
  5. * @author denis
  6. *
  7. */
  8. class iTopPDF extends TCPDF
  9. {
  10. protected $sDocumentTitle;
  11. public function SetDocumentTitle($sDocumentTitle)
  12. {
  13. $this->sDocumentTitle = $sDocumentTitle;
  14. }
  15. /**
  16. * Builds the custom header. Called for each new page.
  17. * @see TCPDF::Header()
  18. */
  19. public function Header()
  20. {
  21. // Title
  22. // Set font
  23. $this->SetFont('dejavusans', 'B', 10);
  24. $iPageNumberWidth = 25;
  25. $aMargins = $this->getMargins();
  26. // Display the title (centered)
  27. $this->SetXY($aMargins['left'] + $iPageNumberWidth, 0);
  28. $this->MultiCell($this->getPageWidth() - $aMargins['left'] - $aMargins['right'] - 2*$iPageNumberWidth, 15, $this->sDocumentTitle, 0, 'C', false, 0 /* $ln */, '', '', true, 0, false, true, 15, 'M' /* $valign */);
  29. $this->SetFont('dejavusans', '', 10);
  30. // Display the page number (right aligned)
  31. // Warning: the 'R'ight alignment does not work when using placeholders like $this->getAliasNumPage() or $this->getAliasNbPages()
  32. $this->MultiCell($iPageNumberWidth, 15, 'Page '.$this->page, 0, 'R', false, 0 /* $ln */, '', '', true, 0, false, true, 15, 'M' /* $valign */);
  33. // Branding logo
  34. $sBrandingIcon = APPROOT.'images/itop-logo.png';
  35. if (file_exists(MODULESROOT.'branding/main-logo.png'))
  36. {
  37. $sBrandingIcon = MODULESROOT.'branding/main-logo.png';
  38. }
  39. $this->Image($sBrandingIcon, $aMargins['left'], 5, 0, 10);
  40. }
  41. // Page footer
  42. public function Footer()
  43. {
  44. // No footer
  45. }
  46. }
  47. /**
  48. * Special class of WebPage for printing into a PDF document
  49. */
  50. class PDFPage extends WebPage
  51. {
  52. /**
  53. * Instance of the TCPDF object for creating the PDF
  54. * @var TCPDF
  55. */
  56. protected $oPdf;
  57. public function __construct($s_title, $sPageFormat = 'A4', $sPageOrientation = 'L')
  58. {
  59. parent::__construct($s_title);
  60. define(K_PATH_FONTS, APPROOT.'lib/tcpdf/fonts');
  61. $this->oPdf = new iTopPDF($sPageOrientation, 'mm', $sPageFormat, true, 'UTF-8', false);
  62. // set document information
  63. $this->oPdf->SetCreator(PDF_CREATOR);
  64. $this->oPdf->SetAuthor('iTop');
  65. $this->oPdf->SetTitle($s_title);
  66. $this->oPdf->SetDocumentTitle($s_title);
  67. $this->oPdf->setFontSubsetting(true);
  68. // Set font
  69. // dejavusans is a UTF-8 Unicode font. Standard PDF fonts like helvetica or times new roman are NOT UTF-8
  70. $this->oPdf->SetFont('dejavusans', '', 10, '', true);
  71. // set auto page breaks
  72. $this->oPdf->SetAutoPageBreak(true, 15); // 15 mm break margin at the bottom
  73. $this->oPdf->SetTopMargin(15);
  74. // Add a page, we're ready to start
  75. $this->oPdf->AddPage();
  76. $this->SetContentDisposition('inline', $s_title.'.pdf');
  77. $this->SetDefaultStyle();
  78. }
  79. /**
  80. * Sets a default style (suitable for printing) to be included each time $this->oPdf->writeHTML() is called
  81. */
  82. protected function SetDefaultStyle()
  83. {
  84. $this->add_style(
  85. <<<EOF
  86. table {
  87. padding: 2pt;
  88. }
  89. table.listResults td {
  90. border: 0.5pt solid #000 ;
  91. }
  92. table.listResults th {
  93. background-color: #eee;
  94. border: 0.5pt solid #000 ;
  95. }
  96. a {
  97. text-decoration: none;
  98. color: #000;
  99. }
  100. table.section td {
  101. vertical-align: middle;
  102. font-size: 10pt;
  103. background-color:#eee;
  104. }
  105. td.icon {
  106. width: 30px;
  107. }
  108. EOF
  109. );
  110. }
  111. /**
  112. * Get access to the underlying TCPDF object
  113. * @return TCPDF
  114. */
  115. public function get_tcpdf()
  116. {
  117. $this->flush();
  118. return $this->oPdf;
  119. }
  120. /**
  121. * Writes the currently buffered HTML content into the PDF. This can be useful:
  122. * - to sync the flow in case you want to access the underlying TCPDF object for some specific/graphic output
  123. * - to process the HTML by smaller chunks instead of processing the whole page at once for performance reasons
  124. */
  125. public function flush()
  126. {
  127. if (strlen($this->s_content) > 0)
  128. {
  129. $sHtml = '';
  130. if (count($this->a_styles) > 0)
  131. {
  132. $sHtml .= "<style>\n".implode("\n", $this->a_styles)."\n</style>\n";
  133. }
  134. $sHtml .= $this->s_content;
  135. $this->oPdf->writeHTML($sHtml); // The style(s) must be supplied each time we call writeHtml
  136. $this->s_content = '';
  137. }
  138. }
  139. /**
  140. * Whether or not the page is a PDF page
  141. * @return boolean
  142. */
  143. public function is_pdf()
  144. {
  145. return true;
  146. }
  147. /**
  148. * Generates the PDF document and returns the PDF content as a string
  149. * @return string
  150. * @see WebPage::output()
  151. */
  152. public function output()
  153. {
  154. $this->add_header('Content-type: application/x-pdf');
  155. if (!empty($this->sContentDisposition))
  156. {
  157. $this->add_header('Content-Disposition: '.$this->sContentDisposition.'; filename="'.$this->sContentFileName.'"');
  158. }
  159. foreach($this->a_headers as $s_header)
  160. {
  161. header($s_header);
  162. }
  163. $this->flush();
  164. echo $this->oPdf->Output($this->s_title.'.pdf', 'S');
  165. }
  166. public function get_pdf()
  167. {
  168. $this->flush();
  169. return $this->oPdf->Output($this->s_title.'.pdf', 'S');
  170. }
  171. }