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