Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | 29 | use Dompdf\Dompdf; |
2256f347 | 30 | use Dompdf\Options; |
6714d8d2 | 31 | |
6a488035 TO |
32 | /** |
33 | * | |
34 | * @package CRM | |
6b83d5bd | 35 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 | 36 | */ |
4acecdfd | 37 | |
6a488035 TO |
38 | class CRM_Utils_PDF_Utils { |
39 | ||
1217b5fc RR |
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 | ||
4acecdfd LMM |
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 | ||
1217b5fc | 59 | $head='\documentclass[12pt]{letter} |
4acecdfd LMM |
60 | \usepackage{url} |
61 | \usepackage{ucs} | |
62 | \usepackage{graphicx} | |
63 | \usepackage[T1]{fontenc} | |
64 | \usepackage{fullpage} | |
1217b5fc RR |
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{CMU Serif}}{} | |
69 | \setTransitionsForLatin{\fontspec{CMU Serif}}{} | |
70 | \setTransitionsForArabics{\fontspec{Droid Sans Arabic}}{} | |
71 | \setTransitionsForCJK{\fontspec{WenQuanYi Zen Hei}}{} | |
72 | \setTransitionsForDiacritics{\fontspec{Droid Sans Arabic}}{} | |
73 | \setTransitionTo{Hebrew}{\fontspec{David CLM}} | |
74 | \setmainfont{CMU Serif} | |
4acecdfd LMM |
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] | |
1217b5fc | 83 | \hspace*{-0.25in}\includegraphics[scale=1.0]{/var/www/drupal-7.27/sites/all/modules/civicrm/sigjohns.pdf} |
4acecdfd LMM |
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. | |
1217b5fc | 94 | %% \usepackage[utf8x]{inputenc} |
4acecdfd LMM |
95 | \pagestyle{empty} |
96 | \tolerance=8000 | |
97 | \address{\vspace{0.05in}} | |
98 | \signature{John Sullivan \\\\ Executive Director} | |
99 | \usepackage[ | |
1217b5fc RR |
100 | top = 1.5in, |
101 | bottom = 1.25in, | |
102 | left = 1.0in, | |
103 | right = 1.0in]{geometry} | |
4acecdfd LMM |
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; | |
1217b5fc RR |
142 | // CRM_Utils_PDF_Utils::enqueuePDF($pdf); |
143 | ||
4acecdfd | 144 | } |
1217b5fc | 145 | } |
4acecdfd | 146 | |
5bc392e6 | 147 | /** |
4f308883 TO |
148 | * @param array $text |
149 | * List of HTML snippets. | |
5bc392e6 | 150 | * @param string $fileName |
3ed92c14 TO |
151 | * The logical filename to display. |
152 | * Ex: "HelloWorld.pdf". | |
5bc392e6 | 153 | * @param bool $output |
3ed92c14 | 154 | * FALSE to display PDF. TRUE to return as string. |
5bc392e6 | 155 | * @param null $pdfFormat |
3ed92c14 | 156 | * Unclear. Possibly PdfFormat or formValues. |
5bc392e6 EM |
157 | * |
158 | * @return string|void | |
159 | */ | |
56d5847d | 160 | public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) { |
6a488035 TO |
161 | if (is_array($text)) { |
162 | $pages = &$text; | |
163 | } | |
164 | else { | |
be2fb01f | 165 | $pages = [$text]; |
6a488035 TO |
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 | } | |
18a3e0c0 | 173 | elseif (!empty($pdfFormat)) { |
6a488035 TO |
174 | // PDF Page Format ID passed in |
175 | $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat); | |
176 | } | |
353ffa53 TO |
177 | $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']); |
178 | $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt'); | |
6a488035 TO |
179 | $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt'); |
180 | // dompdf requires dimensions in points | |
be2fb01f | 181 | $paper_size = [0, 0, $paper_width, $paper_height]; |
6a488035 | 182 | $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format); |
353ffa53 TO |
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); | |
bdfa67c3 | 188 | |
be2fb01f | 189 | $margins = [$metric, $t, $r, $b, $l]; |
6a488035 | 190 | |
b0500874 | 191 | // Add a special region for the HTML header of PDF files: |
7419f31d | 192 | $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE); |
b0500874 AH |
193 | $htmlHeader = ($pdfHeaderRegion) ? $pdfHeaderRegion->render('', FALSE) : ''; |
194 | ||
6a488035 TO |
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> | |
56d5847d | 200 | <style type=\"text/css\">@import url(" . CRM_Core_Config::singleton()->userFrameworkResourceURL . "css/print.css);</style> |
b0500874 | 201 | {$htmlHeader} |
6a488035 TO |
202 | </head> |
203 | <body> | |
204 | <div id=\"crm-container\">\n"; | |
205 | ||
206 | // Strip <html>, <header>, and <body> tags from each page | |
56d5847d | 207 | $htmlElementstoStrip = [ |
6a488035 | 208 | '@<head[^>]*?>.*?</head>@siu', |
8e637fa3 | 209 | '@<script[^>]*?>.*?</script>@siu', |
6a488035 TO |
210 | '@<body>@siu', |
211 | '@</body>@siu', | |
212 | '@<html[^>]*?>@siu', | |
213 | '@</html>@siu', | |
214 | '@<!DOCTYPE[^>]*?>@siu', | |
56d5847d | 215 | ]; |
216 | $htmlElementsInstead = ['', '', '', '', '', '']; | |
6a488035 TO |
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>"; | |
56d5847d | 229 | if (CRM_Core_Config::singleton()->wkhtmltopdfPath) { |
6a488035 TO |
230 | return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName); |
231 | } | |
232 | else { | |
8d7ae5ee | 233 | return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName); |
6a488035 TO |
234 | } |
235 | } | |
236 | ||
2e2605fe EM |
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 | */ | |
00be9182 | 248 | public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) { |
bdfa67c3 | 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'; | |
6714d8d2 SL |
253 | // This library is only in the 'packages' area as of version 4.5 |
254 | require_once 'FPDI/fpdi.php'; | |
bdfa67c3 | 255 | |
be2fb01f | 256 | $paper_size_arr = [$paper_size[2], $paper_size[3]]; |
bdfa67c3 | 257 | |
8e637fa3 | 258 | $pdf = new TCPDF($orientation, 'pt', $paper_size_arr); |
bdfa67c3 | 259 | $pdf->Open(); |
260 | ||
9b873358 | 261 | if (is_readable($stationery_path)) { |
481a74f4 | 262 | $pdf->SetStationery($stationery_path); |
bdfa67c3 | 263 | } |
264 | ||
265 | $pdf->SetAuthor(''); | |
266 | $pdf->SetKeywords('CiviCRM.org'); | |
481a74f4 | 267 | $pdf->setPageUnit($margins[0]); |
e7292422 | 268 | $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE); |
bdfa67c3 | 269 | |
270 | $pdf->setJPEGQuality('100'); | |
e7292422 | 271 | $pdf->SetAutoPageBreak(TRUE, $margins[3]); |
bdfa67c3 | 272 | |
273 | $pdf->AddPage(); | |
274 | ||
e7292422 TO |
275 | $ln = TRUE; |
276 | $fill = FALSE; | |
277 | $reset_parm = FALSE; | |
278 | $cell = FALSE; | |
279 | $align = ''; | |
8e637fa3 | 280 | |
bdfa67c3 | 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(); | |
92fcb95f | 289 | $pdf_file = 'CiviLetter' . '.pdf'; |
bdfa67c3 | 290 | $pdf->Output($pdf_file, 'D'); |
292c8687 | 291 | CRM_Utils_System::civiExit(); |
bdfa67c3 | 292 | } |
293 | ||
5bc392e6 EM |
294 | /** |
295 | * @param $paper_size | |
296 | * @param $orientation | |
297 | * @param $html | |
298 | * @param $output | |
100fef9d | 299 | * @param string $fileName |
5bc392e6 EM |
300 | * |
301 | * @return string | |
302 | */ | |
00be9182 | 303 | public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) { |
2256f347 SL |
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); | |
6a488035 TO |
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 { | |
53a39759 | 317 | // CRM-19183 remove .pdf extension from filename |
318 | $fileName = basename($fileName, ".pdf"); | |
6a488035 TO |
319 | $dompdf->stream($fileName); |
320 | } | |
321 | } | |
322 | ||
5bc392e6 EM |
323 | /** |
324 | * @param $paper_size | |
325 | * @param $orientation | |
326 | * @param $margins | |
327 | * @param $html | |
328 | * @param $output | |
100fef9d | 329 | * @param string $fileName |
5bc392e6 | 330 | */ |
00be9182 | 331 | public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) { |
6a488035 TO |
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]); | |
1217b5fc | 342 | $html = preg_replace('/{ }/', ' ', $html); |
6a488035 TO |
343 | $pdf = $snappy->getOutputFromHtml($html); |
344 | if ($output) { | |
345 | return $pdf; | |
346 | } | |
347 | else { | |
d42a224c CW |
348 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf'); |
349 | CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"'); | |
6a488035 | 350 | echo $pdf; |
1217b5fc RR |
351 | //CRM_Utils_PDF_Utils::enqueuePDF($pdf); |
352 | ||
6a488035 TO |
353 | } |
354 | } | |
355 | ||
5bc392e6 | 356 | /** |
fe482240 | 357 | * convert value from one metric to another. |
d424ffde | 358 | * |
5bc392e6 EM |
359 | * @param $value |
360 | * @param $from | |
361 | * @param $to | |
362 | * @param null $precision | |
363 | * | |
364 | * @return float|int | |
365 | */ | |
00be9182 | 366 | public static function convertMetric($value, $from, $to, $precision = NULL) { |
6a488035 TO |
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 | ||
6a488035 | 422 | } |