Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | 27 | |
383b23e3 | 28 | |
0b6fe14b | 29 | use Dompdf\Dompdf; |
2256f347 | 30 | use Dompdf\Options; |
6714d8d2 | 31 | |
6a488035 TO |
32 | /** |
33 | * | |
34 | * @package CRM | |
6b83d5bd | 35 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 | 36 | */ |
0a48e6fb | 37 | |
6a488035 TO |
38 | class CRM_Utils_PDF_Utils { |
39 | ||
71b59e6d RR |
40 | public static function enqueuePDF($pdf) { |
41 | ||
42 | $fname = time().'_lp.pdf'; | |
43 | file_put_contents('/tmp/'.$fname, $pdf); | |
44 | header('Location: /civicrm/lp-setup?file='.$fname); | |
45 | exit; | |
46 | ||
47 | } | |
48 | ||
0a48e6fb LMM |
49 | public static function latex2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) { |
50 | /* FIXME: get $paper_size, $orientation, $margins */ | |
51 | ||
52 | if (is_array($text)) { | |
53 | $pages = &$text; | |
54 | } | |
55 | else { | |
56 | $pages = array($text); | |
57 | } | |
58 | ||
71b59e6d | 59 | $head='\documentclass[12pt]{letter} |
0a48e6fb LMM |
60 | \usepackage{url} |
61 | \usepackage{ucs} | |
a975b97b | 62 | \usepackage{array} |
0a48e6fb LMM |
63 | \usepackage{graphicx} |
64 | \usepackage[T1]{fontenc} | |
65 | \usepackage{fullpage} | |
71b59e6d RR |
66 | \usepackage{fontspec,xunicode} |
67 | %% VERY IMPORTANT. Configures supported languages and fonts to use for each one. | |
68 | \usepackage[Latin, Hebrew, Arabics, CJK, Diacritics]{ucharclasses} | |
a7a53b8c | 69 | \newfontfamily{\normalfont}[Ligatures=TeX]{FreeSerif} |
a975b97b RR |
70 | \newfontfamily{\cjkfont}{WenQuanYi Zen Hei} |
71 | \setDefaultTransitions{\normalfont}{} | |
72 | \setTransitionsForLatin{\normalfont}{} | |
73 | \setTransitionsForArabics{\normalfont}{} | |
74 | \setTransitionsForCJK{\cjkfont}{} | |
75 | \setTransitionsForDiacritics{\normalfont}{} | |
76 | \setTransitionTo{Hebrew}{\normalfont} | |
0aafca51 | 77 | \setmainfont{FreeSerif} |
0a48e6fb LMM |
78 | |
79 | \newcommand{\fsfclosing}[1]{\par\nobreak\vspace{\parskip} | |
80 | \stopbreaks | |
81 | \noindent | |
82 | \ifx\@empty\fromaddress\else | |
83 | \hspace*{\longindentation}\fi | |
84 | \parbox{\indentedwidth}{\raggedright | |
85 | \ignorespaces #1\\\\[1\medskipamount] | |
551d3ddb | 86 | \hspace*{-0.25in}\includegraphics[scale=1.0]{/var/www/html/sites/all/modules/civicrm/sigjohns.pdf} |
0a48e6fb LMM |
87 | \\\\ |
88 | ||
89 | \ifx\@empty\fromsig | |
90 | \fromname | |
91 | \else \fromsig \fi\strut} | |
92 | \par} | |
93 | \medskipamount=\parskip | |
94 | ||
0a48e6fb LMM |
95 | \pagestyle{empty} |
96 | \tolerance=8000 | |
97 | \address{\vspace{0.05in}} | |
98 | \signature{John Sullivan \\\\ Executive Director} | |
99 | \usepackage[ | |
71b59e6d RR |
100 | top = 1.5in, |
101 | bottom = 1.25in, | |
102 | left = 1.0in, | |
103 | right = 1.0in]{geometry} | |
0a48e6fb LMM |
104 | \begin{document} |
105 | '; | |
106 | $footer=' | |
107 | \end{document}'; | |
108 | ||
109 | $latex = $head; | |
110 | foreach ($pages as $page) { | |
111 | $latex.=$page; | |
112 | } | |
113 | $latex.=$footer; | |
114 | ||
115 | $descriptorspec = array( | |
116 | 0 => array("pipe", "r"), | |
117 | 1 => array("pipe", "w") | |
118 | ); | |
119 | ||
120 | ||
121 | ||
122 | $process = proc_open("/usr/local/bin/pdflatex_wrapper.sh", $descriptorspec, $pipes); | |
123 | ||
124 | ||
125 | if (is_resource($process)) { | |
126 | fwrite($pipes[0], $latex); | |
127 | fclose($pipes[0]); | |
128 | ||
129 | $pdf = stream_get_contents($pipes[1]); | |
130 | fclose($pipes[1]); | |
131 | } else { | |
132 | CRM_Core_Error::debug_log_message("ERROR creating PDF. Check /tmp/pdflatex_*"); | |
133 | } | |
134 | ||
135 | if ($output) { | |
136 | return $pdf; | |
137 | } | |
138 | else { | |
139 | header('Content-Type: application/pdf'); | |
140 | header('Content-Disposition: attachment; filename="' . $fileName . '"'); | |
141 | echo $pdf; | |
a975b97b RR |
142 | // quidam: comment previous line and uncomment next one during printing |
143 | //CRM_Utils_PDF_Utils::enqueuePDF($pdf); | |
71b59e6d | 144 | |
0a48e6fb | 145 | } |
71b59e6d | 146 | } |
0a48e6fb | 147 | |
5bc392e6 | 148 | /** |
4f308883 TO |
149 | * @param array $text |
150 | * List of HTML snippets. | |
5bc392e6 | 151 | * @param string $fileName |
3ed92c14 TO |
152 | * The logical filename to display. |
153 | * Ex: "HelloWorld.pdf". | |
5bc392e6 | 154 | * @param bool $output |
3ed92c14 | 155 | * FALSE to display PDF. TRUE to return as string. |
5bc392e6 | 156 | * @param null $pdfFormat |
3ed92c14 | 157 | * Unclear. Possibly PdfFormat or formValues. |
5bc392e6 EM |
158 | * |
159 | * @return string|void | |
160 | */ | |
56d5847d | 161 | public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) { |
6a488035 TO |
162 | if (is_array($text)) { |
163 | $pages = &$text; | |
164 | } | |
165 | else { | |
be2fb01f | 166 | $pages = [$text]; |
6a488035 TO |
167 | } |
168 | // Get PDF Page Format | |
169 | $format = CRM_Core_BAO_PdfFormat::getDefaultValues(); | |
170 | if (is_array($pdfFormat)) { | |
171 | // PDF Page Format parameters passed in | |
172 | $format = array_merge($format, $pdfFormat); | |
173 | } | |
18a3e0c0 | 174 | elseif (!empty($pdfFormat)) { |
6a488035 TO |
175 | // PDF Page Format ID passed in |
176 | $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat); | |
177 | } | |
353ffa53 TO |
178 | $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']); |
179 | $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt'); | |
6a488035 TO |
180 | $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt'); |
181 | // dompdf requires dimensions in points | |
be2fb01f | 182 | $paper_size = [0, 0, $paper_width, $paper_height]; |
6a488035 | 183 | $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format); |
353ffa53 TO |
184 | $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format); |
185 | $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format); | |
186 | $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format); | |
187 | $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format); | |
188 | $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format); | |
bdfa67c3 | 189 | |
be2fb01f | 190 | $margins = [$metric, $t, $r, $b, $l]; |
6a488035 | 191 | |
b0500874 | 192 | // Add a special region for the HTML header of PDF files: |
7419f31d | 193 | $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE); |
b0500874 AH |
194 | $htmlHeader = ($pdfHeaderRegion) ? $pdfHeaderRegion->render('', FALSE) : ''; |
195 | ||
6a488035 TO |
196 | $html = " |
197 | <html> | |
198 | <head> | |
199 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> | |
200 | <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style> | |
56d5847d | 201 | <style type=\"text/css\">@import url(" . CRM_Core_Config::singleton()->userFrameworkResourceURL . "css/print.css);</style> |
b0500874 | 202 | {$htmlHeader} |
6a488035 TO |
203 | </head> |
204 | <body> | |
205 | <div id=\"crm-container\">\n"; | |
206 | ||
207 | // Strip <html>, <header>, and <body> tags from each page | |
56d5847d | 208 | $htmlElementstoStrip = [ |
6a488035 | 209 | '@<head[^>]*?>.*?</head>@siu', |
8e637fa3 | 210 | '@<script[^>]*?>.*?</script>@siu', |
6a488035 TO |
211 | '@<body>@siu', |
212 | '@</body>@siu', | |
213 | '@<html[^>]*?>@siu', | |
214 | '@</html>@siu', | |
215 | '@<!DOCTYPE[^>]*?>@siu', | |
56d5847d | 216 | ]; |
217 | $htmlElementsInstead = ['', '', '', '', '', '']; | |
6a488035 TO |
218 | foreach ($pages as & $page) { |
219 | $page = preg_replace($htmlElementstoStrip, | |
220 | $htmlElementsInstead, | |
221 | $page | |
222 | ); | |
223 | } | |
224 | // Glue the pages together | |
225 | $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages); | |
226 | $html .= " | |
227 | </div> | |
228 | </body> | |
229 | </html>"; | |
56d5847d | 230 | if (CRM_Core_Config::singleton()->wkhtmltopdfPath) { |
6a488035 TO |
231 | return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName); |
232 | } | |
233 | else { | |
8d7ae5ee | 234 | return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName); |
6a488035 TO |
235 | } |
236 | } | |
237 | ||
2e2605fe EM |
238 | /** |
239 | * Convert html to tcpdf. | |
240 | * | |
241 | * @param $paper_size | |
242 | * @param $orientation | |
243 | * @param $margins | |
244 | * @param $html | |
245 | * @param $output | |
246 | * @param $fileName | |
247 | * @param $stationery_path | |
248 | */ | |
00be9182 | 249 | public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) { |
bdfa67c3 | 250 | // Documentation on the TCPDF library can be found at: http://www.tcpdf.org |
251 | // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/ | |
252 | // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc | |
253 | require_once 'tcpdf/tcpdf.php'; | |
6714d8d2 SL |
254 | // This library is only in the 'packages' area as of version 4.5 |
255 | require_once 'FPDI/fpdi.php'; | |
bdfa67c3 | 256 | |
be2fb01f | 257 | $paper_size_arr = [$paper_size[2], $paper_size[3]]; |
bdfa67c3 | 258 | |
8e637fa3 | 259 | $pdf = new TCPDF($orientation, 'pt', $paper_size_arr); |
bdfa67c3 | 260 | $pdf->Open(); |
261 | ||
9b873358 | 262 | if (is_readable($stationery_path)) { |
481a74f4 | 263 | $pdf->SetStationery($stationery_path); |
bdfa67c3 | 264 | } |
265 | ||
266 | $pdf->SetAuthor(''); | |
267 | $pdf->SetKeywords('CiviCRM.org'); | |
481a74f4 | 268 | $pdf->setPageUnit($margins[0]); |
e7292422 | 269 | $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE); |
bdfa67c3 | 270 | |
271 | $pdf->setJPEGQuality('100'); | |
e7292422 | 272 | $pdf->SetAutoPageBreak(TRUE, $margins[3]); |
bdfa67c3 | 273 | |
274 | $pdf->AddPage(); | |
275 | ||
e7292422 TO |
276 | $ln = TRUE; |
277 | $fill = FALSE; | |
278 | $reset_parm = FALSE; | |
279 | $cell = FALSE; | |
280 | $align = ''; | |
8e637fa3 | 281 | |
bdfa67c3 | 282 | // output the HTML content |
283 | $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align); | |
284 | ||
285 | // reset pointer to the last page | |
286 | $pdf->lastPage(); | |
287 | ||
288 | // close and output the PDF | |
289 | $pdf->Close(); | |
92fcb95f | 290 | $pdf_file = 'CiviLetter' . '.pdf'; |
bdfa67c3 | 291 | $pdf->Output($pdf_file, 'D'); |
292c8687 | 292 | CRM_Utils_System::civiExit(); |
bdfa67c3 | 293 | } |
294 | ||
5bc392e6 EM |
295 | /** |
296 | * @param $paper_size | |
297 | * @param $orientation | |
298 | * @param $html | |
299 | * @param $output | |
100fef9d | 300 | * @param string $fileName |
5bc392e6 EM |
301 | * |
302 | * @return string | |
303 | */ | |
00be9182 | 304 | public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) { |
2256f347 SL |
305 | // CRM-12165 - Remote file support required for image handling. |
306 | $options = new Options(); | |
307 | $options->set('isRemoteEnabled', TRUE); | |
308 | ||
309 | $dompdf = new DOMPDF($options); | |
6a488035 TO |
310 | $dompdf->set_paper($paper_size, $orientation); |
311 | $dompdf->load_html($html); | |
312 | $dompdf->render(); | |
313 | ||
314 | if ($output) { | |
315 | return $dompdf->output(); | |
316 | } | |
317 | else { | |
53a39759 | 318 | // CRM-19183 remove .pdf extension from filename |
319 | $fileName = basename($fileName, ".pdf"); | |
6a488035 TO |
320 | $dompdf->stream($fileName); |
321 | } | |
322 | } | |
323 | ||
5bc392e6 EM |
324 | /** |
325 | * @param $paper_size | |
326 | * @param $orientation | |
327 | * @param $margins | |
328 | * @param $html | |
329 | * @param $output | |
100fef9d | 330 | * @param string $fileName |
5bc392e6 | 331 | */ |
00be9182 | 332 | public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) { |
6a488035 TO |
333 | require_once 'packages/snappy/src/autoload.php'; |
334 | $config = CRM_Core_Config::singleton(); | |
335 | $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath); | |
336 | $snappy->setOption("page-width", $paper_size[2] . "pt"); | |
337 | $snappy->setOption("page-height", $paper_size[3] . "pt"); | |
338 | $snappy->setOption("orientation", $orientation); | |
339 | $snappy->setOption("margin-top", $margins[1] . $margins[0]); | |
340 | $snappy->setOption("margin-right", $margins[2] . $margins[0]); | |
341 | $snappy->setOption("margin-bottom", $margins[3] . $margins[0]); | |
342 | $snappy->setOption("margin-left", $margins[4] . $margins[0]); | |
71b59e6d | 343 | $html = preg_replace('/{ }/', ' ', $html); |
6a488035 TO |
344 | $pdf = $snappy->getOutputFromHtml($html); |
345 | if ($output) { | |
346 | return $pdf; | |
347 | } | |
348 | else { | |
d42a224c CW |
349 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf'); |
350 | CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"'); | |
6a488035 | 351 | echo $pdf; |
a975b97b | 352 | // quidam: comment previous line and uncomment next one during printing |
71b59e6d RR |
353 | //CRM_Utils_PDF_Utils::enqueuePDF($pdf); |
354 | ||
6a488035 TO |
355 | } |
356 | } | |
357 | ||
5bc392e6 | 358 | /** |
fe482240 | 359 | * convert value from one metric to another. |
d424ffde | 360 | * |
5bc392e6 EM |
361 | * @param $value |
362 | * @param $from | |
363 | * @param $to | |
364 | * @param null $precision | |
365 | * | |
366 | * @return float|int | |
367 | */ | |
00be9182 | 368 | public static function convertMetric($value, $from, $to, $precision = NULL) { |
6a488035 TO |
369 | switch ($from . $to) { |
370 | case 'incm': | |
371 | $value *= 2.54; | |
372 | break; | |
373 | ||
374 | case 'inmm': | |
375 | $value *= 25.4; | |
376 | break; | |
377 | ||
378 | case 'inpt': | |
379 | $value *= 72; | |
380 | break; | |
381 | ||
382 | case 'cmin': | |
383 | $value /= 2.54; | |
384 | break; | |
385 | ||
386 | case 'cmmm': | |
387 | $value *= 10; | |
388 | break; | |
389 | ||
390 | case 'cmpt': | |
391 | $value *= 72 / 2.54; | |
392 | break; | |
393 | ||
394 | case 'mmin': | |
395 | $value /= 25.4; | |
396 | break; | |
397 | ||
398 | case 'mmcm': | |
399 | $value /= 10; | |
400 | break; | |
401 | ||
402 | case 'mmpt': | |
403 | $value *= 72 / 25.4; | |
404 | break; | |
405 | ||
406 | case 'ptin': | |
407 | $value /= 72; | |
408 | break; | |
409 | ||
410 | case 'ptcm': | |
411 | $value *= 2.54 / 72; | |
412 | break; | |
413 | ||
414 | case 'ptmm': | |
415 | $value *= 25.4 / 72; | |
416 | break; | |
417 | } | |
418 | if (!is_null($precision)) { | |
419 | $value = round($value, $precision); | |
420 | } | |
421 | return $value; | |
422 | } | |
423 | ||
6a488035 | 424 | } |