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