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