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