Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | */ | |
6a488035 TO |
45 | static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) { |
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 | } | |
62 | $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']); | |
63 | $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt'); | |
64 | $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt'); | |
65 | // dompdf requires dimensions in points | |
66 | $paper_size = array(0, 0, $paper_width, $paper_height); | |
67 | $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format); | |
68 | $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format); | |
69 | $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format); | |
bdfa67c3 | 70 | $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format); |
6a488035 TO |
71 | $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format); |
72 | $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format); | |
bdfa67c3 | 73 | |
74 | $stationery_path_partial = CRM_Core_BAO_PdfFormat::getValue('stationery', $format); | |
75 | ||
76 | $stationery_path = NULL; | |
77 | if(strlen($stationery_path_partial)) { | |
78 | $doc_root = $_SERVER['DOCUMENT_ROOT']; | |
79 | $stationery_path = $doc_root."/".$stationery_path_partial; | |
80 | } | |
81 | ||
6a488035 TO |
82 | $margins = array($metric,$t,$r,$b,$l); |
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', | |
98 | '@<body>@siu', | |
99 | '@</body>@siu', | |
100 | '@<html[^>]*?>@siu', | |
101 | '@</html>@siu', | |
102 | '@<!DOCTYPE[^>]*?>@siu', | |
103 | ); | |
104 | $htmlElementsInstead = array('', '', '', '', '', ''); | |
105 | foreach ($pages as & $page) { | |
106 | $page = preg_replace($htmlElementstoStrip, | |
107 | $htmlElementsInstead, | |
108 | $page | |
109 | ); | |
110 | } | |
111 | // Glue the pages together | |
112 | $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages); | |
113 | $html .= " | |
114 | </div> | |
115 | </body> | |
116 | </html>"; | |
117 | if ($config->wkhtmltopdfPath) { | |
118 | return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName); | |
119 | } | |
120 | else { | |
bdfa67c3 | 121 | //return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName); |
122 | return self::_html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path); | |
6a488035 TO |
123 | } |
124 | } | |
125 | ||
bdfa67c3 | 126 | static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) { |
127 | // Documentation on the TCPDF library can be found at: http://www.tcpdf.org | |
128 | // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/ | |
129 | // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc | |
130 | require_once 'tcpdf/tcpdf.php'; | |
131 | require_once('FPDI/fpdi.php'); // This library is only in the 'packages' area as of version 4.5 | |
132 | ||
133 | $paper_size_arr = array( $paper_size[2], $paper_size[3]); | |
134 | ||
135 | // print_r( $paper_size_arr ); | |
136 | $pdf = new TCPDF($orientation, 'pt', $paper_size_arr, 'UTF-8', FALSE); | |
137 | $pdf->Open(); | |
138 | ||
139 | if (is_readable($stationery_path)){ | |
140 | $pdf->SetStationery( $stationery_path ); | |
141 | } | |
142 | ||
143 | $pdf->SetAuthor(''); | |
144 | $pdf->SetKeywords('CiviCRM.org'); | |
145 | $pdf->setPageUnit( $margins[0] ) ; | |
146 | $pdf->SetMargins($margins[4], $margins[1], $margins[2], true); | |
147 | ||
148 | $pdf->setJPEGQuality('100'); | |
149 | $pdf->SetAutoPageBreak(true, $margins[3]); | |
150 | ||
151 | $pdf->AddPage(); | |
152 | ||
153 | $ln = true ; | |
154 | $fill = false ; | |
155 | $reset_parm = false; | |
156 | $cell = false; | |
157 | $align = '' ; | |
158 | //print $html; | |
159 | // output the HTML content | |
160 | $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align); | |
161 | ||
162 | // reset pointer to the last page | |
163 | $pdf->lastPage(); | |
164 | ||
165 | // close and output the PDF | |
166 | $pdf->Close(); | |
167 | $pdf_file = 'CiviLetter'.'.pdf'; | |
168 | $pdf->Output($pdf_file, 'D'); | |
169 | CRM_Utils_System::civiExit(1); | |
170 | ||
171 | } | |
172 | ||
5bc392e6 EM |
173 | /** |
174 | * @param $paper_size | |
175 | * @param $orientation | |
176 | * @param $html | |
177 | * @param $output | |
178 | * @param $fileName | |
179 | * | |
180 | * @return string | |
181 | */ | |
6a488035 TO |
182 | static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) { |
183 | require_once 'packages/dompdf/dompdf_config.inc.php'; | |
184 | spl_autoload_register('DOMPDF_autoload'); | |
185 | $dompdf = new DOMPDF(); | |
186 | $dompdf->set_paper($paper_size, $orientation); | |
187 | $dompdf->load_html($html); | |
188 | $dompdf->render(); | |
189 | ||
190 | if ($output) { | |
191 | return $dompdf->output(); | |
192 | } | |
193 | else { | |
194 | $dompdf->stream($fileName); | |
195 | } | |
196 | } | |
197 | ||
5bc392e6 EM |
198 | /** |
199 | * @param $paper_size | |
200 | * @param $orientation | |
201 | * @param $margins | |
202 | * @param $html | |
203 | * @param $output | |
204 | * @param $fileName | |
205 | */ | |
6a488035 TO |
206 | static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) { |
207 | require_once 'packages/snappy/src/autoload.php'; | |
208 | $config = CRM_Core_Config::singleton(); | |
209 | $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath); | |
210 | $snappy->setOption("page-width", $paper_size[2] . "pt"); | |
211 | $snappy->setOption("page-height", $paper_size[3] . "pt"); | |
212 | $snappy->setOption("orientation", $orientation); | |
213 | $snappy->setOption("margin-top", $margins[1] . $margins[0]); | |
214 | $snappy->setOption("margin-right", $margins[2] . $margins[0]); | |
215 | $snappy->setOption("margin-bottom", $margins[3] . $margins[0]); | |
216 | $snappy->setOption("margin-left", $margins[4] . $margins[0]); | |
217 | $pdf = $snappy->getOutputFromHtml($html); | |
218 | if ($output) { | |
219 | return $pdf; | |
220 | } | |
221 | else { | |
222 | header('Content-Type: application/pdf'); | |
223 | header('Content-Disposition: attachment; filename="' . $fileName . '"'); | |
224 | echo $pdf; | |
225 | } | |
226 | } | |
227 | ||
228 | /* | |
229 | * function to convert value from one metric to another | |
230 | */ | |
5bc392e6 EM |
231 | /** |
232 | * @param $value | |
233 | * @param $from | |
234 | * @param $to | |
235 | * @param null $precision | |
236 | * | |
237 | * @return float|int | |
238 | */ | |
6a488035 TO |
239 | static function convertMetric($value, $from, $to, $precision = NULL) { |
240 | switch ($from . $to) { | |
241 | case 'incm': | |
242 | $value *= 2.54; | |
243 | break; | |
244 | ||
245 | case 'inmm': | |
246 | $value *= 25.4; | |
247 | break; | |
248 | ||
249 | case 'inpt': | |
250 | $value *= 72; | |
251 | break; | |
252 | ||
253 | case 'cmin': | |
254 | $value /= 2.54; | |
255 | break; | |
256 | ||
257 | case 'cmmm': | |
258 | $value *= 10; | |
259 | break; | |
260 | ||
261 | case 'cmpt': | |
262 | $value *= 72 / 2.54; | |
263 | break; | |
264 | ||
265 | case 'mmin': | |
266 | $value /= 25.4; | |
267 | break; | |
268 | ||
269 | case 'mmcm': | |
270 | $value /= 10; | |
271 | break; | |
272 | ||
273 | case 'mmpt': | |
274 | $value *= 72 / 25.4; | |
275 | break; | |
276 | ||
277 | case 'ptin': | |
278 | $value /= 72; | |
279 | break; | |
280 | ||
281 | case 'ptcm': | |
282 | $value *= 2.54 / 72; | |
283 | break; | |
284 | ||
285 | case 'ptmm': | |
286 | $value *= 25.4 / 72; | |
287 | break; | |
288 | } | |
289 | if (!is_null($precision)) { | |
290 | $value = round($value, $precision); | |
291 | } | |
292 | return $value; | |
293 | } | |
294 | ||
5bc392e6 EM |
295 | /** |
296 | * @param $fileName | |
297 | * @param $searchPath | |
298 | * @param $values | |
299 | * @param int $numPages | |
300 | * @param bool $echo | |
301 | * @param string $output | |
302 | * @param string $creator | |
303 | * @param string $author | |
304 | * @param string $title | |
305 | */ | |
6a488035 TO |
306 | static function &pdflib($fileName, |
307 | $searchPath, | |
308 | &$values, | |
309 | $numPages = 1, | |
310 | $echo = TRUE, | |
311 | $output = 'College_Match_App', | |
312 | $creator = 'CiviCRM', | |
313 | $author = 'http://www.civicrm.org/', | |
314 | $title = '2006 College Match Scholarship Application' | |
315 | ) { | |
316 | try { | |
317 | $pdf = new PDFlib(); | |
318 | $pdf->set_parameter("compatibility", "1.6"); | |
319 | $pdf->set_parameter("licensefile", "/home/paras/bin/license/pdflib.txt"); | |
320 | ||
321 | if ($pdf->begin_document('', '') == 0) { | |
322 | CRM_Core_Error::statusBounce("PDFlib Error: " . $pdf->get_errmsg()); | |
323 | } | |
324 | ||
325 | $config = CRM_Core_Config::singleton(); | |
326 | $pdf->set_parameter('resourcefile', $config->templateDir . '/Quest/pdf/pdflib.upr'); | |
327 | $pdf->set_parameter('textformat', 'utf8'); | |
328 | ||
329 | /* Set the search path for fonts and PDF files */ | |
330 | ||
331 | $pdf->set_parameter('SearchPath', $searchPath); | |
332 | ||
333 | /* This line is required to avoid problems on Japanese systems */ | |
334 | ||
335 | $pdf->set_parameter('hypertextencoding', 'winansi'); | |
336 | ||
337 | $pdf->set_info('Creator', $creator); | |
338 | $pdf->set_info('Author', $author); | |
339 | $pdf->set_info('Title', $title); | |
340 | ||
341 | $blockContainer = $pdf->open_pdi($fileName, '', 0); | |
342 | if ($blockContainer == 0) { | |
343 | CRM_Core_Error::statusBounce('PDFlib Error: ' . $pdf->get_errmsg()); | |
344 | } | |
345 | ||
346 | for ($i = 1; $i <= $numPages; $i++) { | |
347 | $page = $pdf->open_pdi_page($blockContainer, $i, ''); | |
348 | if ($page == 0) { | |
349 | CRM_Core_Error::statusBounce('PDFlib Error: ' . $pdf->get_errmsg()); | |
350 | } | |
351 | ||
352 | /* dummy page size */ | |
353 | $pdf->begin_page_ext(20, 20, ''); | |
354 | ||
355 | /* This will adjust the page size to the block container's size. */ | |
356 | ||
357 | $pdf->fit_pdi_page($page, 0, 0, 'adjustpage'); | |
358 | ||
359 | ||
360 | $status = array(); | |
361 | /* Fill all text blocks with dynamic data */ | |
362 | ||
363 | foreach ($values as $key => $value) { | |
364 | if (is_array($value)) { | |
365 | continue; | |
366 | } | |
367 | ||
368 | // pdflib does like the forward slash character, hence convert | |
369 | $value = str_replace('/', '_', $value); | |
370 | ||
371 | $res = $pdf->fill_textblock($page, | |
372 | $key, | |
373 | $value, | |
374 | 'embedding encoding=winansi' | |
375 | ); | |
376 | ||
377 | /** | |
378 | if ( $res == 0 ) { | |
379 | CRM_Core_Error::debug( "$key, $value: $res", $pdf->get_errmsg( ) ); | |
380 | } else { | |
381 | CRM_Core_Error::debug( "SUCCESS: $key, $value", null ); | |
382 | } | |
383 | **/ | |
384 | } | |
385 | ||
386 | $pdf->end_page_ext(''); | |
387 | $pdf->close_pdi_page($page); | |
388 | } | |
389 | ||
390 | $pdf->end_document(''); | |
391 | $pdf->close_pdi($blockContainer); | |
392 | ||
393 | $buf = $pdf->get_buffer(); | |
394 | $len = strlen($buf); | |
395 | ||
396 | if ($echo) { | |
397 | header('Content-type: application/pdf'); | |
398 | header("Content-Length: $len"); | |
399 | header("Content-Disposition: inline; filename={$output}.pdf"); | |
400 | echo $buf; | |
401 | CRM_Utils_System::civiExit(); | |
402 | } | |
403 | else { | |
404 | return $buf; | |
405 | } | |
406 | } | |
407 | catch(PDFlibException$excp) { | |
408 | CRM_Core_Error::statusBounce('PDFlib Error: Exception' . | |
409 | "[" . $excp->get_errnum() . "] " . $excp->get_apiname() . ": " . | |
410 | $excp->get_errmsg() | |
411 | ); | |
412 | } | |
413 | catch(Exception$excp) { | |
414 | CRM_Core_Error::statusBounce("PDFlib Error: " . $excp->get_errmsg()); | |
415 | } | |
416 | } | |
417 | } | |
418 |