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