Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_PDF_Utils { | |
36 | ||
5bc392e6 EM |
37 | /** |
38 | * @param $text | |
39 | * @param string $fileName | |
40 | * @param bool $output | |
41 | * @param null $pdfFormat | |
42 | * | |
43 | * @return string|void | |
44 | */ | |
00be9182 | 45 | public static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) { |
6a488035 TO |
46 | if (is_array($text)) { |
47 | $pages = &$text; | |
48 | } | |
49 | else { | |
50 | $pages = array($text); | |
51 | } | |
52 | // Get PDF Page Format | |
53 | $format = CRM_Core_BAO_PdfFormat::getDefaultValues(); | |
54 | if (is_array($pdfFormat)) { | |
55 | // PDF Page Format parameters passed in | |
56 | $format = array_merge($format, $pdfFormat); | |
57 | } | |
58 | else { | |
59 | // PDF Page Format ID passed in | |
60 | $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat); | |
61 | } | |
353ffa53 TO |
62 | $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']); |
63 | $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt'); | |
6a488035 TO |
64 | $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt'); |
65 | // dompdf requires dimensions in points | |
353ffa53 | 66 | $paper_size = array(0, 0, $paper_width, $paper_height); |
6a488035 | 67 | $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format); |
353ffa53 TO |
68 | $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format); |
69 | $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format); | |
70 | $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format); | |
71 | $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format); | |
72 | $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format); | |
bdfa67c3 | 73 | |
353ffa53 | 74 | $stationery_path_partial = CRM_Core_BAO_PdfFormat::getValue('stationery', $format); |
bdfa67c3 | 75 | |
76 | $stationery_path = NULL; | |
8e637fa3 | 77 | if (strlen($stationery_path_partial)) { |
78 | $doc_root = $_SERVER['DOCUMENT_ROOT']; | |
79 | $stationery_path = $doc_root . "/" . $stationery_path_partial; | |
bdfa67c3 | 80 | } |
81 | ||
353ffa53 | 82 | $margins = array($metric, $t, $r, $b, $l); |
6a488035 TO |
83 | |
84 | $config = CRM_Core_Config::singleton(); | |
85 | $html = " | |
86 | <html> | |
87 | <head> | |
88 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> | |
89 | <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style> | |
90 | <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style> | |
91 | </head> | |
92 | <body> | |
93 | <div id=\"crm-container\">\n"; | |
94 | ||
95 | // Strip <html>, <header>, and <body> tags from each page | |
96 | $htmlElementstoStrip = array( | |
97 | '@<head[^>]*?>.*?</head>@siu', | |
8e637fa3 | 98 | '@<script[^>]*?>.*?</script>@siu', |
6a488035 TO |
99 | '@<body>@siu', |
100 | '@</body>@siu', | |
101 | '@<html[^>]*?>@siu', | |
102 | '@</html>@siu', | |
103 | '@<!DOCTYPE[^>]*?>@siu', | |
104 | ); | |
105 | $htmlElementsInstead = array('', '', '', '', '', ''); | |
106 | foreach ($pages as & $page) { | |
107 | $page = preg_replace($htmlElementstoStrip, | |
108 | $htmlElementsInstead, | |
109 | $page | |
110 | ); | |
111 | } | |
112 | // Glue the pages together | |
113 | $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages); | |
114 | $html .= " | |
115 | </div> | |
116 | </body> | |
117 | </html>"; | |
118 | if ($config->wkhtmltopdfPath) { | |
119 | return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName); | |
120 | } | |
121 | else { | |
8d7ae5ee | 122 | return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName); |
123 | //return self::_html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path); | |
6a488035 TO |
124 | } |
125 | } | |
126 | ||
2e2605fe EM |
127 | /** |
128 | * Convert html to tcpdf. | |
129 | * | |
130 | * @param $paper_size | |
131 | * @param $orientation | |
132 | * @param $margins | |
133 | * @param $html | |
134 | * @param $output | |
135 | * @param $fileName | |
136 | * @param $stationery_path | |
137 | */ | |
00be9182 | 138 | public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) { |
bdfa67c3 | 139 | // Documentation on the TCPDF library can be found at: http://www.tcpdf.org |
140 | // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/ | |
141 | // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc | |
142 | require_once 'tcpdf/tcpdf.php'; | |
e7292422 | 143 | require_once 'FPDI/fpdi.php'; // This library is only in the 'packages' area as of version 4.5 |
bdfa67c3 | 144 | |
353ffa53 | 145 | $paper_size_arr = array($paper_size[2], $paper_size[3]); |
bdfa67c3 | 146 | |
8e637fa3 | 147 | $pdf = new TCPDF($orientation, 'pt', $paper_size_arr); |
bdfa67c3 | 148 | $pdf->Open(); |
149 | ||
9b873358 | 150 | if (is_readable($stationery_path)) { |
481a74f4 | 151 | $pdf->SetStationery($stationery_path); |
bdfa67c3 | 152 | } |
153 | ||
154 | $pdf->SetAuthor(''); | |
155 | $pdf->SetKeywords('CiviCRM.org'); | |
481a74f4 | 156 | $pdf->setPageUnit($margins[0]); |
e7292422 | 157 | $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE); |
bdfa67c3 | 158 | |
159 | $pdf->setJPEGQuality('100'); | |
e7292422 | 160 | $pdf->SetAutoPageBreak(TRUE, $margins[3]); |
bdfa67c3 | 161 | |
162 | $pdf->AddPage(); | |
163 | ||
e7292422 TO |
164 | $ln = TRUE; |
165 | $fill = FALSE; | |
166 | $reset_parm = FALSE; | |
167 | $cell = FALSE; | |
168 | $align = ''; | |
8e637fa3 | 169 | |
bdfa67c3 | 170 | // output the HTML content |
171 | $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align); | |
172 | ||
173 | // reset pointer to the last page | |
174 | $pdf->lastPage(); | |
175 | ||
176 | // close and output the PDF | |
177 | $pdf->Close(); | |
92fcb95f | 178 | $pdf_file = 'CiviLetter' . '.pdf'; |
bdfa67c3 | 179 | $pdf->Output($pdf_file, 'D'); |
180 | CRM_Utils_System::civiExit(1); | |
bdfa67c3 | 181 | } |
182 | ||
5bc392e6 EM |
183 | /** |
184 | * @param $paper_size | |
185 | * @param $orientation | |
186 | * @param $html | |
187 | * @param $output | |
100fef9d | 188 | * @param string $fileName |
5bc392e6 EM |
189 | * |
190 | * @return string | |
191 | */ | |
00be9182 | 192 | public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) { |
2b20175c | 193 | // CRM-12165 - Remote file support required for image handling. |
f28ac29c TO |
194 | define('DOMPDF_ENABLE_REMOTE', TRUE); |
195 | define('DOMPDF_ENABLE_AUTOLOAD', FALSE); | |
196 | require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php'; | |
2b20175c | 197 | |
6a488035 TO |
198 | $dompdf = new DOMPDF(); |
199 | $dompdf->set_paper($paper_size, $orientation); | |
200 | $dompdf->load_html($html); | |
201 | $dompdf->render(); | |
202 | ||
203 | if ($output) { | |
204 | return $dompdf->output(); | |
205 | } | |
206 | else { | |
207 | $dompdf->stream($fileName); | |
208 | } | |
209 | } | |
210 | ||
5bc392e6 EM |
211 | /** |
212 | * @param $paper_size | |
213 | * @param $orientation | |
214 | * @param $margins | |
215 | * @param $html | |
216 | * @param $output | |
100fef9d | 217 | * @param string $fileName |
5bc392e6 | 218 | */ |
00be9182 | 219 | public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) { |
6a488035 TO |
220 | require_once 'packages/snappy/src/autoload.php'; |
221 | $config = CRM_Core_Config::singleton(); | |
222 | $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath); | |
223 | $snappy->setOption("page-width", $paper_size[2] . "pt"); | |
224 | $snappy->setOption("page-height", $paper_size[3] . "pt"); | |
225 | $snappy->setOption("orientation", $orientation); | |
226 | $snappy->setOption("margin-top", $margins[1] . $margins[0]); | |
227 | $snappy->setOption("margin-right", $margins[2] . $margins[0]); | |
228 | $snappy->setOption("margin-bottom", $margins[3] . $margins[0]); | |
229 | $snappy->setOption("margin-left", $margins[4] . $margins[0]); | |
230 | $pdf = $snappy->getOutputFromHtml($html); | |
231 | if ($output) { | |
232 | return $pdf; | |
233 | } | |
234 | else { | |
235 | header('Content-Type: application/pdf'); | |
236 | header('Content-Disposition: attachment; filename="' . $fileName . '"'); | |
237 | echo $pdf; | |
238 | } | |
239 | } | |
240 | ||
5bc392e6 | 241 | /** |
fe482240 | 242 | * convert value from one metric to another. |
d424ffde | 243 | * |
5bc392e6 EM |
244 | * @param $value |
245 | * @param $from | |
246 | * @param $to | |
247 | * @param null $precision | |
248 | * | |
249 | * @return float|int | |
250 | */ | |
00be9182 | 251 | public static function convertMetric($value, $from, $to, $precision = NULL) { |
6a488035 TO |
252 | switch ($from . $to) { |
253 | case 'incm': | |
254 | $value *= 2.54; | |
255 | break; | |
256 | ||
257 | case 'inmm': | |
258 | $value *= 25.4; | |
259 | break; | |
260 | ||
261 | case 'inpt': | |
262 | $value *= 72; | |
263 | break; | |
264 | ||
265 | case 'cmin': | |
266 | $value /= 2.54; | |
267 | break; | |
268 | ||
269 | case 'cmmm': | |
270 | $value *= 10; | |
271 | break; | |
272 | ||
273 | case 'cmpt': | |
274 | $value *= 72 / 2.54; | |
275 | break; | |
276 | ||
277 | case 'mmin': | |
278 | $value /= 25.4; | |
279 | break; | |
280 | ||
281 | case 'mmcm': | |
282 | $value /= 10; | |
283 | break; | |
284 | ||
285 | case 'mmpt': | |
286 | $value *= 72 / 25.4; | |
287 | break; | |
288 | ||
289 | case 'ptin': | |
290 | $value /= 72; | |
291 | break; | |
292 | ||
293 | case 'ptcm': | |
294 | $value *= 2.54 / 72; | |
295 | break; | |
296 | ||
297 | case 'ptmm': | |
298 | $value *= 25.4 / 72; | |
299 | break; | |
300 | } | |
301 | if (!is_null($precision)) { | |
302 | $value = round($value, $precision); | |
303 | } | |
304 | return $value; | |
305 | } | |
306 | ||
5bc392e6 | 307 | /** |
100fef9d | 308 | * @param string $fileName |
5bc392e6 EM |
309 | * @param $searchPath |
310 | * @param $values | |
311 | * @param int $numPages | |
312 | * @param bool $echo | |
313 | * @param string $output | |
314 | * @param string $creator | |
315 | * @param string $author | |
316 | * @param string $title | |
317 | */ | |
13efab9d | 318 | public static function &pdflib( |
a3e55d9c | 319 | $fileName, |
6a488035 TO |
320 | $searchPath, |
321 | &$values, | |
322 | $numPages = 1, | |
e7292422 TO |
323 | $echo = TRUE, |
324 | $output = 'College_Match_App', | |
325 | $creator = 'CiviCRM', | |
326 | $author = 'http://www.civicrm.org/', | |
327 | $title = '2006 College Match Scholarship Application' | |
6a488035 TO |
328 | ) { |
329 | try { | |
330 | $pdf = new PDFlib(); | |
331 | $pdf->set_parameter("compatibility", "1.6"); | |
332 | $pdf->set_parameter("licensefile", "/home/paras/bin/license/pdflib.txt"); | |
333 | ||
334 | if ($pdf->begin_document('', '') == 0) { | |
335 | CRM_Core_Error::statusBounce("PDFlib Error: " . $pdf->get_errmsg()); | |
336 | } | |
337 | ||
338 | $config = CRM_Core_Config::singleton(); | |
339 | $pdf->set_parameter('resourcefile', $config->templateDir . '/Quest/pdf/pdflib.upr'); | |
340 | $pdf->set_parameter('textformat', 'utf8'); | |
341 | ||
342 | /* Set the search path for fonts and PDF files */ | |
343 | ||
344 | $pdf->set_parameter('SearchPath', $searchPath); | |
345 | ||
346 | /* This line is required to avoid problems on Japanese systems */ | |
347 | ||
348 | $pdf->set_parameter('hypertextencoding', 'winansi'); | |
349 | ||
350 | $pdf->set_info('Creator', $creator); | |
351 | $pdf->set_info('Author', $author); | |
352 | $pdf->set_info('Title', $title); | |
353 | ||
354 | $blockContainer = $pdf->open_pdi($fileName, '', 0); | |
355 | if ($blockContainer == 0) { | |
356 | CRM_Core_Error::statusBounce('PDFlib Error: ' . $pdf->get_errmsg()); | |
357 | } | |
358 | ||
359 | for ($i = 1; $i <= $numPages; $i++) { | |
360 | $page = $pdf->open_pdi_page($blockContainer, $i, ''); | |
361 | if ($page == 0) { | |
362 | CRM_Core_Error::statusBounce('PDFlib Error: ' . $pdf->get_errmsg()); | |
363 | } | |
364 | ||
365 | /* dummy page size */ | |
366 | $pdf->begin_page_ext(20, 20, ''); | |
367 | ||
368 | /* This will adjust the page size to the block container's size. */ | |
369 | ||
370 | $pdf->fit_pdi_page($page, 0, 0, 'adjustpage'); | |
371 | ||
6a488035 TO |
372 | $status = array(); |
373 | /* Fill all text blocks with dynamic data */ | |
374 | ||
375 | foreach ($values as $key => $value) { | |
376 | if (is_array($value)) { | |
377 | continue; | |
378 | } | |
379 | ||
380 | // pdflib does like the forward slash character, hence convert | |
381 | $value = str_replace('/', '_', $value); | |
382 | ||
383 | $res = $pdf->fill_textblock($page, | |
384 | $key, | |
385 | $value, | |
386 | 'embedding encoding=winansi' | |
387 | ); | |
388 | ||
6a488035 TO |
389 | } |
390 | ||
391 | $pdf->end_page_ext(''); | |
392 | $pdf->close_pdi_page($page); | |
393 | } | |
394 | ||
395 | $pdf->end_document(''); | |
396 | $pdf->close_pdi($blockContainer); | |
397 | ||
398 | $buf = $pdf->get_buffer(); | |
399 | $len = strlen($buf); | |
400 | ||
401 | if ($echo) { | |
402 | header('Content-type: application/pdf'); | |
403 | header("Content-Length: $len"); | |
404 | header("Content-Disposition: inline; filename={$output}.pdf"); | |
405 | echo $buf; | |
406 | CRM_Utils_System::civiExit(); | |
407 | } | |
408 | else { | |
409 | return $buf; | |
410 | } | |
411 | } | |
353ffa53 | 412 | catch (PDFlibException$excp) { |
6a488035 TO |
413 | CRM_Core_Error::statusBounce('PDFlib Error: Exception' . |
414 | "[" . $excp->get_errnum() . "] " . $excp->get_apiname() . ": " . | |
415 | $excp->get_errmsg() | |
416 | ); | |
417 | } | |
353ffa53 | 418 | catch (Exception$excp) { |
6a488035 TO |
419 | CRM_Core_Error::statusBounce("PDFlib Error: " . $excp->get_errmsg()); |
420 | } | |
421 | } | |
96025800 | 422 | |
6a488035 | 423 | } |