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.
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
;
21 use HTMLPurifier_Config
;
22 use OpenEMR\Pdf\Config_Mpdf
;
24 class PatientPortalPDFDocumentCreator
26 public function createPdfObject($htmlIn)
28 $config_mpdf = Config_Mpdf
::getConfigMpdf();
29 $pdf = new Mpdf($config_mpdf);
30 if ($_SESSION['language_direction'] == 'rtl') {
31 $pdf->SetDirectionality('rtl');
34 // snatch style tags content to insert after content purified
35 $style_flag = preg_match('#<\s*?style\b[^>]*>(.*?)</style\b[^>]*>#s', $htmlIn, $style_matches);
36 $style = str_replace('<style type="text/css">', '<style>', $style_matches);
37 $pos = stripos($htmlIn, "<style>");
38 $pos1 = stripos($htmlIn, "</style>");
41 $config = HTMLPurifier_Config
::createDefault();
42 $config->set('URI.AllowedSchemes', array('data' => true, 'http' => true, 'https' => true));
43 $purify = new \
HTMLPurifier($config);
44 $htmlIn = $purify->purify($htmlIn);
45 // need to create custom stylesheet for templates
46 // also our styles_pdf.scss isn't being compiled!!!
47 // replace existing style tag in template after purifies removes! why!!!
48 // e,g this scheme gets removed <html><head><body> etc
49 $stylesheet = "<style>.signature {vertical-align: middle;max-height:65px; height:65px !important;width:auto !important;}</style>";
50 if ($pos !== false && $pos1 !== false && !empty($style[0] ??
'')) {
51 $stylesheet = str_replace('</style>', $stylesheet, $style[0]);
53 $htmlIn = "<!DOCTYPE html><html><head>" . $stylesheet . "</head><body>$htmlIn</body></html>";
54 $pdf->writeHtml($htmlIn);
57 public function createPdfDocument($cpid, $formFilename, $documentCategory, $htmlIn): \Document
60 $pdf = $this->createPdfObject($htmlIn);
62 throw new \
InvalidArgumentException("Missing Patient ID");
63 echo js_escape("ERROR " . xla("Missing Patient ID"));
66 $data = $pdf->Output($formFilename, 'S');
68 $rc = $d->createDocument($cpid, $documentCategory, $formFilename, 'application/pdf', $data);
72 throw new \
RuntimeException("Failed to create document: " . $rc);