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
39 class CRM_Utils_PDF_Utils
{
43 * @param string $fileName
45 * @param null $pdfFormat
49 public static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
50 if (is_array($text)) {
54 $pages = array($text);
56 // Get PDF Page Format
57 $format = CRM_Core_BAO_PdfFormat
::getDefaultValues();
58 if (is_array($pdfFormat)) {
59 // PDF Page Format parameters passed in
60 $format = array_merge($format, $pdfFormat);
63 // PDF Page Format ID passed in
64 $format = CRM_Core_BAO_PdfFormat
::getById($pdfFormat);
66 $paperSize = CRM_Core_BAO_PaperSize
::getByName($format['paper_size']);
67 $paper_width = self
::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
68 $paper_height = self
::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
69 // dompdf requires dimensions in points
70 $paper_size = array(0, 0, $paper_width, $paper_height);
71 $orientation = CRM_Core_BAO_PdfFormat
::getValue('orientation', $format);
72 $metric = CRM_Core_BAO_PdfFormat
::getValue('metric', $format);
73 $t = CRM_Core_BAO_PdfFormat
::getValue('margin_top', $format);
74 $r = CRM_Core_BAO_PdfFormat
::getValue('margin_right', $format);
75 $b = CRM_Core_BAO_PdfFormat
::getValue('margin_bottom', $format);
76 $l = CRM_Core_BAO_PdfFormat
::getValue('margin_left', $format);
78 $stationery_path_partial = CRM_Core_BAO_PdfFormat
::getValue('stationery', $format);
80 $stationery_path = NULL;
81 if (strlen($stationery_path_partial)) {
82 $doc_root = $_SERVER['DOCUMENT_ROOT'];
83 $stationery_path = $doc_root . "/" . $stationery_path_partial;
86 $margins = array($metric, $t, $r, $b, $l);
88 $config = CRM_Core_Config
::singleton();
92 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
93 <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>
94 <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>
97 <div id=\"crm-container\">\n";
99 // Strip <html>, <header>, and <body> tags from each page
100 $htmlElementstoStrip = array(
101 '@<head[^>]*?>.*?</head>@siu',
102 '@<script[^>]*?>.*?</script>@siu',
107 '@<!DOCTYPE[^>]*?>@siu',
109 $htmlElementsInstead = array('', '', '', '', '', '');
110 foreach ($pages as & $page) {
111 $page = preg_replace($htmlElementstoStrip,
112 $htmlElementsInstead,
116 // Glue the pages together
117 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
122 if ($config->wkhtmltopdfPath
) {
123 return self
::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
126 return self
::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
127 //return self::_html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path);
132 * Convert html to tcpdf.
135 * @param $orientation
140 * @param $stationery_path
142 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
143 // Documentation on the TCPDF library can be found at: http://www.tcpdf.org
144 // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/
145 // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc
146 require_once 'tcpdf/tcpdf.php';
147 require_once 'FPDI/fpdi.php'; // This library is only in the 'packages' area as of version 4.5
149 $paper_size_arr = array($paper_size[2], $paper_size[3]);
151 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
154 if (is_readable($stationery_path)) {
155 $pdf->SetStationery($stationery_path);
159 $pdf->SetKeywords('CiviCRM.org');
160 $pdf->setPageUnit($margins[0]);
161 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
163 $pdf->setJPEGQuality('100');
164 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
174 // output the HTML content
175 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
177 // reset pointer to the last page
180 // close and output the PDF
182 $pdf_file = 'CiviLetter' . '.pdf';
183 $pdf->Output($pdf_file, 'D');
184 CRM_Utils_System
::civiExit(1);
189 * @param $orientation
192 * @param string $fileName
196 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
197 require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php';
199 $dompdf = new DOMPDF();
200 $dompdf->set_paper($paper_size, $orientation);
201 $dompdf->load_html($html);
205 return $dompdf->output();
208 $dompdf->stream($fileName);
214 * @param $orientation
218 * @param string $fileName
220 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
221 require_once 'packages/snappy/src/autoload.php';
222 $config = CRM_Core_Config
::singleton();
223 $snappy = new Knp\Snappy\
Pdf($config->wkhtmltopdfPath
);
224 $snappy->setOption("page-width", $paper_size[2] . "pt");
225 $snappy->setOption("page-height", $paper_size[3] . "pt");
226 $snappy->setOption("orientation", $orientation);
227 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
228 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
229 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
230 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
231 $pdf = $snappy->getOutputFromHtml($html);
236 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/pdf');
237 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
243 * convert value from one metric to another.
248 * @param null $precision
252 public static function convertMetric($value, $from, $to, $precision = NULL) {
253 switch ($from . $to) {
302 if (!is_null($precision)) {
303 $value = round($value, $precision);
309 * @param string $fileName
312 * @param int $numPages
314 * @param string $output
315 * @param string $creator
316 * @param string $author
317 * @param string $title
319 public static function &pdflib(
325 $output = 'College_Match_App',
326 $creator = 'CiviCRM',
327 $author = 'http://www.civicrm.org/',
328 $title = '2006 College Match Scholarship Application'
332 $pdf->set_parameter("compatibility", "1.6");
333 $pdf->set_parameter("licensefile", "/home/paras/bin/license/pdflib.txt");
335 if ($pdf->begin_document('', '') == 0) {
336 CRM_Core_Error
::statusBounce("PDFlib Error: " . $pdf->get_errmsg());
339 $config = CRM_Core_Config
::singleton();
340 $pdf->set_parameter('resourcefile', $config->templateDir
. '/Quest/pdf/pdflib.upr');
341 $pdf->set_parameter('textformat', 'utf8');
343 /* Set the search path for fonts and PDF files */
345 $pdf->set_parameter('SearchPath', $searchPath);
347 /* This line is required to avoid problems on Japanese systems */
349 $pdf->set_parameter('hypertextencoding', 'winansi');
351 $pdf->set_info('Creator', $creator);
352 $pdf->set_info('Author', $author);
353 $pdf->set_info('Title', $title);
355 $blockContainer = $pdf->open_pdi($fileName, '', 0);
356 if ($blockContainer == 0) {
357 CRM_Core_Error
::statusBounce('PDFlib Error: ' . $pdf->get_errmsg());
360 for ($i = 1; $i <= $numPages; $i++
) {
361 $page = $pdf->open_pdi_page($blockContainer, $i, '');
363 CRM_Core_Error
::statusBounce('PDFlib Error: ' . $pdf->get_errmsg());
366 /* dummy page size */
367 $pdf->begin_page_ext(20, 20, '');
369 /* This will adjust the page size to the block container's size. */
371 $pdf->fit_pdi_page($page, 0, 0, 'adjustpage');
374 /* Fill all text blocks with dynamic data */
376 foreach ($values as $key => $value) {
377 if (is_array($value)) {
381 // pdflib does like the forward slash character, hence convert
382 $value = str_replace('/', '_', $value);
384 $res = $pdf->fill_textblock($page,
387 'embedding encoding=winansi'
392 $pdf->end_page_ext('');
393 $pdf->close_pdi_page($page);
396 $pdf->end_document('');
397 $pdf->close_pdi($blockContainer);
399 $buf = $pdf->get_buffer();
403 CRM_Utils_System
::setHttpHeader('Content-type', 'application/pdf');
404 CRM_Utils_System
::setHttpHeader("Content-Length", $len);
405 CRM_Utils_System
::setHttpHeader("Content-Disposition", "inline; filename={$output}.pdf");
407 CRM_Utils_System
::civiExit();
413 catch (PDFlibException
$excp) {
414 CRM_Core_Error
::statusBounce('PDFlib Error: Exception' .
415 "[" . $excp->get_errnum() . "] " . $excp->get_apiname() . ": " .
419 catch (Exception
$excp) {
420 CRM_Core_Error
::statusBounce("PDFlib Error: " . $excp->get_errmsg());