3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
35 * @copyright CiviCRM LLC (c) 2004-2019
38 class CRM_Utils_PDF_Utils
{
40 public static function enqueuePDF($pdf) {
42 $fname = time().'_lp.pdf';
43 file_put_contents('/tmp/'.$fname, $pdf);
44 header('Location: /civicrm/lp-setup?file='.$fname);
49 public static function latex2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
50 /* FIXME: get $paper_size, $orientation, $margins */
52 if (is_array($text)) {
56 $pages = array($text);
59 $head='\documentclass[12pt]{letter}
63 \usepackage[T1]{fontenc}
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}
76 \newcommand{\fsfclosing}[1]{\par\nobreak\vspace{\parskip}
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}
88 \else \fromsig \fi\strut}
90 \medskipamount=\parskip
92 %% This line might be necessary, but it was not able to find utf8.def on my
94 %% \usepackage[utf8x]{inputenc}
97 \address{\vspace{0.05in}}
98 \signature{John Sullivan \\\\ Executive Director}
103 right = 1.0in]{geometry}
110 foreach ($pages as $page) {
115 $descriptorspec = array(
116 0 => array("pipe", "r"),
117 1 => array("pipe", "w")
122 $process = proc_open("/usr/local/bin/pdflatex_wrapper.sh", $descriptorspec, $pipes);
125 if (is_resource($process)) {
126 fwrite($pipes[0], $latex);
129 $pdf = stream_get_contents($pipes[1]);
132 CRM_Core_Error
::debug_log_message("ERROR creating PDF. Check /tmp/pdflatex_*");
139 header('Content-Type: application/pdf');
140 header('Content-Disposition: attachment; filename="' . $fileName . '"');
142 // CRM_Utils_PDF_Utils::enqueuePDF($pdf);
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.
158 * @return string|void
160 public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
161 if (is_array($text)) {
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);
173 elseif (!empty($pdfFormat)) {
174 // PDF Page Format ID passed in
175 $format = CRM_Core_BAO_PdfFormat
::getById($pdfFormat);
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);
189 $margins = [$metric, $t, $r, $b, $l];
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) : '';
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>
204 <div id=\"crm-container\">\n";
206 // Strip <html>, <header>, and <body> tags from each page
207 $htmlElementstoStrip = [
208 '@<head[^>]*?>.*?</head>@siu',
209 '@<script[^>]*?>.*?</script>@siu',
214 '@<!DOCTYPE[^>]*?>@siu',
216 $htmlElementsInstead = ['', '', '', '', '', ''];
217 foreach ($pages as & $page) {
218 $page = preg_replace($htmlElementstoStrip,
219 $htmlElementsInstead,
223 // Glue the pages together
224 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
229 if (CRM_Core_Config
::singleton()->wkhtmltopdfPath
) {
230 return self
::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
233 return self
::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
238 * Convert html to tcpdf.
241 * @param $orientation
246 * @param $stationery_path
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';
256 $paper_size_arr = [$paper_size[2], $paper_size[3]];
258 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
261 if (is_readable($stationery_path)) {
262 $pdf->SetStationery($stationery_path);
266 $pdf->SetKeywords('CiviCRM.org');
267 $pdf->setPageUnit($margins[0]);
268 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
270 $pdf->setJPEGQuality('100');
271 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
281 // output the HTML content
282 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
284 // reset pointer to the last page
287 // close and output the PDF
289 $pdf_file = 'CiviLetter' . '.pdf';
290 $pdf->Output($pdf_file, 'D');
291 CRM_Utils_System
::civiExit();
296 * @param $orientation
299 * @param string $fileName
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);
308 $dompdf = new DOMPDF($options);
309 $dompdf->set_paper($paper_size, $orientation);
310 $dompdf->load_html($html);
314 return $dompdf->output();
317 // CRM-19183 remove .pdf extension from filename
318 $fileName = basename($fileName, ".pdf");
319 $dompdf->stream($fileName);
325 * @param $orientation
329 * @param string $fileName
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);
348 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/pdf');
349 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
351 //CRM_Utils_PDF_Utils::enqueuePDF($pdf);
357 * convert value from one metric to another.
362 * @param null $precision
366 public static function convertMetric($value, $from, $to, $precision = NULL) {
367 switch ($from . $to) {
416 if (!is_null($precision)) {
417 $value = round($value, $precision);