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