55c674dc16d634b9747914f5876019381f4c542a
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 latex2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
41 /* FIXME: get $paper_size, $orientation, $margins */
43 if (is_array($text)) {
47 $pages = array($text);
51 $head='\documentclass[11pt]{letter}
55 \usepackage[T1]{fontenc}
58 \newcommand{\fsfclosing}[1]{\par\nobreak\vspace{\parskip}
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}
70 \else \fromsig \fi\strut}
72 \medskipamount=\parskip
74 %% This line might be necessary, but it was not able to find utf8.def on my
76 \usepackage[utf8x]{inputenc}
79 \address{\vspace{0.05in}}
80 \signature{John Sullivan \\\\ Executive Director}
85 right = 0.8in]{geometry}
92 foreach ($pages as $page) {
97 $descriptorspec = array(
98 0 => array("pipe", "r"),
99 1 => array("pipe", "w")
104 $process = proc_open("/usr/local/bin/pdflatex_wrapper.sh", $descriptorspec, $pipes);
107 if (is_resource($process)) {
108 fwrite($pipes[0], $latex);
111 $pdf = stream_get_contents($pipes[1]);
114 CRM_Core_Error
::debug_log_message("ERROR creating PDF. Check /tmp/pdflatex_*");
121 header('Content-Type: application/pdf');
122 header('Content-Disposition: attachment; filename="' . $fileName . '"');
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.
138 * @return string|void
140 public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
141 if (is_array($text)) {
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);
153 elseif (!empty($pdfFormat)) {
154 // PDF Page Format ID passed in
155 $format = CRM_Core_BAO_PdfFormat
::getById($pdfFormat);
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);
169 $margins = [$metric, $t, $r, $b, $l];
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) : '';
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>
184 <div id=\"crm-container\">\n";
186 // Strip <html>, <header>, and <body> tags from each page
187 $htmlElementstoStrip = [
188 '@<head[^>]*?>.*?</head>@siu',
189 '@<script[^>]*?>.*?</script>@siu',
194 '@<!DOCTYPE[^>]*?>@siu',
196 $htmlElementsInstead = ['', '', '', '', '', ''];
197 foreach ($pages as & $page) {
198 $page = preg_replace($htmlElementstoStrip,
199 $htmlElementsInstead,
203 // Glue the pages together
204 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
209 if (CRM_Core_Config
::singleton()->wkhtmltopdfPath
) {
210 return self
::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
213 return self
::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
218 * Convert html to tcpdf.
221 * @param $orientation
226 * @param $stationery_path
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';
236 $paper_size_arr = [$paper_size[2], $paper_size[3]];
238 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
241 if (is_readable($stationery_path)) {
242 $pdf->SetStationery($stationery_path);
246 $pdf->SetKeywords('CiviCRM.org');
247 $pdf->setPageUnit($margins[0]);
248 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
250 $pdf->setJPEGQuality('100');
251 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
261 // output the HTML content
262 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
264 // reset pointer to the last page
267 // close and output the PDF
269 $pdf_file = 'CiviLetter' . '.pdf';
270 $pdf->Output($pdf_file, 'D');
271 CRM_Utils_System
::civiExit();
276 * @param $orientation
279 * @param string $fileName
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);
288 $dompdf = new DOMPDF($options);
289 $dompdf->set_paper($paper_size, $orientation);
290 $dompdf->load_html($html);
294 return $dompdf->output();
297 // CRM-19183 remove .pdf extension from filename
298 $fileName = basename($fileName, ".pdf");
299 $dompdf->stream($fileName);
305 * @param $orientation
309 * @param string $fileName
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);
327 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/pdf');
328 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
334 * convert value from one metric to another.
339 * @param null $precision
343 public static function convertMetric($value, $from, $to, $precision = NULL) {
344 switch ($from . $to) {
393 if (!is_null($precision)) {
394 $value = round($value, $precision);