Fixes #6570 portal pdf document creator class (#6572)
[openemr.git] / src / Pdf / PatientPortalPDFDocumentCreator.php
blobb3183b176b3657a6715269e9cdc3a342626c477d
1 <?php
3 /**
4 * PatientPortalPDFDocumentCreator is used for generating pdf documents from html documents that have been submitted
5 * via a patient either from the patient portal or in a patient smart app.
7 * @package OpenEMR
8 * @link https://www.open-emr.org
9 * @author Jerry Padgett <sjpadgett@gmail.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @author Discover and Change, Inc. <snielson@discoverandchange.com>
12 * @copyright Copyright (c) 2016-2022 Jerry Padgett <sjpadgett@gmail.com>
13 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
14 * @copyright Copyright (c) 2023 Discover and Change, Inc. <snielson@discoverandchange.com>
15 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
18 namespace OpenEMR\Pdf;
20 use Mpdf\Mpdf;
21 use HTMLPurifier_Config;
23 class PatientPortalPDFDocumentCreator
25 public function createPdfObject($htmlIn)
27 $config_mpdf = array(
28 'tempDir' => $GLOBALS['MPDF_WRITE_DIR'],
29 'mode' => $GLOBALS['pdf_language'],
30 'format' => $GLOBALS['pdf_size'],
31 'default_font_size' => '9',
32 'default_font' => 'dejavusans',
33 'margin_left' => $GLOBALS['pdf_left_margin'],
34 'margin_right' => $GLOBALS['pdf_right_margin'],
35 'margin_top' => $GLOBALS['pdf_top_margin'],
36 'margin_bottom' => $GLOBALS['pdf_bottom_margin'],
37 'margin_header' => '',
38 'margin_footer' => '',
39 'orientation' => $GLOBALS['pdf_layout'],
40 'shrink_tables_to_fit' => 1,
41 'use_kwt' => true,
42 'autoScriptToLang' => true,
43 'keep_table_proportions' => true
46 $pdf = new Mpdf($config_mpdf);
47 if ($_SESSION['language_direction'] == 'rtl') {
48 $pdf->SetDirectionality('rtl');
51 // snatch style tags content to insert after content purified
52 $style_flag = preg_match('#<\s*?style\b[^>]*>(.*?)</style\b[^>]*>#s', $htmlIn, $style_matches);
53 $style = str_replace('<style type="text/css">', '<style>', $style_matches);
54 $pos = stripos($htmlIn, "<style>");
55 $pos1 = stripos($htmlIn, "</style>");
57 // purify html
58 $config = HTMLPurifier_Config::createDefault();
59 $config->set('URI.AllowedSchemes', array('data' => true, 'http' => true, 'https' => true));
60 $purify = new \HTMLPurifier($config);
61 $htmlIn = $purify->purify($htmlIn);
62 // need to create custom stylesheet for templates
63 // also our styles_pdf.scss isn't being compiled!!!
64 // replace existing style tag in template after purifies removes! why!!!
65 // e,g this scheme gets removed <html><head><body> etc
66 $stylesheet = "<style>.signature {vertical-align: middle;max-height:65px; height:65px !important;width:auto !important;}</style>";
67 if ($pos !== false && $pos1 !== false && !empty($style[0] ?? '')) {
68 $stylesheet = str_replace('</style>', $stylesheet, $style[0]);
70 $htmlIn = "<!DOCTYPE html><html><head>" . $stylesheet . "</head><body>$htmlIn</body></html>";
71 $pdf->writeHtml($htmlIn);
72 return $pdf;
74 public function createPdfDocument($cpid, $formFilename, $documentCategory, $htmlIn)
77 $pdf = $this->createPdfObject($htmlIn);
78 if (!$cpid) {
79 throw new \InvalidArgumentException("Missing Patient ID");
80 echo js_escape("ERROR " . xla("Missing Patient ID"));
81 exit();
83 $data = $pdf->Output($formFilename, 'S');
84 $d = new \Document();
85 $rc = $d->createDocument($cpid, $documentCategory, $formFilename, 'application/pdf', $data);
86 return $rc;