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