Display normal link in pages of type book that are not in books
[project/print.git] / print_pdf / print_pdf.pages.inc
CommitLineData
b09f08f7 1<?php
3483245d
JV
2// $Id$
3
4/**
5 * @file
6 * Generates the PDF versions of the pages
7 *
8 * This file is included by the print_pdf module and includes the
9 * functions that interface with the PDF generation packages.
10 */
b09f08f7 11
c67b9ed4 12require_once(drupal_get_path('module', 'print') .'/print.pages.inc');
b09f08f7 13
6a4c89f9
JV
14/**
15 * Generate a PDF version of the printer-friendly page
16 *
17 * @see print_controller()
18 * @see _print_get_template()
19 * @see _print_pdf_dompdf()
20 * @see _print_pdf_tcpdf()
21 */
b09f08f7 22function print_pdf_controller() {
5b1b9562 23 $args = func_get_args();
3c90dace 24 // Remove the printpdf/ prefix
5b1b9562 25 $path = implode('/', $args);
0706a465 26 $cid = isset($_GET['comment']) ? $_GET['comment'] : NULL;
3c90dace 27
7d62e7c4 28 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
b09f08f7 29
0706a465 30 $print = print_controller($path, $cid);
b09f08f7 31 // Img elements must be set to absolute
7d62e7c4
JV
32 $pattern = '!<(img\s[^>]*?)>!is';
33 $print['content'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['content']);
34 $print['logo'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['logo']);
35 $print['footer_message'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['footer_message']);
b09f08f7 36
7d62e7c4 37 $node = $print['node'];
1caf9f9d 38 ob_start();
7d62e7c4 39 include_once(_print_get_template('pdf', $print['type']));
1caf9f9d
JV
40 $html = ob_get_contents();
41 ob_end_clean();
42
7d62e7c4
JV
43 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
44 _print_pdf_dompdf($print, $html, $path .'.pdf');
b09f08f7 45 }
7d62e7c4
JV
46 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
47 _print_pdf_tcpdf($print, $html, $path .'.pdf');
b09f08f7
JV
48 }
49 else {
50 return drupal_not_found();
51 }
52}
53
6a4c89f9
JV
54/**
55 * Generate the PDF file using the dompdf library
56 *
57 * @param $print
58 * array containing the configured data
59 * @param $html
60 * contents of the post-processed template already with the node data
61 * @param $filename
62 * name of the PDF file to be generated
63 * @see print_pdf_controller()
64 */
1caf9f9d 65function _print_pdf_dompdf($print, $html, $filename) {
7d62e7c4
JV
66 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
67 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
68 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
69 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
70 require_once($print_pdf_pdf_tool);
b09f08f7 71
56bdd124
JV
72 $url_array = parse_url($print['url']);
73
3483245d 74 $protocol = $url_array['scheme'] .'://';
56bdd124 75 $host = $url_array['host'];
3483245d 76 $path = dirname($url_array['path']) .'/';
b09f08f7
JV
77
78 $dompdf = new DOMPDF();
56bdd124
JV
79 $dompdf->set_base_path($path);
80 $dompdf->set_host($host);
6a4c89f9 81 $dompdf->set_paper(drupal_strtolower($print_pdf_paper_size), $print_pdf_page_orientation);
56bdd124
JV
82 $dompdf->set_protocol($protocol);
83
b09f08f7 84 $dompdf->load_html($html);
3483245d 85
b09f08f7 86 $dompdf->render();
7d62e7c4 87 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
b09f08f7
JV
88}
89
6a4c89f9
JV
90/**
91 * Generate the PDF file using the TCPDF library
92 *
93 * @param $print
94 * array containing the configured data
95 * @param $html
96 * contents of the post-processed template already with the node data
97 * @param $filename
98 * name of the PDF file to be generated
99 * @see print_pdf_controller()
100 */
1caf9f9d 101function _print_pdf_tcpdf($print, $html, $filename) {
dc300af6 102 global $base_url;
7d62e7c4
JV
103 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
104 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
105 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
106 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
107
4817021f
JV
108 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
109
7d62e7c4 110 define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
dc300af6
JV
111 define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
112 define('K_PATH_URL', $base_url);
4817021f
JV
113 define('K_PATH_FONTS', $pdf_tool_path .'/fonts/');
114 define('K_PATH_CACHE', $pdf_tool_path .'/cache/');
115 define('K_PATH_IMAGES', '');
116 define('K_BLANK_IMAGE', $pdf_tool_path .'/images/_blank.png');
7d62e7c4
JV
117 define('K_CELL_HEIGHT_RATIO', 1.25);
118 define('K_SMALL_RATIO', 2/3);
119
120 require_once($print_pdf_pdf_tool);
e03112ed 121 if (strpos(PDF_PRODUCER, 'PHP4') === FALSE) {
1024f54b 122 require_once(drupal_get_path('module', 'print_pdf') .'/print_pdf.class.inc');
90263e69
JV
123 }
124 else {
1024f54b 125 require_once(drupal_get_path('module', 'print_pdf') .'/print_pdf.class_php4.inc');
90263e69 126 }
7d62e7c4 127
455e82fd
JV
128 $font = Array(
129 variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT),
b2a85b2b 130 '',
455e82fd
JV
131 variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT),
132 );
6a4c89f9 133 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
56bdd124
JV
134
135 // create new PDF document
3da84173 136 $pdf = new PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE);
b09f08f7 137
56bdd124 138 // set document information
7d62e7c4
JV
139 $pdf->SetAuthor(strip_tags($print['submitted']));
140 $pdf->SetCreator(variable_get('site_name', 'Drupal'));
141 $pdf->SetTitle($print['title']);
142 $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
56bdd124 143 $pdf->SetKeywords($keys);
7d62e7c4 144 $pdf->setPDFVersion('1.6');
56bdd124 145
1024f54b
JV
146 $pdf = theme('print_pdf_tcpdf_header', $pdf, $html, $font);
147 $pdf = theme('print_pdf_tcpdf_footer', $pdf, $html, $font);
148 $pdf = theme('print_pdf_tcpdf_page', $pdf);
4817021f
JV
149
150 //initialize document
151 $pdf->AliasNbPages();
152
153 // add a page
154 $pdf->AddPage();
155
1024f54b 156 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
4817021f
JV
157
158 // reset pointer to the last page
159 $pdf->lastPage();
160
161 //Close and output PDF document
162 $output_dest = ($print_pdf_content_disposition == 2) ? 'D' : 'I';
163 $pdf->Output($filename, $output_dest);
164}
165
6a4c89f9
JV
166/**
167 * Format the TCPDF header
168 *
169 * @param $pdf
170 * current TCPDF object
171 * @param $html
172 * contents of the body of the HTML from the original node
173 * @param $font
174 * array with the font definition (font name, styles and size)
175 * @see theme_print_pdf_tcpdf_header()
176 */
4817021f
JV
177function _print_pdf_tcpdf_header_aux(&$pdf, &$html, $font) {
178 preg_match('!<div class="print-logo">(.*?)</div>!si', $html, $tpl_logo);
179 preg_match('!<h1 class="print-title">(.*?)</h1>!si', $html, $tpl_title);
180 preg_match('!<div class="print-site_name">(.*?)</div>!si', $html, $tpl_site_name);
181
74df721a 182 $ratio = 0;
4817021f
JV
183 $logo = '';
184 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
3da84173
JV
185 if ($logo_ret) {
186 $logo = trim($matches[1], '\'"');
4817021f
JV
187 $size = getimagesize($logo);
188 $ratio = $size[0] / $size[1];
3da84173
JV
189 }
190
4817021f
JV
191 // set header font
192 $pdf->setHeaderFont($font);
193 // set header margin
194 $pdf->SetHeaderMargin(5);
56bdd124 195 // set header data
4817021f 196 $pdf->SetHeaderData($logo, 10 * $ratio, $tpl_title[1], strip_tags($tpl_site_name[1]));
1024f54b
JV
197
198 return $pdf;
4817021f 199}
3da84173 200
6a4c89f9
JV
201/**
202 * Format the TCPDF page settings (margins, etc)
203 *
204 * @param $pdf
205 * current TCPDF object
206 * @see theme_print_pdf_tcpdf_page()
207 */
4817021f 208function _print_pdf_tcpdf_page_aux(&$pdf) {
b09f08f7 209 // set margins
4817021f 210 $pdf->SetMargins(15, 20, 15);
c67b9ed4 211 // set auto page breaks
4817021f 212 $pdf->SetAutoPageBreak(TRUE, 15);
b09f08f7 213 // set image scale factor
75628d9a 214 $pdf->setImageScale(4);
973f5b72
JV
215 // set image compression quality
216 $pdf->setJPEGQuality(100);
1024f54b
JV
217
218 return $pdf;
4817021f 219}
973f5b72 220
6a4c89f9
JV
221/**
222 * Format the TCPDF page content
223 *
224 * @param $pdf
225 * current TCPDF object
226 * @param $html
227 * contents of the body of the HTML from the original node
228 * @param $font
229 * array with the font definition (font name, styles and size)
230 * @see theme_print_pdf_tcpdf_content()
231 */
4817021f
JV
232function _print_pdf_tcpdf_content_aux(&$pdf, &$html, $font) {
233 // set content font
234 $pdf->setFont($font[0], $font[1], $font[2]);
b09f08f7 235
26673803 236 preg_match('!<body.*?>(.*)</body>!sim', $html, $matches);
4817021f 237 $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
3da84173
JV
238 $matches[1] = preg_replace($pattern, '', $matches[1]);
239
4817021f
JV
240// $pdf->writeHTML($matches[1]);
241
242 // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
243 $matches[1] = preg_replace('!(<p\s*/>|</p>)!i', '<br />', $matches[1]);
244 $matches[1] = str_replace(array('<div', 'div>'), array('<p', 'p>'), $matches[1]);
245 $paragraphs = preg_split('!(<p[^>]*?>|</p>)!i', $matches[1], -1, PREG_SPLIT_NO_EMPTY);
6a4c89f9 246 foreach ($paragraphs as $value) {
3da84173
JV
247 $par = trim($value);
248 if (!empty($par)) {
4817021f 249 $pdf->writeHTML($par);
3da84173 250 }
b09f08f7 251 }
1024f54b
JV
252
253 return $pdf;
4817021f 254}
b09f08f7 255
6a4c89f9
JV
256/**
257 * Format the TCPDF footer contents
258 *
259 * @param $pdf
260 * current TCPDF object
261 * @param $html
262 * contents of the body of the HTML from the original node
263 * @param $font
264 * array with the font definition (font name, styles and size)
265 * @see theme_print_pdf_tcpdf_footer()
266 */
4817021f 267function _print_pdf_tcpdf_footer_aux(&$pdf, &$html, $font) {
90263e69 268 preg_match('!<div class="print-footer">.*<div class="content">(.*?)</div>!si', $html, $tpl_footer);
2c779f90 269
4817021f
JV
270 // set footer font
271 $font[2] *= 0.8;
272 $pdf->setFooterFont($font);
273 // set footer margin
274 $pdf->SetFooterMargin(10);
275 // set footer data
276 $pdf->SetFooterData($tpl_footer[1]);
1024f54b
JV
277
278 return $pdf;
b09f08f7 279}
6a4c89f9
JV
280
281/**
282 * Format the TCPDF footer layout
283 *
284 * @param $pdf
285 * current TCPDF object
286 * @see theme_print_pdf_tcpdf_footer2()
287 */
288function _print_pdf_tcpdf_footer2_aux(&$pdf) {
289 //Position at 1.5 cm from bottom
90263e69 290 $pdf->writeHTMLCell(0, 15, 15, 0, $pdf->footer, 0, 0, 0, TRUE, '');
6a4c89f9
JV
291
292 $ormargins = $pdf->getOriginalMargins();
90263e69 293 $pagenumtxt = t('Page') .' '. $pdf->PageNo() .' / '. $pdf->getAliasNbPages();
6a4c89f9
JV
294 //Print page number
295 if ($pdf->getRTL()) {
296 $pdf->SetX($ormargins['right']);
297 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
298 }
299 else {
300 $pdf->SetX($ormargins['left']);
301 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');
302 }
1024f54b
JV
303
304 return $pdf;
6a4c89f9 305}