print_pdf_tcpdf.pages.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * @file
  4. * Generates the PDF version using TCPDF
  5. *
  6. * This file is included by the print_pdf_tcpdf module and includes the
  7. * functions that interface with the TCPDF library
  8. *
  9. * @ingroup print
  10. */
  11. /**
  12. * Implements hook_print_pdf_generate().
  13. */
  14. function print_pdf_tcpdf_print_pdf_generate($html, $meta, $paper_size = NULL, $page_orientation = NULL) {
  15. global $base_url, $language;
  16. module_load_include('inc', 'print', 'includes/print');
  17. $pdf_tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
  18. if (empty($paper_size)) {
  19. $paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  20. }
  21. if (empty($page_orientation)) {
  22. $page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  23. }
  24. $content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
  25. $images_via_file = variable_get('print_pdf_images_via_file', PRINT_PDF_IMAGES_VIA_FILE_DEFAULT);
  26. if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
  27. $pdf_tool_path = realpath(dirname($pdf_tool[1]));
  28. if (!defined('K_TCPDF_EXTERNAL_CONFIG')) define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
  29. if (!defined('K_PATH_MAIN')) define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
  30. if (!defined('K_PATH_URL')) define('K_PATH_URL', $base_url);
  31. if (!defined('K_PATH_FONTS')) define('K_PATH_FONTS', $pdf_tool_path . '/fonts/');
  32. if (!defined('K_PATH_CACHE')) define('K_PATH_CACHE', drupal_realpath('public://print_pdf/print_pdf_tcpdf/cache') . '/');
  33. if (!defined('K_PATH_IMAGES')) define('K_PATH_IMAGES', '');
  34. if (!defined('K_BLANK_IMAGE')) define('K_BLANK_IMAGE', $pdf_tool_path . '/images/_blank.png');
  35. if (!defined('K_CELL_HEIGHT_RATIO')) define('K_CELL_HEIGHT_RATIO', 1.25);
  36. if (!defined('K_SMALL_RATIO')) define('K_SMALL_RATIO', 2/3);
  37. }
  38. // Try to use local file access for image files
  39. $html = _print_access_images_via_file($html, $images_via_file);
  40. // Decode HTML entities in image filenames
  41. $pattern = "!<img\s[^>]*?src\s*?=\s*?['\"]?{$base_url}[^>]*?>!is";
  42. $html = preg_replace_callback($pattern, create_function('$matches', 'return html_entity_decode($matches[0], ENT_QUOTES);'), $html);
  43. // Remove queries from the image URL
  44. $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?{$base_url}[^>]*?)(?:%3F|\?)[^\s'\"]+([^>]*?>)!is";
  45. $html = preg_replace($pattern, '$1$2', $html);
  46. require_once(DRUPAL_ROOT . '/' . $pdf_tool[1]);
  47. module_load_include('inc', 'print_pdf_tcpdf', 'print_pdf_tcpdf.class');
  48. $font = Array(
  49. check_plain(variable_get('print_pdf_font_family', PRINT_PDF_TCPDF_FONT_FAMILY_DEFAULT)),
  50. '',
  51. check_plain(variable_get('print_pdf_font_size', PRINT_PDF_TCPDF_FONT_SIZE_DEFAULT)),
  52. );
  53. $orientation = drupal_strtoupper($page_orientation[0]);
  54. // create new PDF document
  55. $pdf = new PrintTCPDF($orientation , 'mm', $paper_size, TRUE);
  56. // set document information
  57. if (isset($meta['name'])) {
  58. $pdf->SetAuthor(strip_tags($meta['name']));
  59. }
  60. $pdf->SetCreator(variable_get('site_name', 'Drupal'));
  61. $pdf->SetTitle(html_entity_decode($meta['title'], ENT_QUOTES, 'UTF-8'));
  62. $pdf->setPDFVersion('1.6');
  63. $pdf->setFontSubsetting(variable_get('print_pdf_font_subsetting', PRINT_PDF_TCPDF_FONT_SUBSETTING_DEFAULT));
  64. $pdf->setTcpdfLink(false);
  65. if ($language->direction == LANGUAGE_RTL) {
  66. $pdf->setRTL(TRUE);
  67. }
  68. $pdf = theme('print_pdf_tcpdf_header', array('pdf' => $pdf, 'html' => $html, 'font' => $font));
  69. $pdf = theme('print_pdf_tcpdf_footer', array('pdf' => $pdf, 'html' => $html, 'font' => $font));
  70. $pdf = theme('print_pdf_tcpdf_page', array('pdf' => $pdf));
  71. // Enable third-party module to alter the pdf object, via hook_print_pdf_tcpdf_alter()
  72. drupal_alter('print_pdf_tcpdf', $pdf, $html, $meta);
  73. // add a page
  74. $pdf->AddPage();
  75. $pdf = theme('print_pdf_tcpdf_content', array('pdf' => $pdf, 'html' => $html, 'font' => $font));
  76. // reset pointer to the last page
  77. $pdf->lastPage();
  78. // try to recover from any warning/error
  79. ob_clean();
  80. return $pdf = $pdf->Output('', 'S');
  81. }
  82. /**
  83. * Format the TCPDF header
  84. *
  85. * @param array $vars
  86. * An associative array containing:
  87. * - $pdf: current TCPDF object
  88. * - $html: contents of the body of the HTML from the original node
  89. * - $font: array with the font definition (font name, styles and size)
  90. *
  91. * @return object
  92. * modified PDF object
  93. *
  94. * @ingroup themeable
  95. * @ingroup print_themeable
  96. */
  97. function theme_print_pdf_tcpdf_header($vars) {
  98. global $base_url;
  99. $pdf = $vars['pdf'];
  100. preg_match('!<div class="print-logo">(.*?)</div>!si', $vars['html'], $tpl_logo);
  101. preg_match('!<title>(.*?)</title>!si', $vars['html'], $tpl_title);
  102. preg_match('!<div class="print-site_name">(.*?)</div>!si', $vars['html'], $tpl_site_name);
  103. $ratio = 0;
  104. $logo = '';
  105. if (isset($tpl_logo[1]) && preg_match('!src\s*=\s*(?:"(.*?)"|\'(.*?)\'|([^\s]*))!i', $tpl_logo[1], $logo_url)) {
  106. $logo = $logo_url[1];
  107. // Make logo relative again
  108. $logo = preg_replace("!^$base_url(.*)!sm", dirname($_SERVER['SCRIPT_FILENAME']) . '$1', $logo);
  109. if (!empty($logo)) {
  110. $size = getimagesize($logo);
  111. $ratio = $size ? ($size[0] / $size[1]) : 0;
  112. }
  113. }
  114. // set header font
  115. $pdf->setHeaderFont($vars['font']);
  116. // set header margin
  117. $pdf->setHeaderMargin(5);
  118. // set header data
  119. $pdf->setHeaderData($logo, 10 * $ratio, html_entity_decode($tpl_title[1], ENT_QUOTES, 'UTF-8'), html_entity_decode(strip_tags($tpl_site_name[1]), ENT_QUOTES, 'UTF-8'));
  120. return $pdf;
  121. }
  122. /**
  123. * Format the TCPDF page settings (margins, etc)
  124. *
  125. * @param array $vars
  126. * An associative array containing:
  127. * - $pdf: current TCPDF object
  128. *
  129. * @return object
  130. * modified PDF object
  131. *
  132. * @ingroup themeable
  133. * @ingroup print_themeable
  134. */
  135. function theme_print_pdf_tcpdf_page($vars) {
  136. $pdf = $vars['pdf'];
  137. // set margins
  138. $pdf->SetMargins(15, 20, 15);
  139. // set auto page breaks
  140. $pdf->SetAutoPageBreak(TRUE, 15);
  141. // set image scale factor
  142. $pdf->setImageScale(1);
  143. // set image compression quality
  144. $pdf->setJPEGQuality(100);
  145. return $pdf;
  146. }
  147. /**
  148. * Format the TCPDF page content
  149. *
  150. * @param array $vars
  151. * An associative array containing:
  152. * - $pdf: current TCPDF object
  153. * - $html: contents of the body of the HTML from the original node
  154. * - $font: array with the font definition (font name, styles and size)
  155. *
  156. * @return object
  157. * modified PDF object
  158. *
  159. * @ingroup themeable
  160. * @ingroup print_themeable
  161. */
  162. function theme_print_pdf_tcpdf_content($vars) {
  163. $pdf = $vars['pdf'];
  164. // set content font
  165. $pdf->setFont($vars['font'][0], $vars['font'][1], $vars['font'][2]);
  166. // Remove the logo, published, breadcrumb and footer from the main content
  167. preg_match('!<body.*?>(.*)</body>!sim', $vars['html'], $matches);
  168. $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
  169. $matches[1] = preg_replace($pattern, '', $matches[1]);
  170. // Make CCK fields look better
  171. $matches[1] = preg_replace('!(<div class="field.*?>)\s*!sm', '$1', $matches[1]);
  172. $matches[1] = preg_replace('!(<div class="field.*?>.*?</div>)\s*!sm', '$1', $matches[1]);
  173. $matches[1] = preg_replace('!<div( class="field-label.*?>.*?)</div>!sm', '<strong$1</strong>', $matches[1]);
  174. // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
  175. $matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '<br$1 />', $matches[1]);
  176. $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
  177. do {
  178. $prev = $matches[1];
  179. $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
  180. } while ($prev != $matches[1]);
  181. @$pdf->writeHTML($matches[1]);
  182. return $pdf;
  183. }
  184. /**
  185. * Format the TCPDF footer contents
  186. *
  187. * @param array $vars
  188. * An associative array containing:
  189. * - $pdf: current TCPDF object
  190. * - $html: contents of the body of the HTML from the original node
  191. * - $font: array with the font definition (font name, styles and size)
  192. *
  193. * @return object
  194. * modified PDF object
  195. *
  196. * @ingroup themeable
  197. * @ingroup print_themeable
  198. */
  199. function theme_print_pdf_tcpdf_footer($vars) {
  200. $pdf = $vars['pdf'];
  201. preg_match('!<div class="print-footer">(.*?)</div>!si', $vars['html'], $tpl_footer);
  202. if (isset($tpl_footer[1])) {
  203. $footer = trim(preg_replace('!</?div[^>]*?>!i', '', $tpl_footer[1]));
  204. // set footer font
  205. $vars['font'][2] *= 0.8;
  206. $pdf->setFooterFont($vars['font']);
  207. // set footer margin
  208. $pdf->SetFooterMargin(10);
  209. // set footer data
  210. $pdf->setFooterContent($footer);
  211. }
  212. return $pdf;
  213. }
  214. /**
  215. * Format the TCPDF footer layout
  216. *
  217. * @param array $vars
  218. * An associative array containing:
  219. * - $pdf: current TCPDF object
  220. *
  221. * @return object
  222. * modified PDF object
  223. *
  224. * @ingroup themeable
  225. * @ingroup print_themeable
  226. */
  227. function theme_print_pdf_tcpdf_footer2($vars) {
  228. $pdf = $vars['pdf'];
  229. // Position at 1.5 cm from bottom
  230. $pdf->writeHTMLCell(0, 15, 15, $pdf->getPageHeight()-15, $pdf->footer);
  231. $ormargins = $pdf->getOriginalMargins();
  232. $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
  233. // Print page number
  234. if ($pdf->getRTL()) {
  235. $pdf->SetX($ormargins['right']);
  236. $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
  237. }
  238. else {
  239. $pdf->SetX($ormargins['left']);
  240. $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');
  241. }
  242. return $pdf;
  243. }