Merge pull request #15813 from eileenmcnaughton/fee
[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 $htmlElementstoStrip = [
84 '@<head[^>]*?>.*?</head>@siu',
85 '@<script[^>]*?>.*?</script>@siu',
86 '@<body>@siu',
87 '@</body>@siu',
88 '@<html[^>]*?>@siu',
89 '@</html>@siu',
90 '@<!DOCTYPE[^>]*?>@siu',
91 ];
92 $htmlElementsInstead = ['', '', '', '', '', ''];
93 foreach ($pages as & $page) {
94 $page = preg_replace($htmlElementstoStrip,
95 $htmlElementsInstead,
96 $page
97 );
98 }
99 // Glue the pages together
100 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
101 $html .= "
102 </div>
103 </body>
104 </html>";
105 if (CRM_Core_Config::singleton()->wkhtmltopdfPath) {
106 return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
107 }
108 else {
109 return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
110 }
111 }
112
113 /**
114 * Convert html to tcpdf.
115 *
116 * @param $paper_size
117 * @param $orientation
118 * @param $margins
119 * @param $html
120 * @param $output
121 * @param $fileName
122 * @param $stationery_path
123 */
124 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
125 // Documentation on the TCPDF library can be found at: http://www.tcpdf.org
126 // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/
127 // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc
128 require_once 'tcpdf/tcpdf.php';
129 // This library is only in the 'packages' area as of version 4.5
130 require_once 'FPDI/fpdi.php';
131
132 $paper_size_arr = [$paper_size[2], $paper_size[3]];
133
134 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
135 $pdf->Open();
136
137 if (is_readable($stationery_path)) {
138 $pdf->SetStationery($stationery_path);
139 }
140
141 $pdf->SetAuthor('');
142 $pdf->SetKeywords('CiviCRM.org');
143 $pdf->setPageUnit($margins[0]);
144 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
145
146 $pdf->setJPEGQuality('100');
147 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
148
149 $pdf->AddPage();
150
151 $ln = TRUE;
152 $fill = FALSE;
153 $reset_parm = FALSE;
154 $cell = FALSE;
155 $align = '';
156
157 // output the HTML content
158 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
159
160 // reset pointer to the last page
161 $pdf->lastPage();
162
163 // close and output the PDF
164 $pdf->Close();
165 $pdf_file = 'CiviLetter' . '.pdf';
166 $pdf->Output($pdf_file, 'D');
167 CRM_Utils_System::civiExit();
168 }
169
170 /**
171 * @param $paper_size
172 * @param $orientation
173 * @param $html
174 * @param $output
175 * @param string $fileName
176 *
177 * @return string
178 */
179 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
180 // CRM-12165 - Remote file support required for image handling.
181 $options = new Options();
182 $options->set('isRemoteEnabled', TRUE);
183
184 $dompdf = new DOMPDF($options);
185 $dompdf->set_paper($paper_size, $orientation);
186 $dompdf->load_html($html);
187 $dompdf->render();
188
189 if ($output) {
190 return $dompdf->output();
191 }
192 else {
193 // CRM-19183 remove .pdf extension from filename
194 $fileName = basename($fileName, ".pdf");
195 $dompdf->stream($fileName);
196 }
197 }
198
199 /**
200 * @param $paper_size
201 * @param $orientation
202 * @param $margins
203 * @param $html
204 * @param $output
205 * @param string $fileName
206 */
207 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
208 require_once 'packages/snappy/src/autoload.php';
209 $config = CRM_Core_Config::singleton();
210 $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath);
211 $snappy->setOption("page-width", $paper_size[2] . "pt");
212 $snappy->setOption("page-height", $paper_size[3] . "pt");
213 $snappy->setOption("orientation", $orientation);
214 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
215 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
216 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
217 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
218 $pdf = $snappy->getOutputFromHtml($html);
219 if ($output) {
220 return $pdf;
221 }
222 else {
223 CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf');
224 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
225 echo $pdf;
226 }
227 }
228
229 /**
230 * convert value from one metric to another.
231 *
232 * @param $value
233 * @param $from
234 * @param $to
235 * @param null $precision
236 *
237 * @return float|int
238 */
239 public static function convertMetric($value, $from, $to, $precision = NULL) {
240 switch ($from . $to) {
241 case 'incm':
242 $value *= 2.54;
243 break;
244
245 case 'inmm':
246 $value *= 25.4;
247 break;
248
249 case 'inpt':
250 $value *= 72;
251 break;
252
253 case 'cmin':
254 $value /= 2.54;
255 break;
256
257 case 'cmmm':
258 $value *= 10;
259 break;
260
261 case 'cmpt':
262 $value *= 72 / 2.54;
263 break;
264
265 case 'mmin':
266 $value /= 25.4;
267 break;
268
269 case 'mmcm':
270 $value /= 10;
271 break;
272
273 case 'mmpt':
274 $value *= 72 / 25.4;
275 break;
276
277 case 'ptin':
278 $value /= 72;
279 break;
280
281 case 'ptcm':
282 $value *= 2.54 / 72;
283 break;
284
285 case 'ptmm':
286 $value *= 25.4 / 72;
287 break;
288 }
289 if (!is_null($precision)) {
290 $value = round($value, $precision);
291 }
292 return $value;
293 }
294
295 }