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