3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
19 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 class CRM_Utils_PDF_Utils
{
24 public static function enqueuePDF($pdf) {
26 $fname = time().'_lp.pdf';
27 file_put_contents('/tmp/'.$fname, $pdf);
28 header('Location: /civicrm/lp-setup?file='.$fname);
33 public static function latex2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
34 /* FIXME: get $paper_size, $orientation, $margins */
36 if (is_array($text)) {
40 $pages = array($text);
43 $head='\documentclass[12pt]{letter}
47 \usepackage[T1]{fontenc}
49 \usepackage{fontspec,xunicode}
50 %% VERY IMPORTANT. Configures supported languages and fonts to use for each one.
51 \usepackage[Latin, Hebrew, Arabics, CJK, Diacritics]{ucharclasses}
52 \setDefaultTransitions{\fontspec{CMU Serif}}{}
53 \setTransitionsForLatin{\fontspec{CMU Serif}}{}
54 \setTransitionsForArabics{\fontspec{Droid Sans Arabic}}{}
55 \setTransitionsForCJK{\fontspec{WenQuanYi Zen Hei}}{}
56 \setTransitionsForDiacritics{\fontspec{Droid Sans Arabic}}{}
57 \setTransitionTo{Hebrew}{\fontspec{David CLM}}
58 \setmainfont{CMU Serif}
60 \newcommand{\fsfclosing}[1]{\par\nobreak\vspace{\parskip}
63 \ifx\@empty\fromaddress\else
64 \hspace*{\longindentation}\fi
65 \parbox{\indentedwidth}{\raggedright
66 \ignorespaces #1\\\\[1\medskipamount]
67 \hspace*{-0.25in}\includegraphics[scale=1.0]{/var/www/drupal-7.27/sites/all/modules/civicrm/sigjohns.pdf}
72 \else \fromsig \fi\strut}
74 \medskipamount=\parskip
76 %% This line might be necessary, but it was not able to find utf8.def on my
78 %% \usepackage[utf8x]{inputenc}
81 \address{\vspace{0.05in}}
82 \signature{John Sullivan \\\\ Executive Director}
87 right = 1.0in]{geometry}
94 foreach ($pages as $page) {
99 $descriptorspec = array(
100 0 => array("pipe", "r"),
101 1 => array("pipe", "w")
106 $process = proc_open("/usr/local/bin/pdflatex_wrapper.sh", $descriptorspec, $pipes);
109 if (is_resource($process)) {
110 fwrite($pipes[0], $latex);
113 $pdf = stream_get_contents($pipes[1]);
116 CRM_Core_Error
::debug_log_message("ERROR creating PDF. Check /tmp/pdflatex_*");
123 header('Content-Type: application/pdf');
124 header('Content-Disposition: attachment; filename="' . $fileName . '"');
126 // CRM_Utils_PDF_Utils::enqueuePDF($pdf);
133 * List of HTML snippets.
134 * @param string $fileName
135 * The logical filename to display.
136 * Ex: "HelloWorld.pdf".
137 * @param bool $output
138 * FALSE to display PDF. TRUE to return as string.
139 * @param array|int|null $pdfFormat
140 * Unclear. Possibly PdfFormat or formValues.
142 * @return string|void
144 public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
145 if (is_array($text)) {
151 // Get PDF Page Format
152 $format = CRM_Core_BAO_PdfFormat
::getDefaultValues();
153 if (is_array($pdfFormat)) {
154 // PDF Page Format parameters passed in
155 $format = array_merge($format, $pdfFormat);
157 elseif (!empty($pdfFormat)) {
158 // PDF Page Format ID passed in
159 $format = CRM_Core_BAO_PdfFormat
::getById($pdfFormat);
161 $paperSize = CRM_Core_BAO_PaperSize
::getByName($format['paper_size']);
162 $paper_width = self
::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
163 $paper_height = self
::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
164 // dompdf requires dimensions in points
165 $paper_size = [0, 0, $paper_width, $paper_height];
166 $orientation = CRM_Core_BAO_PdfFormat
::getValue('orientation', $format);
167 $metric = CRM_Core_BAO_PdfFormat
::getValue('metric', $format);
168 $t = CRM_Core_BAO_PdfFormat
::getValue('margin_top', $format);
169 $r = CRM_Core_BAO_PdfFormat
::getValue('margin_right', $format);
170 $b = CRM_Core_BAO_PdfFormat
::getValue('margin_bottom', $format);
171 $l = CRM_Core_BAO_PdfFormat
::getValue('margin_left', $format);
173 $margins = [$metric, $t, $r, $b, $l];
175 // Add a special region for the HTML header of PDF files:
176 $pdfHeaderRegion = CRM_Core_Region
::instance('export-document-header', FALSE);
177 $htmlHeader = ($pdfHeaderRegion) ?
$pdfHeaderRegion->render('', FALSE) : '';
182 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
183 <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>
184 <style type=\"text/css\">@import url(" . CRM_Core_Config
::singleton()->userFrameworkResourceURL
. "css/print.css);</style>
188 <div id=\"crm-container\">\n";
190 // Strip <html>, <header>, and <body> tags from each page
192 $htmlElementstoStrip = [
193 '<head[^>]*?>.*?</head>',
194 '<script[^>]*?>.*?</script>',
201 foreach ($pages as & $page) {
202 foreach ($htmlElementstoStrip as $pattern) {
203 $page = mb_eregi_replace($pattern, '', $page);
206 // Glue the pages together
207 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
212 if (CRM_Core_Config
::singleton()->wkhtmltopdfPath
) {
213 return self
::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
216 return self
::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
221 * Convert html to tcpdf.
225 * @param $orientation
230 * @param $stationery_path
232 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
233 CRM_Core_Error
::deprecatedFunctionWarning('CRM_Utils_PDF::_html2pdf_dompdf');
234 return self
::_html2pdf_dompdf($paper_size, $orientation, $margins, $html, $output, $fileName);
235 // Documentation on the TCPDF library can be found at: http://www.tcpdf.org
236 // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/
237 // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc
238 require_once 'tcpdf/tcpdf.php';
239 // This library is only in the 'packages' area as of version 4.5
240 require_once 'FPDI/fpdi.php';
242 $paper_size_arr = [$paper_size[2], $paper_size[3]];
244 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
247 if (is_readable($stationery_path)) {
248 $pdf->SetStationery($stationery_path);
252 $pdf->SetKeywords('CiviCRM.org');
253 $pdf->setPageUnit($margins[0]);
254 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
256 $pdf->setJPEGQuality('100');
257 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
267 // output the HTML content
268 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
270 // reset pointer to the last page
273 // close and output the PDF
275 $pdf_file = 'CiviLetter' . '.pdf';
276 $pdf->Output($pdf_file, 'D');
277 CRM_Utils_System
::civiExit();
282 * @param $orientation
285 * @param string $fileName
289 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
290 $options = self
::getDompdfOptions();
291 $dompdf = new DOMPDF($options);
292 $dompdf->set_paper($paper_size, $orientation);
293 $dompdf->load_html($html);
297 return $dompdf->output();
299 // CRM-19183 remove .pdf extension from filename
300 $fileName = basename($fileName, ".pdf");
301 if (CIVICRM_UF
=== 'UnitTests') {
302 // Streaming content will 'die' in unit tests unless ob_start()
304 throw new CRM_Core_Exception_PrematureExitException('_html2pdf_dompdf called', [
306 'fileName' => $fileName,
310 $dompdf->stream($fileName);
314 * @param float|int[] $paper_size
315 * @param string $orientation
316 * @param array $margins
317 * @param string $html
318 * @param bool $output
319 * @param string $fileName
321 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
322 require_once 'snappy/src/autoload.php';
323 $config = CRM_Core_Config
::singleton();
324 $snappy = new Knp\Snappy\
Pdf($config->wkhtmltopdfPath
);
325 $snappy->setOption("page-width", $paper_size[2] . "pt");
326 $snappy->setOption("page-height", $paper_size[3] . "pt");
327 $snappy->setOption("orientation", $orientation);
328 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
329 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
330 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
331 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
332 $html = preg_replace('/{ }/', ' ', $html);
333 $pdf = $snappy->getOutputFromHtml($html);
338 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/pdf');
339 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
341 //CRM_Utils_PDF_Utils::enqueuePDF($pdf);
347 * convert value from one metric to another.
350 * @param string $from
352 * @param int|null $precision
356 public static function convertMetric($value, $from, $to, $precision = NULL) {
357 switch ($from . $to) {
406 if (!is_null($precision)) {
407 $value = round($value, $precision);
413 * Allow setting some dompdf options.
415 * We don't support all the available dompdf options.
417 * @return \Dompdf\Options
419 private static function getDompdfOptions(): Options
{
420 $options = new Options();
422 // CRM-12165 - Remote file support required for image handling so default to TRUE
423 'enable_remote' => \Civi
::settings()->get('dompdf_enable_remote') ??
TRUE,
425 // only set these ones if a setting exists for them
426 foreach (['font_dir', 'chroot', 'log_output_file'] as $setting) {
427 $value = \Civi
::settings()->get("dompdf_$setting");
429 $settings[$setting] = $value;
432 $options->set($settings);