55c674dc16d634b9747914f5876019381f4c542a
[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 *
34 * @package CRM
35 * @copyright CiviCRM LLC (c) 2004-2019
36 */
37
38 class CRM_Utils_PDF_Utils {
39
40 public static function latex2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
41 /* FIXME: get $paper_size, $orientation, $margins */
42
43 if (is_array($text)) {
44 $pages = &$text;
45 }
46 else {
47 $pages = array($text);
48 }
49
50
51 $head='\documentclass[11pt]{letter}
52 \usepackage{url}
53 \usepackage{ucs}
54 \usepackage{graphicx}
55 \usepackage[T1]{fontenc}
56 \usepackage{fullpage}
57
58 \newcommand{\fsfclosing}[1]{\par\nobreak\vspace{\parskip}
59 \stopbreaks
60 \noindent
61 \ifx\@empty\fromaddress\else
62 \hspace*{\longindentation}\fi
63 \parbox{\indentedwidth}{\raggedright
64 \ignorespaces #1\\\\[1\medskipamount]
65 \hspace*{-0.25in}\includegraphics[scale=1.0]{sigjohns.pdf}
66 \\\\
67
68 \ifx\@empty\fromsig
69 \fromname
70 \else \fromsig \fi\strut}
71 \par}
72 \medskipamount=\parskip
73
74 %% This line might be necessary, but it was not able to find utf8.def on my
75 %% machine.
76 \usepackage[utf8x]{inputenc}
77 \pagestyle{empty}
78 \tolerance=8000
79 \address{\vspace{0.05in}}
80 \signature{John Sullivan \\\\ Executive Director}
81 \usepackage[
82 top = 0.4in,
83 bottom = 0.9in,
84 left = 0.8in,
85 right = 0.8in]{geometry}
86 \begin{document}
87 ';
88 $footer='
89 \end{document}';
90
91 $latex = $head;
92 foreach ($pages as $page) {
93 $latex.=$page;
94 }
95 $latex.=$footer;
96
97 $descriptorspec = array(
98 0 => array("pipe", "r"),
99 1 => array("pipe", "w")
100 );
101
102
103
104 $process = proc_open("/usr/local/bin/pdflatex_wrapper.sh", $descriptorspec, $pipes);
105
106
107 if (is_resource($process)) {
108 fwrite($pipes[0], $latex);
109 fclose($pipes[0]);
110
111 $pdf = stream_get_contents($pipes[1]);
112 fclose($pipes[1]);
113 } else {
114 CRM_Core_Error::debug_log_message("ERROR creating PDF. Check /tmp/pdflatex_*");
115 }
116
117 if ($output) {
118 return $pdf;
119 }
120 else {
121 header('Content-Type: application/pdf');
122 header('Content-Disposition: attachment; filename="' . $fileName . '"');
123 echo $pdf;
124 }
125 }
126
127 /**
128 * @param array $text
129 * List of HTML snippets.
130 * @param string $fileName
131 * The logical filename to display.
132 * Ex: "HelloWorld.pdf".
133 * @param bool $output
134 * FALSE to display PDF. TRUE to return as string.
135 * @param null $pdfFormat
136 * Unclear. Possibly PdfFormat or formValues.
137 *
138 * @return string|void
139 */
140 public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
141 if (is_array($text)) {
142 $pages = &$text;
143 }
144 else {
145 $pages = [$text];
146 }
147 // Get PDF Page Format
148 $format = CRM_Core_BAO_PdfFormat::getDefaultValues();
149 if (is_array($pdfFormat)) {
150 // PDF Page Format parameters passed in
151 $format = array_merge($format, $pdfFormat);
152 }
153 elseif (!empty($pdfFormat)) {
154 // PDF Page Format ID passed in
155 $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat);
156 }
157 $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
158 $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
159 $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
160 // dompdf requires dimensions in points
161 $paper_size = [0, 0, $paper_width, $paper_height];
162 $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format);
163 $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
164 $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format);
165 $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format);
166 $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format);
167 $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format);
168
169 $margins = [$metric, $t, $r, $b, $l];
170
171 // Add a special region for the HTML header of PDF files:
172 $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE);
173 $htmlHeader = ($pdfHeaderRegion) ? $pdfHeaderRegion->render('', FALSE) : '';
174
175 $html = "
176 <html>
177 <head>
178 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
179 <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>
180 <style type=\"text/css\">@import url(" . CRM_Core_Config::singleton()->userFrameworkResourceURL . "css/print.css);</style>
181 {$htmlHeader}
182 </head>
183 <body>
184 <div id=\"crm-container\">\n";
185
186 // Strip <html>, <header>, and <body> tags from each page
187 $htmlElementstoStrip = [
188 '@<head[^>]*?>.*?</head>@siu',
189 '@<script[^>]*?>.*?</script>@siu',
190 '@<body>@siu',
191 '@</body>@siu',
192 '@<html[^>]*?>@siu',
193 '@</html>@siu',
194 '@<!DOCTYPE[^>]*?>@siu',
195 ];
196 $htmlElementsInstead = ['', '', '', '', '', ''];
197 foreach ($pages as & $page) {
198 $page = preg_replace($htmlElementstoStrip,
199 $htmlElementsInstead,
200 $page
201 );
202 }
203 // Glue the pages together
204 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
205 $html .= "
206 </div>
207 </body>
208 </html>";
209 if (CRM_Core_Config::singleton()->wkhtmltopdfPath) {
210 return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
211 }
212 else {
213 return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
214 }
215 }
216
217 /**
218 * Convert html to tcpdf.
219 *
220 * @param $paper_size
221 * @param $orientation
222 * @param $margins
223 * @param $html
224 * @param $output
225 * @param $fileName
226 * @param $stationery_path
227 */
228 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
229 // Documentation on the TCPDF library can be found at: http://www.tcpdf.org
230 // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/
231 // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc
232 require_once 'tcpdf/tcpdf.php';
233 // This library is only in the 'packages' area as of version 4.5
234 require_once 'FPDI/fpdi.php';
235
236 $paper_size_arr = [$paper_size[2], $paper_size[3]];
237
238 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
239 $pdf->Open();
240
241 if (is_readable($stationery_path)) {
242 $pdf->SetStationery($stationery_path);
243 }
244
245 $pdf->SetAuthor('');
246 $pdf->SetKeywords('CiviCRM.org');
247 $pdf->setPageUnit($margins[0]);
248 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
249
250 $pdf->setJPEGQuality('100');
251 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
252
253 $pdf->AddPage();
254
255 $ln = TRUE;
256 $fill = FALSE;
257 $reset_parm = FALSE;
258 $cell = FALSE;
259 $align = '';
260
261 // output the HTML content
262 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
263
264 // reset pointer to the last page
265 $pdf->lastPage();
266
267 // close and output the PDF
268 $pdf->Close();
269 $pdf_file = 'CiviLetter' . '.pdf';
270 $pdf->Output($pdf_file, 'D');
271 CRM_Utils_System::civiExit();
272 }
273
274 /**
275 * @param $paper_size
276 * @param $orientation
277 * @param $html
278 * @param $output
279 * @param string $fileName
280 *
281 * @return string
282 */
283 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
284 // CRM-12165 - Remote file support required for image handling.
285 $options = new Options();
286 $options->set('isRemoteEnabled', TRUE);
287
288 $dompdf = new DOMPDF($options);
289 $dompdf->set_paper($paper_size, $orientation);
290 $dompdf->load_html($html);
291 $dompdf->render();
292
293 if ($output) {
294 return $dompdf->output();
295 }
296 else {
297 // CRM-19183 remove .pdf extension from filename
298 $fileName = basename($fileName, ".pdf");
299 $dompdf->stream($fileName);
300 }
301 }
302
303 /**
304 * @param $paper_size
305 * @param $orientation
306 * @param $margins
307 * @param $html
308 * @param $output
309 * @param string $fileName
310 */
311 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
312 require_once 'packages/snappy/src/autoload.php';
313 $config = CRM_Core_Config::singleton();
314 $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath);
315 $snappy->setOption("page-width", $paper_size[2] . "pt");
316 $snappy->setOption("page-height", $paper_size[3] . "pt");
317 $snappy->setOption("orientation", $orientation);
318 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
319 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
320 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
321 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
322 $pdf = $snappy->getOutputFromHtml($html);
323 if ($output) {
324 return $pdf;
325 }
326 else {
327 CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf');
328 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
329 echo $pdf;
330 }
331 }
332
333 /**
334 * convert value from one metric to another.
335 *
336 * @param $value
337 * @param $from
338 * @param $to
339 * @param null $precision
340 *
341 * @return float|int
342 */
343 public static function convertMetric($value, $from, $to, $precision = NULL) {
344 switch ($from . $to) {
345 case 'incm':
346 $value *= 2.54;
347 break;
348
349 case 'inmm':
350 $value *= 25.4;
351 break;
352
353 case 'inpt':
354 $value *= 72;
355 break;
356
357 case 'cmin':
358 $value /= 2.54;
359 break;
360
361 case 'cmmm':
362 $value *= 10;
363 break;
364
365 case 'cmpt':
366 $value *= 72 / 2.54;
367 break;
368
369 case 'mmin':
370 $value /= 25.4;
371 break;
372
373 case 'mmcm':
374 $value /= 10;
375 break;
376
377 case 'mmpt':
378 $value *= 72 / 25.4;
379 break;
380
381 case 'ptin':
382 $value /= 72;
383 break;
384
385 case 'ptcm':
386 $value *= 2.54 / 72;
387 break;
388
389 case 'ptmm':
390 $value *= 25.4 / 72;
391 break;
392 }
393 if (!is_null($precision)) {
394 $value = round($value, $precision);
395 }
396 return $value;
397 }
398
399 }