Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
383b23e3 | 28 | |
0b6fe14b | 29 | use Dompdf\Dompdf; |
2256f347 | 30 | use Dompdf\Options; |
6a488035 TO |
31 | /** |
32 | * | |
33 | * @package CRM | |
6b83d5bd | 34 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
35 | */ |
36 | class CRM_Utils_PDF_Utils { | |
37 | ||
5bc392e6 | 38 | /** |
4f308883 TO |
39 | * @param array $text |
40 | * List of HTML snippets. | |
5bc392e6 | 41 | * @param string $fileName |
3ed92c14 TO |
42 | * The logical filename to display. |
43 | * Ex: "HelloWorld.pdf". | |
5bc392e6 | 44 | * @param bool $output |
3ed92c14 | 45 | * FALSE to display PDF. TRUE to return as string. |
5bc392e6 | 46 | * @param null $pdfFormat |
3ed92c14 | 47 | * Unclear. Possibly PdfFormat or formValues. |
5bc392e6 EM |
48 | * |
49 | * @return string|void | |
50 | */ | |
56d5847d | 51 | public static function html2pdf($text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) { |
6a488035 TO |
52 | if (is_array($text)) { |
53 | $pages = &$text; | |
54 | } | |
55 | else { | |
56 | $pages = array($text); | |
57 | } | |
58 | // Get PDF Page Format | |
59 | $format = CRM_Core_BAO_PdfFormat::getDefaultValues(); | |
60 | if (is_array($pdfFormat)) { | |
61 | // PDF Page Format parameters passed in | |
62 | $format = array_merge($format, $pdfFormat); | |
63 | } | |
18a3e0c0 | 64 | elseif (!empty($pdfFormat)) { |
6a488035 TO |
65 | // PDF Page Format ID passed in |
66 | $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat); | |
67 | } | |
353ffa53 TO |
68 | $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']); |
69 | $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt'); | |
6a488035 TO |
70 | $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt'); |
71 | // dompdf requires dimensions in points | |
353ffa53 | 72 | $paper_size = array(0, 0, $paper_width, $paper_height); |
6a488035 | 73 | $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format); |
353ffa53 TO |
74 | $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format); |
75 | $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format); | |
76 | $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format); | |
77 | $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format); | |
78 | $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format); | |
bdfa67c3 | 79 | |
353ffa53 | 80 | $margins = array($metric, $t, $r, $b, $l); |
6a488035 | 81 | |
b0500874 | 82 | // Add a special region for the HTML header of PDF files: |
7419f31d | 83 | $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE); |
b0500874 AH |
84 | $htmlHeader = ($pdfHeaderRegion) ? $pdfHeaderRegion->render('', FALSE) : ''; |
85 | ||
6a488035 TO |
86 | $html = " |
87 | <html> | |
88 | <head> | |
89 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> | |
90 | <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style> | |
56d5847d | 91 | <style type=\"text/css\">@import url(" . CRM_Core_Config::singleton()->userFrameworkResourceURL . "css/print.css);</style> |
b0500874 | 92 | {$htmlHeader} |
6a488035 TO |
93 | </head> |
94 | <body> | |
95 | <div id=\"crm-container\">\n"; | |
96 | ||
97 | // Strip <html>, <header>, and <body> tags from each page | |
56d5847d | 98 | $htmlElementstoStrip = [ |
6a488035 | 99 | '@<head[^>]*?>.*?</head>@siu', |
8e637fa3 | 100 | '@<script[^>]*?>.*?</script>@siu', |
6a488035 TO |
101 | '@<body>@siu', |
102 | '@</body>@siu', | |
103 | '@<html[^>]*?>@siu', | |
104 | '@</html>@siu', | |
105 | '@<!DOCTYPE[^>]*?>@siu', | |
56d5847d | 106 | ]; |
107 | $htmlElementsInstead = ['', '', '', '', '', '']; | |
6a488035 TO |
108 | foreach ($pages as & $page) { |
109 | $page = preg_replace($htmlElementstoStrip, | |
110 | $htmlElementsInstead, | |
111 | $page | |
112 | ); | |
113 | } | |
114 | // Glue the pages together | |
115 | $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages); | |
116 | $html .= " | |
117 | </div> | |
118 | </body> | |
119 | </html>"; | |
56d5847d | 120 | if (CRM_Core_Config::singleton()->wkhtmltopdfPath) { |
6a488035 TO |
121 | return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName); |
122 | } | |
123 | else { | |
8d7ae5ee | 124 | return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName); |
6a488035 TO |
125 | } |
126 | } | |
127 | ||
2e2605fe EM |
128 | /** |
129 | * Convert html to tcpdf. | |
130 | * | |
131 | * @param $paper_size | |
132 | * @param $orientation | |
133 | * @param $margins | |
134 | * @param $html | |
135 | * @param $output | |
136 | * @param $fileName | |
137 | * @param $stationery_path | |
138 | */ | |
00be9182 | 139 | public static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) { |
bdfa67c3 | 140 | // Documentation on the TCPDF library can be found at: http://www.tcpdf.org |
141 | // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/ | |
142 | // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc | |
143 | require_once 'tcpdf/tcpdf.php'; | |
e7292422 | 144 | require_once 'FPDI/fpdi.php'; // This library is only in the 'packages' area as of version 4.5 |
bdfa67c3 | 145 | |
353ffa53 | 146 | $paper_size_arr = array($paper_size[2], $paper_size[3]); |
bdfa67c3 | 147 | |
8e637fa3 | 148 | $pdf = new TCPDF($orientation, 'pt', $paper_size_arr); |
bdfa67c3 | 149 | $pdf->Open(); |
150 | ||
9b873358 | 151 | if (is_readable($stationery_path)) { |
481a74f4 | 152 | $pdf->SetStationery($stationery_path); |
bdfa67c3 | 153 | } |
154 | ||
155 | $pdf->SetAuthor(''); | |
156 | $pdf->SetKeywords('CiviCRM.org'); | |
481a74f4 | 157 | $pdf->setPageUnit($margins[0]); |
e7292422 | 158 | $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE); |
bdfa67c3 | 159 | |
160 | $pdf->setJPEGQuality('100'); | |
e7292422 | 161 | $pdf->SetAutoPageBreak(TRUE, $margins[3]); |
bdfa67c3 | 162 | |
163 | $pdf->AddPage(); | |
164 | ||
e7292422 TO |
165 | $ln = TRUE; |
166 | $fill = FALSE; | |
167 | $reset_parm = FALSE; | |
168 | $cell = FALSE; | |
169 | $align = ''; | |
8e637fa3 | 170 | |
bdfa67c3 | 171 | // output the HTML content |
172 | $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align); | |
173 | ||
174 | // reset pointer to the last page | |
175 | $pdf->lastPage(); | |
176 | ||
177 | // close and output the PDF | |
178 | $pdf->Close(); | |
92fcb95f | 179 | $pdf_file = 'CiviLetter' . '.pdf'; |
bdfa67c3 | 180 | $pdf->Output($pdf_file, 'D'); |
292c8687 | 181 | CRM_Utils_System::civiExit(); |
bdfa67c3 | 182 | } |
183 | ||
5bc392e6 EM |
184 | /** |
185 | * @param $paper_size | |
186 | * @param $orientation | |
187 | * @param $html | |
188 | * @param $output | |
100fef9d | 189 | * @param string $fileName |
5bc392e6 EM |
190 | * |
191 | * @return string | |
192 | */ | |
00be9182 | 193 | public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) { |
2256f347 SL |
194 | // CRM-12165 - Remote file support required for image handling. |
195 | $options = new Options(); | |
196 | $options->set('isRemoteEnabled', TRUE); | |
197 | ||
198 | $dompdf = new DOMPDF($options); | |
6a488035 TO |
199 | $dompdf->set_paper($paper_size, $orientation); |
200 | $dompdf->load_html($html); | |
201 | $dompdf->render(); | |
202 | ||
203 | if ($output) { | |
204 | return $dompdf->output(); | |
205 | } | |
206 | else { | |
53a39759 | 207 | // CRM-19183 remove .pdf extension from filename |
208 | $fileName = basename($fileName, ".pdf"); | |
6a488035 TO |
209 | $dompdf->stream($fileName); |
210 | } | |
211 | } | |
212 | ||
5bc392e6 EM |
213 | /** |
214 | * @param $paper_size | |
215 | * @param $orientation | |
216 | * @param $margins | |
217 | * @param $html | |
218 | * @param $output | |
100fef9d | 219 | * @param string $fileName |
5bc392e6 | 220 | */ |
00be9182 | 221 | public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) { |
6a488035 TO |
222 | require_once 'packages/snappy/src/autoload.php'; |
223 | $config = CRM_Core_Config::singleton(); | |
224 | $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath); | |
225 | $snappy->setOption("page-width", $paper_size[2] . "pt"); | |
226 | $snappy->setOption("page-height", $paper_size[3] . "pt"); | |
227 | $snappy->setOption("orientation", $orientation); | |
228 | $snappy->setOption("margin-top", $margins[1] . $margins[0]); | |
229 | $snappy->setOption("margin-right", $margins[2] . $margins[0]); | |
230 | $snappy->setOption("margin-bottom", $margins[3] . $margins[0]); | |
231 | $snappy->setOption("margin-left", $margins[4] . $margins[0]); | |
232 | $pdf = $snappy->getOutputFromHtml($html); | |
233 | if ($output) { | |
234 | return $pdf; | |
235 | } | |
236 | else { | |
d42a224c CW |
237 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf'); |
238 | CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"'); | |
6a488035 TO |
239 | echo $pdf; |
240 | } | |
241 | } | |
242 | ||
5bc392e6 | 243 | /** |
fe482240 | 244 | * convert value from one metric to another. |
d424ffde | 245 | * |
5bc392e6 EM |
246 | * @param $value |
247 | * @param $from | |
248 | * @param $to | |
249 | * @param null $precision | |
250 | * | |
251 | * @return float|int | |
252 | */ | |
00be9182 | 253 | public static function convertMetric($value, $from, $to, $precision = NULL) { |
6a488035 TO |
254 | switch ($from . $to) { |
255 | case 'incm': | |
256 | $value *= 2.54; | |
257 | break; | |
258 | ||
259 | case 'inmm': | |
260 | $value *= 25.4; | |
261 | break; | |
262 | ||
263 | case 'inpt': | |
264 | $value *= 72; | |
265 | break; | |
266 | ||
267 | case 'cmin': | |
268 | $value /= 2.54; | |
269 | break; | |
270 | ||
271 | case 'cmmm': | |
272 | $value *= 10; | |
273 | break; | |
274 | ||
275 | case 'cmpt': | |
276 | $value *= 72 / 2.54; | |
277 | break; | |
278 | ||
279 | case 'mmin': | |
280 | $value /= 25.4; | |
281 | break; | |
282 | ||
283 | case 'mmcm': | |
284 | $value /= 10; | |
285 | break; | |
286 | ||
287 | case 'mmpt': | |
288 | $value *= 72 / 25.4; | |
289 | break; | |
290 | ||
291 | case 'ptin': | |
292 | $value /= 72; | |
293 | break; | |
294 | ||
295 | case 'ptcm': | |
296 | $value *= 2.54 / 72; | |
297 | break; | |
298 | ||
299 | case 'ptmm': | |
300 | $value *= 25.4 / 72; | |
301 | break; | |
302 | } | |
303 | if (!is_null($precision)) { | |
304 | $value = round($value, $precision); | |
305 | } | |
306 | return $value; | |
307 | } | |
308 | ||
6a488035 | 309 | } |