Merge pull request #15268 from mlutfy/partialFixDrupal8q-518
[civicrm-core.git] / CRM / Utils / PDF / Utils.php
CommitLineData
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 29use Dompdf\Dompdf;
2256f347 30use Dompdf\Options;
6714d8d2 31
6a488035
TO
32/**
33 *
34 * @package CRM
6b83d5bd 35 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
36 */
37class CRM_Utils_PDF_Utils {
38
5bc392e6 39 /**
4f308883
TO
40 * @param array $text
41 * List of HTML snippets.
5bc392e6 42 * @param string $fileName
3ed92c14
TO
43 * The logical filename to display.
44 * Ex: "HelloWorld.pdf".
5bc392e6 45 * @param bool $output
3ed92c14 46 * FALSE to display PDF. TRUE to return as string.
5bc392e6 47 * @param null $pdfFormat
3ed92c14 48 * Unclear. Possibly PdfFormat or formValues.
5bc392e6
EM
49 *
50 * @return string|void
51 */
56d5847d 52 public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
6a488035
TO
53 if (is_array($text)) {
54 $pages = &$text;
55 }
56 else {
be2fb01f 57 $pages = [$text];
6a488035
TO
58 }
59 // Get PDF Page Format
60 $format = CRM_Core_BAO_PdfFormat::getDefaultValues();
61 if (is_array($pdfFormat)) {
62 // PDF Page Format parameters passed in
63 $format = array_merge($format, $pdfFormat);
64 }
18a3e0c0 65 elseif (!empty($pdfFormat)) {
6a488035
TO
66 // PDF Page Format ID passed in
67 $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat);
68 }
353ffa53
TO
69 $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
70 $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
6a488035
TO
71 $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
72 // dompdf requires dimensions in points
be2fb01f 73 $paper_size = [0, 0, $paper_width, $paper_height];
6a488035 74 $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format);
353ffa53
TO
75 $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
76 $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format);
77 $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format);
78 $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format);
79 $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format);
bdfa67c3 80
be2fb01f 81 $margins = [$metric, $t, $r, $b, $l];
6a488035 82
b0500874 83 // Add a special region for the HTML header of PDF files:
7419f31d 84 $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE);
b0500874
AH
85 $htmlHeader = ($pdfHeaderRegion) ? $pdfHeaderRegion->render('', FALSE) : '';
86
6a488035
TO
87 $html = "
88<html>
89 <head>
90 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
91 <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>
56d5847d 92 <style type=\"text/css\">@import url(" . CRM_Core_Config::singleton()->userFrameworkResourceURL . "css/print.css);</style>
b0500874 93 {$htmlHeader}
6a488035
TO
94 </head>
95 <body>
96 <div id=\"crm-container\">\n";
97
98 // Strip <html>, <header>, and <body> tags from each page
56d5847d 99 $htmlElementstoStrip = [
6a488035 100 '@<head[^>]*?>.*?</head>@siu',
8e637fa3 101 '@<script[^>]*?>.*?</script>@siu',
6a488035
TO
102 '@<body>@siu',
103 '@</body>@siu',
104 '@<html[^>]*?>@siu',
105 '@</html>@siu',
106 '@<!DOCTYPE[^>]*?>@siu',
56d5847d 107 ];
108 $htmlElementsInstead = ['', '', '', '', '', ''];
6a488035
TO
109 foreach ($pages as & $page) {
110 $page = preg_replace($htmlElementstoStrip,
111 $htmlElementsInstead,
112 $page
113 );
114 }
115 // Glue the pages together
116 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
117 $html .= "
118 </div>
119 </body>
120</html>";
56d5847d 121 if (CRM_Core_Config::singleton()->wkhtmltopdfPath) {
6a488035
TO
122 return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
123 }
124 else {
8d7ae5ee 125 return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
6a488035
TO
126 }
127 }
128
2e2605fe
EM
129 /**
130 * Convert html to tcpdf.
131 *
132 * @param $paper_size
133 * @param $orientation
134 * @param $margins
135 * @param $html
136 * @param $output
137 * @param $fileName
138 * @param $stationery_path
139 */
00be9182 140 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
bdfa67c3 141 // Documentation on the TCPDF library can be found at: http://www.tcpdf.org
142 // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/
143 // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc
144 require_once 'tcpdf/tcpdf.php';
6714d8d2
SL
145 // This library is only in the 'packages' area as of version 4.5
146 require_once 'FPDI/fpdi.php';
bdfa67c3 147
be2fb01f 148 $paper_size_arr = [$paper_size[2], $paper_size[3]];
bdfa67c3 149
8e637fa3 150 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
bdfa67c3 151 $pdf->Open();
152
9b873358 153 if (is_readable($stationery_path)) {
481a74f4 154 $pdf->SetStationery($stationery_path);
bdfa67c3 155 }
156
157 $pdf->SetAuthor('');
158 $pdf->SetKeywords('CiviCRM.org');
481a74f4 159 $pdf->setPageUnit($margins[0]);
e7292422 160 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
bdfa67c3 161
162 $pdf->setJPEGQuality('100');
e7292422 163 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
bdfa67c3 164
165 $pdf->AddPage();
166
e7292422
TO
167 $ln = TRUE;
168 $fill = FALSE;
169 $reset_parm = FALSE;
170 $cell = FALSE;
171 $align = '';
8e637fa3 172
bdfa67c3 173 // output the HTML content
174 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
175
176 // reset pointer to the last page
177 $pdf->lastPage();
178
179 // close and output the PDF
180 $pdf->Close();
92fcb95f 181 $pdf_file = 'CiviLetter' . '.pdf';
bdfa67c3 182 $pdf->Output($pdf_file, 'D');
292c8687 183 CRM_Utils_System::civiExit();
bdfa67c3 184 }
185
5bc392e6
EM
186 /**
187 * @param $paper_size
188 * @param $orientation
189 * @param $html
190 * @param $output
100fef9d 191 * @param string $fileName
5bc392e6
EM
192 *
193 * @return string
194 */
00be9182 195 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
2256f347
SL
196 // CRM-12165 - Remote file support required for image handling.
197 $options = new Options();
198 $options->set('isRemoteEnabled', TRUE);
199
200 $dompdf = new DOMPDF($options);
6a488035
TO
201 $dompdf->set_paper($paper_size, $orientation);
202 $dompdf->load_html($html);
203 $dompdf->render();
204
205 if ($output) {
206 return $dompdf->output();
207 }
208 else {
53a39759 209 // CRM-19183 remove .pdf extension from filename
210 $fileName = basename($fileName, ".pdf");
6a488035
TO
211 $dompdf->stream($fileName);
212 }
213 }
214
5bc392e6
EM
215 /**
216 * @param $paper_size
217 * @param $orientation
218 * @param $margins
219 * @param $html
220 * @param $output
100fef9d 221 * @param string $fileName
5bc392e6 222 */
00be9182 223 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
6a488035
TO
224 require_once 'packages/snappy/src/autoload.php';
225 $config = CRM_Core_Config::singleton();
226 $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath);
227 $snappy->setOption("page-width", $paper_size[2] . "pt");
228 $snappy->setOption("page-height", $paper_size[3] . "pt");
229 $snappy->setOption("orientation", $orientation);
230 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
231 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
232 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
233 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
234 $pdf = $snappy->getOutputFromHtml($html);
235 if ($output) {
236 return $pdf;
237 }
238 else {
d42a224c
CW
239 CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf');
240 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
6a488035
TO
241 echo $pdf;
242 }
243 }
244
5bc392e6 245 /**
fe482240 246 * convert value from one metric to another.
d424ffde 247 *
5bc392e6
EM
248 * @param $value
249 * @param $from
250 * @param $to
251 * @param null $precision
252 *
253 * @return float|int
254 */
00be9182 255 public static function convertMetric($value, $from, $to, $precision = NULL) {
6a488035
TO
256 switch ($from . $to) {
257 case 'incm':
258 $value *= 2.54;
259 break;
260
261 case 'inmm':
262 $value *= 25.4;
263 break;
264
265 case 'inpt':
266 $value *= 72;
267 break;
268
269 case 'cmin':
270 $value /= 2.54;
271 break;
272
273 case 'cmmm':
274 $value *= 10;
275 break;
276
277 case 'cmpt':
278 $value *= 72 / 2.54;
279 break;
280
281 case 'mmin':
282 $value /= 25.4;
283 break;
284
285 case 'mmcm':
286 $value /= 10;
287 break;
288
289 case 'mmpt':
290 $value *= 72 / 25.4;
291 break;
292
293 case 'ptin':
294 $value /= 72;
295 break;
296
297 case 'ptcm':
298 $value *= 2.54 / 72;
299 break;
300
301 case 'ptmm':
302 $value *= 25.4 / 72;
303 break;
304 }
305 if (!is_null($precision)) {
306 $value = round($value, $precision);
307 }
308 return $value;
309 }
310
6a488035 311}