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