3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Utils_PDF_Utils
{
39 * @param string $fileName
41 * @param null $pdfFormat
45 public static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
46 if (is_array($text)) {
50 $pages = array($text);
52 // Get PDF Page Format
53 $format = CRM_Core_BAO_PdfFormat
::getDefaultValues();
54 if (is_array($pdfFormat)) {
55 // PDF Page Format parameters passed in
56 $format = array_merge($format, $pdfFormat);
59 // PDF Page Format ID passed in
60 $format = CRM_Core_BAO_PdfFormat
::getById($pdfFormat);
62 $paperSize = CRM_Core_BAO_PaperSize
::getByName($format['paper_size']);
63 $paper_width = self
::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
64 $paper_height = self
::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
65 // dompdf requires dimensions in points
66 $paper_size = array(0, 0, $paper_width, $paper_height);
67 $orientation = CRM_Core_BAO_PdfFormat
::getValue('orientation', $format);
68 $metric = CRM_Core_BAO_PdfFormat
::getValue('metric', $format);
69 $t = CRM_Core_BAO_PdfFormat
::getValue('margin_top', $format);
70 $r = CRM_Core_BAO_PdfFormat
::getValue('margin_right', $format);
71 $b = CRM_Core_BAO_PdfFormat
::getValue('margin_bottom', $format);
72 $l = CRM_Core_BAO_PdfFormat
::getValue('margin_left', $format);
74 $stationery_path_partial = CRM_Core_BAO_PdfFormat
::getValue('stationery', $format);
76 $stationery_path = NULL;
77 if (strlen($stationery_path_partial)) {
78 $doc_root = $_SERVER['DOCUMENT_ROOT'];
79 $stationery_path = $doc_root . "/" . $stationery_path_partial;
82 $margins = array($metric, $t, $r, $b, $l);
84 $config = CRM_Core_Config
::singleton();
88 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
89 <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>
90 <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>
93 <div id=\"crm-container\">\n";
95 // Strip <html>, <header>, and <body> tags from each page
96 $htmlElementstoStrip = array(
97 '@<head[^>]*?>.*?</head>@siu',
98 '@<script[^>]*?>.*?</script>@siu',
103 '@<!DOCTYPE[^>]*?>@siu',
105 $htmlElementsInstead = array('', '', '', '', '', '');
106 foreach ($pages as & $page) {
107 $page = preg_replace($htmlElementstoStrip,
108 $htmlElementsInstead,
112 // Glue the pages together
113 $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
118 if ($config->wkhtmltopdfPath
) {
119 return self
::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
122 return self
::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
123 //return self::_html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path);
128 * Convert html to tcpdf.
131 * @param $orientation
136 * @param $stationery_path
138 public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
139 // Documentation on the TCPDF library can be found at: http://www.tcpdf.org
140 // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/
141 // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc
142 require_once 'tcpdf/tcpdf.php';
143 require_once 'FPDI/fpdi.php'; // This library is only in the 'packages' area as of version 4.5
145 $paper_size_arr = array($paper_size[2], $paper_size[3]);
147 $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
150 if (is_readable($stationery_path)) {
151 $pdf->SetStationery($stationery_path);
155 $pdf->SetKeywords('CiviCRM.org');
156 $pdf->setPageUnit($margins[0]);
157 $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
159 $pdf->setJPEGQuality('100');
160 $pdf->SetAutoPageBreak(TRUE, $margins[3]);
170 // output the HTML content
171 $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
173 // reset pointer to the last page
176 // close and output the PDF
178 $pdf_file = 'CiviLetter' . '.pdf';
179 $pdf->Output($pdf_file, 'D');
180 CRM_Utils_System
::civiExit(1);
185 * @param $orientation
188 * @param string $fileName
192 public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
193 require_once 'packages/dompdf/dompdf_config.inc.php';
194 spl_autoload_register('DOMPDF_autoload');
195 $dompdf = new DOMPDF();
196 $dompdf->set_paper($paper_size, $orientation);
197 $dompdf->load_html($html);
201 return $dompdf->output();
204 $dompdf->stream($fileName);
210 * @param $orientation
214 * @param string $fileName
216 public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
217 require_once 'packages/snappy/src/autoload.php';
218 $config = CRM_Core_Config
::singleton();
219 $snappy = new Knp\Snappy\
Pdf($config->wkhtmltopdfPath
);
220 $snappy->setOption("page-width", $paper_size[2] . "pt");
221 $snappy->setOption("page-height", $paper_size[3] . "pt");
222 $snappy->setOption("orientation", $orientation);
223 $snappy->setOption("margin-top", $margins[1] . $margins[0]);
224 $snappy->setOption("margin-right", $margins[2] . $margins[0]);
225 $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
226 $snappy->setOption("margin-left", $margins[4] . $margins[0]);
227 $pdf = $snappy->getOutputFromHtml($html);
232 header('Content-Type: application/pdf');
233 header('Content-Disposition: attachment; filename="' . $fileName . '"');
239 * convert value from one metric to another.
244 * @param null $precision
248 public static function convertMetric($value, $from, $to, $precision = NULL) {
249 switch ($from . $to) {
298 if (!is_null($precision)) {
299 $value = round($value, $precision);
305 * @param string $fileName
308 * @param int $numPages
310 * @param string $output
311 * @param string $creator
312 * @param string $author
313 * @param string $title
315 public static function &pdflib(
321 $output = 'College_Match_App',
322 $creator = 'CiviCRM',
323 $author = 'http://www.civicrm.org/',
324 $title = '2006 College Match Scholarship Application'
328 $pdf->set_parameter("compatibility", "1.6");
329 $pdf->set_parameter("licensefile", "/home/paras/bin/license/pdflib.txt");
331 if ($pdf->begin_document('', '') == 0) {
332 CRM_Core_Error
::statusBounce("PDFlib Error: " . $pdf->get_errmsg());
335 $config = CRM_Core_Config
::singleton();
336 $pdf->set_parameter('resourcefile', $config->templateDir
. '/Quest/pdf/pdflib.upr');
337 $pdf->set_parameter('textformat', 'utf8');
339 /* Set the search path for fonts and PDF files */
341 $pdf->set_parameter('SearchPath', $searchPath);
343 /* This line is required to avoid problems on Japanese systems */
345 $pdf->set_parameter('hypertextencoding', 'winansi');
347 $pdf->set_info('Creator', $creator);
348 $pdf->set_info('Author', $author);
349 $pdf->set_info('Title', $title);
351 $blockContainer = $pdf->open_pdi($fileName, '', 0);
352 if ($blockContainer == 0) {
353 CRM_Core_Error
::statusBounce('PDFlib Error: ' . $pdf->get_errmsg());
356 for ($i = 1; $i <= $numPages; $i++
) {
357 $page = $pdf->open_pdi_page($blockContainer, $i, '');
359 CRM_Core_Error
::statusBounce('PDFlib Error: ' . $pdf->get_errmsg());
362 /* dummy page size */
363 $pdf->begin_page_ext(20, 20, '');
365 /* This will adjust the page size to the block container's size. */
367 $pdf->fit_pdi_page($page, 0, 0, 'adjustpage');
370 /* Fill all text blocks with dynamic data */
372 foreach ($values as $key => $value) {
373 if (is_array($value)) {
377 // pdflib does like the forward slash character, hence convert
378 $value = str_replace('/', '_', $value);
380 $res = $pdf->fill_textblock($page,
383 'embedding encoding=winansi'
388 $pdf->end_page_ext('');
389 $pdf->close_pdi_page($page);
392 $pdf->end_document('');
393 $pdf->close_pdi($blockContainer);
395 $buf = $pdf->get_buffer();
399 header('Content-type: application/pdf');
400 header("Content-Length: $len");
401 header("Content-Disposition: inline; filename={$output}.pdf");
403 CRM_Utils_System
::civiExit();
409 catch (PDFlibException
$excp) {
410 CRM_Core_Error
::statusBounce('PDFlib Error: Exception' .
411 "[" . $excp->get_errnum() . "] " . $excp->get_apiname() . ": " .
415 catch (Exception
$excp) {
416 CRM_Core_Error
::statusBounce("PDFlib Error: " . $excp->get_errmsg());