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