Merge pull request #12995 from eileenmcnaughton/activity_extract2
[civicrm-core.git] / CRM / Utils / PDF / Utils.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28
29 use Dompdf\Dompdf;
30 use Dompdf\Options;
31 /**
32 *
33 * @package CRM
34 * @copyright CiviCRM LLC (c) 2004-2019
35 */
36 class CRM_Utils_PDF_Utils {
37
38 /**
39 * @param array $text
40 * List of HTML snippets.
41 * @param string $fileName
42 * The logical filename to display.
43 * Ex: "HelloWorld.pdf".
44 * @param bool $output
45 * FALSE to display PDF. TRUE to return as string.
46 * @param null $pdfFormat
47 * Unclear. Possibly PdfFormat or formValues.
48 *
49 * @return string|void
50 */
51 public static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
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 }
68 $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
69 $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
70 $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
71 // dompdf requires dimensions in points
72 $paper_size = array(0, 0, $paper_width, $paper_height);
73 $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format);
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);
79
80 $stationery_path_partial = CRM_Core_BAO_PdfFormat::getValue('stationery', $format);
81
82 $stationery_path = NULL;
83 if (strlen($stationery_path_partial)) {
84 $doc_root = $_SERVER['DOCUMENT_ROOT'];
85 $stationery_path = $doc_root . "/" . $stationery_path_partial;
86 }
87
88 $margins = array($metric, $t, $r, $b, $l);
89
90 $config = CRM_Core_Config::singleton();
91
92 // Add a special region for the HTML header of PDF files:
93 $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE);
94 $htmlHeader = ($pdfHeaderRegion) ? $pdfHeaderRegion->render('', FALSE) : '';
95
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>
102 {$htmlHeader}
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',
110 '@<script[^>]*?>.*?</script>@siu',
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 {
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);
136 }
137 }
138
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 */
150 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
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';
155 require_once 'FPDI/fpdi.php'; // This library is only in the 'packages' area as of version 4.5
156
157 $paper_size_arr = array($paper_size[2], $paper_size[3]);
158
159 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
160 $pdf->Open();
161
162 if (is_readable($stationery_path)) {
163 $pdf->SetStationery($stationery_path);
164 }
165
166 $pdf->SetAuthor('');
167 $pdf->SetKeywords('CiviCRM.org');
168 $pdf->setPageUnit($margins[0]);
169 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
170
171 $pdf->setJPEGQuality('100');
172 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
173
174 $pdf->AddPage();
175
176 $ln = TRUE;
177 $fill = FALSE;
178 $reset_parm = FALSE;
179 $cell = FALSE;
180 $align = '';
181
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();
190 $pdf_file = 'CiviLetter' . '.pdf';
191 $pdf->Output($pdf_file, 'D');
192 CRM_Utils_System::civiExit();
193 }
194
195 /**
196 * @param $paper_size
197 * @param $orientation
198 * @param $html
199 * @param $output
200 * @param string $fileName
201 *
202 * @return string
203 */
204 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
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);
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 {
218 // CRM-19183 remove .pdf extension from filename
219 $fileName = basename($fileName, ".pdf");
220 $dompdf->stream($fileName);
221 }
222 }
223
224 /**
225 * @param $paper_size
226 * @param $orientation
227 * @param $margins
228 * @param $html
229 * @param $output
230 * @param string $fileName
231 */
232 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
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 {
248 CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf');
249 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
250 echo $pdf;
251 }
252 }
253
254 /**
255 * convert value from one metric to another.
256 *
257 * @param $value
258 * @param $from
259 * @param $to
260 * @param null $precision
261 *
262 * @return float|int
263 */
264 public static function convertMetric($value, $from, $to, $precision = NULL) {
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
320 }