APIv4 - Add Address::getCoordinates action
[civicrm-core.git] / CRM / Utils / PDF / Utils.php
CommitLineData
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 13use Dompdf\Dompdf;
2256f347 14use Dompdf\Options;
6714d8d2 15
6a488035
TO
16/**
17 *
18 * @package CRM
ca5cec67 19 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
20 */
21class CRM_Utils_PDF_Utils {
22
5bc392e6 23 /**
4f308883
TO
24 * @param array $text
25 * List of HTML snippets.
5bc392e6 26 * @param string $fileName
3ed92c14
TO
27 * The logical filename to display.
28 * Ex: "HelloWorld.pdf".
5bc392e6 29 * @param bool $output
3ed92c14 30 * FALSE to display PDF. TRUE to return as string.
a2f24340 31 * @param array|int|null $pdfFormat
3ed92c14 32 * Unclear. Possibly PdfFormat or formValues.
5bc392e6
EM
33 *
34 * @return string|void
35 */
56d5847d 36 public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
6a488035
TO
37 if (is_array($text)) {
38 $pages = &$text;
39 }
40 else {
be2fb01f 41 $pages = [$text];
6a488035
TO
42 }
43 // Get PDF Page Format
44 $format = CRM_Core_BAO_PdfFormat::getDefaultValues();
45 if (is_array($pdfFormat)) {
46 // PDF Page Format parameters passed in
47 $format = array_merge($format, $pdfFormat);
48 }
18a3e0c0 49 elseif (!empty($pdfFormat)) {
6a488035
TO
50 // PDF Page Format ID passed in
51 $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat);
52 }
353ffa53
TO
53 $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
54 $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
6a488035
TO
55 $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
56 // dompdf requires dimensions in points
be2fb01f 57 $paper_size = [0, 0, $paper_width, $paper_height];
6a488035 58 $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format);
353ffa53
TO
59 $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
60 $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format);
61 $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format);
62 $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format);
63 $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format);
bdfa67c3 64
be2fb01f 65 $margins = [$metric, $t, $r, $b, $l];
6a488035 66
b0500874 67 // Add a special region for the HTML header of PDF files:
7419f31d 68 $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE);
b0500874
AH
69 $htmlHeader = ($pdfHeaderRegion) ? $pdfHeaderRegion->render('', FALSE) : '';
70
6a488035
TO
71 $html = "
72<html>
73 <head>
74 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
75 <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>
56d5847d 76 <style type=\"text/css\">@import url(" . CRM_Core_Config::singleton()->userFrameworkResourceURL . "css/print.css);</style>
b0500874 77 {$htmlHeader}
6a488035
TO
78 </head>
79 <body>
80 <div id=\"crm-container\">\n";
81
82 // Strip <html>, <header>, and <body> tags from each page
fe276fa5 83
56d5847d 84 $htmlElementstoStrip = [
fe276fa5
J
85 '<head[^>]*?>.*?</head>',
86 '<script[^>]*?>.*?</script>',
87 '<body>',
88 '</body>',
89 '<html[^>]*?>',
90 '</html>',
91 '<!DOCTYPE[^>]*?>',
56d5847d 92 ];
6a488035 93 foreach ($pages as & $page) {
fe276fa5
J
94 foreach ($htmlElementstoStrip as $pattern) {
95 $page = mb_eregi_replace($pattern, '', $page);
96 }
6a488035
TO
97 }
98 // Glue the pages together
99 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
100 $html .= "
101 </div>
102 </body>
103</html>";
56d5847d 104 if (CRM_Core_Config::singleton()->wkhtmltopdfPath) {
6a488035
TO
105 return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
106 }
107 else {
8d7ae5ee 108 return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
6a488035
TO
109 }
110 }
111
2e2605fe
EM
112 /**
113 * Convert html to tcpdf.
114 *
0677f5a4 115 * @deprecated
2e2605fe
EM
116 * @param $paper_size
117 * @param $orientation
118 * @param $margins
119 * @param $html
120 * @param $output
121 * @param $fileName
122 * @param $stationery_path
123 */
00be9182 124 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
f713b8b6
SL
125 CRM_Core_Error::deprecatedFunctionWarning('CRM_Utils_PDF::_html2pdf_dompdf');
126 return self::_html2pdf_dompdf($paper_size, $orientation, $margins, $html, $output, $fileName);
bdfa67c3 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';
6714d8d2
SL
131 // This library is only in the 'packages' area as of version 4.5
132 require_once 'FPDI/fpdi.php';
bdfa67c3 133
be2fb01f 134 $paper_size_arr = [$paper_size[2], $paper_size[3]];
bdfa67c3 135
8e637fa3 136 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
bdfa67c3 137 $pdf->Open();
138
9b873358 139 if (is_readable($stationery_path)) {
481a74f4 140 $pdf->SetStationery($stationery_path);
bdfa67c3 141 }
142
143 $pdf->SetAuthor('');
144 $pdf->SetKeywords('CiviCRM.org');
481a74f4 145 $pdf->setPageUnit($margins[0]);
e7292422 146 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
bdfa67c3 147
148 $pdf->setJPEGQuality('100');
e7292422 149 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
bdfa67c3 150
151 $pdf->AddPage();
152
e7292422
TO
153 $ln = TRUE;
154 $fill = FALSE;
155 $reset_parm = FALSE;
156 $cell = FALSE;
157 $align = '';
8e637fa3 158
bdfa67c3 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();
92fcb95f 167 $pdf_file = 'CiviLetter' . '.pdf';
bdfa67c3 168 $pdf->Output($pdf_file, 'D');
292c8687 169 CRM_Utils_System::civiExit();
bdfa67c3 170 }
171
5bc392e6
EM
172 /**
173 * @param $paper_size
174 * @param $orientation
175 * @param $html
176 * @param $output
100fef9d 177 * @param string $fileName
5bc392e6
EM
178 *
179 * @return string
180 */
00be9182 181 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
442be3b9 182 $options = self::getDompdfOptions();
2256f347 183 $dompdf = new DOMPDF($options);
6a488035
TO
184 $dompdf->set_paper($paper_size, $orientation);
185 $dompdf->load_html($html);
186 $dompdf->render();
187
188 if ($output) {
189 return $dompdf->output();
190 }
40022216 191 // CRM-19183 remove .pdf extension from filename
192 $fileName = basename($fileName, ".pdf");
cb66edd5 193 if (CIVICRM_UF === 'UnitTests' && headers_sent()) {
194 // Streaming content will 'die' in unit tests unless ob_start()
195 // has been called.
40022216 196 throw new CRM_Core_Exception_PrematureExitException('_html2pdf_dompdf called', [
197 'html' => $html,
198 'fileName' => $fileName,
199 ]);
6a488035 200 }
40022216 201 $dompdf->stream($fileName);
6a488035
TO
202 }
203
5bc392e6 204 /**
2024d5b9 205 * @param float|int[] $paper_size
a2f24340
BT
206 * @param string $orientation
207 * @param array $margins
208 * @param string $html
209 * @param bool $output
100fef9d 210 * @param string $fileName
5bc392e6 211 */
00be9182 212 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
6bbe0cf6 213 require_once 'snappy/src/autoload.php';
f5f63246 214 $config = CRM_Core_Config::singleton();
6a488035
TO
215 $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath);
216 $snappy->setOption("page-width", $paper_size[2] . "pt");
217 $snappy->setOption("page-height", $paper_size[3] . "pt");
218 $snappy->setOption("orientation", $orientation);
219 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
220 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
221 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
222 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
223 $pdf = $snappy->getOutputFromHtml($html);
224 if ($output) {
225 return $pdf;
226 }
227 else {
d42a224c
CW
228 CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf');
229 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
6a488035
TO
230 echo $pdf;
231 }
232 }
233
5bc392e6 234 /**
fe482240 235 * convert value from one metric to another.
d424ffde 236 *
a2f24340
BT
237 * @param int $value
238 * @param string $from
239 * @param string $to
240 * @param int|null $precision
5bc392e6
EM
241 *
242 * @return float|int
243 */
00be9182 244 public static function convertMetric($value, $from, $to, $precision = NULL) {
6a488035
TO
245 switch ($from . $to) {
246 case 'incm':
247 $value *= 2.54;
248 break;
249
250 case 'inmm':
251 $value *= 25.4;
252 break;
253
254 case 'inpt':
255 $value *= 72;
256 break;
257
258 case 'cmin':
259 $value /= 2.54;
260 break;
261
262 case 'cmmm':
263 $value *= 10;
264 break;
265
266 case 'cmpt':
267 $value *= 72 / 2.54;
268 break;
269
270 case 'mmin':
271 $value /= 25.4;
272 break;
273
274 case 'mmcm':
275 $value /= 10;
276 break;
277
278 case 'mmpt':
279 $value *= 72 / 25.4;
280 break;
281
282 case 'ptin':
283 $value /= 72;
284 break;
285
286 case 'ptcm':
287 $value *= 2.54 / 72;
288 break;
289
290 case 'ptmm':
291 $value *= 25.4 / 72;
292 break;
293 }
294 if (!is_null($precision)) {
295 $value = round($value, $precision);
296 }
297 return $value;
298 }
299
442be3b9 300 /**
301 * Allow setting some dompdf options.
302 *
303 * We don't support all the available dompdf options.
304 *
305 * @return \Dompdf\Options
306 */
307 private static function getDompdfOptions(): Options {
308 $options = new Options();
309 $settings = [
310 // CRM-12165 - Remote file support required for image handling so default to TRUE
311 'enable_remote' => \Civi::settings()->get('dompdf_enable_remote') ?? TRUE,
312 ];
313 // only set these ones if a setting exists for them
314 foreach (['font_dir', 'chroot', 'log_output_file'] as $setting) {
315 $value = \Civi::settings()->get("dompdf_$setting");
316 if (isset($value)) {
317 $settings[$setting] = $value;
318 }
319 }
320 $options->set($settings);
321 return $options;
322 }
323
6a488035 324}