3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
28 // CRM-12165 - Remote file support required for image handling.
29 define("DOMPDF_ENABLE_REMOTE", TRUE);
30 define('DOMPDF_ENABLE_AUTOLOAD', FALSE);
35 * @copyright CiviCRM LLC (c) 2004-2015
37 class CRM_Utils_PDF_Utils
{
41 * @param string $fileName
43 * @param null $pdfFormat
47 public static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
48 if (is_array($text)) {
52 $pages = array($text);
54 // Get PDF Page Format
55 $format = CRM_Core_BAO_PdfFormat
::getDefaultValues();
56 if (is_array($pdfFormat)) {
57 // PDF Page Format parameters passed in
58 $format = array_merge($format, $pdfFormat);
61 // PDF Page Format ID passed in
62 $format = CRM_Core_BAO_PdfFormat
::getById($pdfFormat);
64 $paperSize = CRM_Core_BAO_PaperSize
::getByName($format['paper_size']);
65 $paper_width = self
::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
66 $paper_height = self
::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
67 // dompdf requires dimensions in points
68 $paper_size = array(0, 0, $paper_width, $paper_height);
69 $orientation = CRM_Core_BAO_PdfFormat
::getValue('orientation', $format);
70 $metric = CRM_Core_BAO_PdfFormat
::getValue('metric', $format);
71 $t = CRM_Core_BAO_PdfFormat
::getValue('margin_top', $format);
72 $r = CRM_Core_BAO_PdfFormat
::getValue('margin_right', $format);
73 $b = CRM_Core_BAO_PdfFormat
::getValue('margin_bottom', $format);
74 $l = CRM_Core_BAO_PdfFormat
::getValue('margin_left', $format);
76 $stationery_path_partial = CRM_Core_BAO_PdfFormat
::getValue('stationery', $format);
78 $stationery_path = NULL;
79 if (strlen($stationery_path_partial)) {
80 $doc_root = $_SERVER['DOCUMENT_ROOT'];
81 $stationery_path = $doc_root . "/" . $stationery_path_partial;
84 $margins = array($metric, $t, $r, $b, $l);
86 $config = CRM_Core_Config
::singleton();
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({$config->userFrameworkResourceURL}css/print.css);</style>
95 <div id=\"crm-container\">\n";
97 // Strip <html>, <header>, and <body> tags from each page
98 $htmlElementstoStrip = array(
99 '@<head[^>]*?>.*?</head>@siu',
100 '@<script[^>]*?>.*?</script>@siu',
105 '@<!DOCTYPE[^>]*?>@siu',
107 $htmlElementsInstead = array('', '', '', '', '', '');
108 foreach ($pages as & $page) {
109 $page = preg_replace($htmlElementstoStrip,
110 $htmlElementsInstead,
114 // Glue the pages together
115 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
120 if ($config->wkhtmltopdfPath
) {
121 return self
::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
124 return self
::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
125 //return self::_html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path);
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 require_once 'FPDI/fpdi.php'; // This library is only in the 'packages' area as of version 4.5
147 $paper_size_arr = array($paper_size[2], $paper_size[3]);
149 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
152 if (is_readable($stationery_path)) {
153 $pdf->SetStationery($stationery_path);
157 $pdf->SetKeywords('CiviCRM.org');
158 $pdf->setPageUnit($margins[0]);
159 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
161 $pdf->setJPEGQuality('100');
162 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
172 // output the HTML content
173 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
175 // reset pointer to the last page
178 // close and output the PDF
180 $pdf_file = 'CiviLetter' . '.pdf';
181 $pdf->Output($pdf_file, 'D');
182 CRM_Utils_System
::civiExit(1);
187 * @param $orientation
190 * @param string $fileName
194 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
195 require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php';
197 $dompdf = new DOMPDF();
198 $dompdf->set_paper($paper_size, $orientation);
199 $dompdf->load_html($html);
203 return $dompdf->output();
206 $dompdf->stream($fileName);
212 * @param $orientation
216 * @param string $fileName
218 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
219 require_once 'packages/snappy/src/autoload.php';
220 $config = CRM_Core_Config
::singleton();
221 $snappy = new Knp\Snappy\
Pdf($config->wkhtmltopdfPath
);
222 $snappy->setOption("page-width", $paper_size[2] . "pt");
223 $snappy->setOption("page-height", $paper_size[3] . "pt");
224 $snappy->setOption("orientation", $orientation);
225 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
226 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
227 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
228 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
229 $pdf = $snappy->getOutputFromHtml($html);
234 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/pdf');
235 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
241 * convert value from one metric to another.
246 * @param null $precision
250 public static function convertMetric($value, $from, $to, $precision = NULL) {
251 switch ($from . $to) {
300 if (!is_null($precision)) {
301 $value = round($value, $precision);