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
37 class CRM_Utils_PDF_Utils
{
41 * List of HTML snippets.
42 * @param string $fileName
43 * The logical filename to display.
44 * Ex: "HelloWorld.pdf".
46 * FALSE to display PDF. TRUE to return as string.
47 * @param null $pdfFormat
48 * Unclear. Possibly PdfFormat or formValues.
52 public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
53 if (is_array($text)) {
59 // Get PDF Page Format
60 $format = CRM_Core_BAO_PdfFormat
::getDefaultValues();
61 if (is_array($pdfFormat)) {
62 // PDF Page Format parameters passed in
63 $format = array_merge($format, $pdfFormat);
65 elseif (!empty($pdfFormat)) {
66 // PDF Page Format ID passed in
67 $format = CRM_Core_BAO_PdfFormat
::getById($pdfFormat);
69 $paperSize = CRM_Core_BAO_PaperSize
::getByName($format['paper_size']);
70 $paper_width = self
::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
71 $paper_height = self
::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
72 // dompdf requires dimensions in points
73 $paper_size = [0, 0, $paper_width, $paper_height];
74 $orientation = CRM_Core_BAO_PdfFormat
::getValue('orientation', $format);
75 $metric = CRM_Core_BAO_PdfFormat
::getValue('metric', $format);
76 $t = CRM_Core_BAO_PdfFormat
::getValue('margin_top', $format);
77 $r = CRM_Core_BAO_PdfFormat
::getValue('margin_right', $format);
78 $b = CRM_Core_BAO_PdfFormat
::getValue('margin_bottom', $format);
79 $l = CRM_Core_BAO_PdfFormat
::getValue('margin_left', $format);
81 $margins = [$metric, $t, $r, $b, $l];
83 // Add a special region for the HTML header of PDF files:
84 $pdfHeaderRegion = CRM_Core_Region
::instance('export-document-header', FALSE);
85 $htmlHeader = ($pdfHeaderRegion) ?
$pdfHeaderRegion->render('', FALSE) : '';
90 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
91 <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>
92 <style type=\"text/css\">@import url(" . CRM_Core_Config
::singleton()->userFrameworkResourceURL
. "css/print.css);</style>
96 <div id=\"crm-container\">\n";
98 // Strip <html>, <header>, and <body> tags from each page
99 $htmlElementstoStrip = [
100 '@<head[^>]*?>.*?</head>@siu',
101 '@<script[^>]*?>.*?</script>@siu',
106 '@<!DOCTYPE[^>]*?>@siu',
108 $htmlElementsInstead = ['', '', '', '', '', ''];
109 foreach ($pages as & $page) {
110 $page = preg_replace($htmlElementstoStrip,
111 $htmlElementsInstead,
115 // Glue the pages together
116 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
121 if (CRM_Core_Config
::singleton()->wkhtmltopdfPath
) {
122 return self
::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
125 return self
::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
130 * Convert html to tcpdf.
133 * @param $orientation
138 * @param $stationery_path
140 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
141 // Documentation on the TCPDF library can be found at: http://www.tcpdf.org
142 // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/
143 // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc
144 require_once 'tcpdf/tcpdf.php';
145 // This library is only in the 'packages' area as of version 4.5
146 require_once 'FPDI/fpdi.php';
148 $paper_size_arr = [$paper_size[2], $paper_size[3]];
150 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
153 if (is_readable($stationery_path)) {
154 $pdf->SetStationery($stationery_path);
158 $pdf->SetKeywords('CiviCRM.org');
159 $pdf->setPageUnit($margins[0]);
160 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
162 $pdf->setJPEGQuality('100');
163 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
173 // output the HTML content
174 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
176 // reset pointer to the last page
179 // close and output the PDF
181 $pdf_file = 'CiviLetter' . '.pdf';
182 $pdf->Output($pdf_file, 'D');
183 CRM_Utils_System
::civiExit();
188 * @param $orientation
191 * @param string $fileName
195 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
196 // CRM-12165 - Remote file support required for image handling.
197 $options = new Options();
198 $options->set('isRemoteEnabled', TRUE);
200 $dompdf = new DOMPDF($options);
201 $dompdf->set_paper($paper_size, $orientation);
202 $dompdf->load_html($html);
206 return $dompdf->output();
209 // CRM-19183 remove .pdf extension from filename
210 $fileName = basename($fileName, ".pdf");
211 $dompdf->stream($fileName);
217 * @param $orientation
221 * @param string $fileName
223 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
224 require_once 'packages/snappy/src/autoload.php';
225 $config = CRM_Core_Config
::singleton();
226 $snappy = new Knp\Snappy\
Pdf($config->wkhtmltopdfPath
);
227 $snappy->setOption("page-width", $paper_size[2] . "pt");
228 $snappy->setOption("page-height", $paper_size[3] . "pt");
229 $snappy->setOption("orientation", $orientation);
230 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
231 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
232 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
233 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
234 $pdf = $snappy->getOutputFromHtml($html);
239 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/pdf');
240 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
246 * convert value from one metric to another.
251 * @param null $precision
255 public static function convertMetric($value, $from, $to, $precision = NULL) {
256 switch ($from . $to) {
305 if (!is_null($precision)) {
306 $value = round($value, $precision);