/** * Attach the details view as a PDF to the email * * @param array &$thisAttachments Attachments * * @throws RuntimeException * * @return void */ protected function pdfAttachement(&$thisAttachments) { $params = $this->getParams(); if ($params->get('attach_pdf', 0) == 0) { return; } $model = $this->getModel(); $document = JFactory::getDocument(); $config = JFactory::getConfig(); $docType = $document->getType(); $document->setType('pdf'); $app = JFactory::getApplication(); $input = $app->input; $orig['details'] = $input->get('view'); $orig['format'] = $input->get('format'); $input->set('view', 'details'); $input->set('format', 'pdf'); // Ensure the package is set to fabrik $prevUserState = $app->getUserState('com_fabrik.package'); $app->setUserState('com_fabrik.package', 'fabrik'); try { // Require files and set up DOM pdf require_once JPATH_SITE . '/components/com_fabrik/helpers/pdf.php'; require_once JPATH_SITE . '/components/com_fabrik/controllers/details.php'; FabrikPDFHelper::iniDomPdf(); $dompdf = new DOMPDF(); $size = strtoupper($params->get('pdf_size', 'A4')); $orientation = $params->get('pdf_orientation', 'portrait'); $dompdf->set_paper($size, $orientation); // Store in output buffer ob_start(); $controller = new FabrikControllerDetails(); $controller->display(); $html = ob_get_contents(); ob_end_clean(); // Load the HTML into DOMPdf and render it. $dompdf->load_html($html); $dompdf->render(); // Store the file in the tmp folder so it can be attached $file = $config->get('tmp_path') . '/' . JStringNormalise::toDashSeparated($model->getForm()->label . '-' . $input->getString('rowid')) . '.pdf'; $pdf = $dompdf->output(); if (JFile::write($file, $pdf)) { $thisAttachments[] = $file; } else { throw new RuntimeException('Could not write PDF file to tmp folder'); } } catch (Exception $e) { $app->enqueueMessage($e->getMessage(), 'error'); } // Set the package back to what it was before rendering the module $app->setUserState('com_fabrik.package', $prevUserState); // Reset input foreach ($orig as $key => $val) { $input->set($key, $val); } // Reset document type $document->setType($docType); }
/** * Attach the details view as a PDF to the email * * @param array &$thisAttachments Attachments * * @throws RuntimeException * * @return void */ protected function pdfAttachment(&$thisAttachments) { $params = $this->getParams(); if ($params->get('attach_pdf', 0) == 0) { return; } /** @var FabrikFEModelForm $model */ $model = $this->getModel(); $document = JFactory::getDocument(); $docType = $document->getType(); $document->setType('pdf'); $input = $this->app->input; $orig['details'] = $input->get('view'); $orig['format'] = $input->get('format'); $input->set('view', 'details'); $input->set('format', 'pdf'); // set editable false so things like getFormCss() pick up the detail, not form, CSS $model->setEditable(false); // Ensure the package is set to fabrik $prevUserState = $this->app->getUserState('com_fabrik.package'); $this->app->setUserState('com_fabrik.package', 'fabrik'); try { $model->getFormCss(); foreach ($document->_styleSheets as $url => $ss) { $url = htmlspecialchars_decode($url); $formCss[] = file_get_contents($url); } // Require files and set up DOM pdf require_once JPATH_SITE . '/components/com_fabrik/helpers/pdf.php'; require_once JPATH_SITE . '/components/com_fabrik/controllers/details.php'; FabrikPDFHelper::iniDomPdf(); $domPdf = new DOMPDF(); $size = strtoupper($params->get('pdf_size', 'A4')); $orientation = $params->get('pdf_orientation', 'portrait'); $domPdf->set_paper($size, $orientation); $controller = new FabrikControllerDetails(); /** * $$$ hugh - stuff our model in there, with already formatted data, so it doesn't get rendered * all over again by the view, with unformatted data. Should probably use a setModel() method * here instead of poking in to the _model, but I don't think there is a setModel for controllers? */ $controller->_model = $model; $controller->_model->data = $this->getProcessData(); // Store in output buffer ob_start(); $controller->display(); $html = ob_get_contents(); ob_end_clean(); if (!empty($formCss)) { $html = "<style>\n" . implode("\n", $formCss) . "</style>\n" . $html; } // Load the HTML into DOMPdf and render it. $domPdf->load_html(utf8_decode($html)); $domPdf->render(); // Store the file in the tmp folder so it can be attached $layout = FabrikHelperHTML::getLayout('form.fabrik-pdf-title'); $displayData = new stdClass(); $displayData->doc = $document; $displayData->model = $model; $fileName = $layout->render($displayData); $file = $this->config->get('tmp_path') . '/' . JStringNormalise::toDashSeparated($fileName) . '.pdf'; $pdf = $domPdf->output(); if (JFile::write($file, $pdf)) { $thisAttachments[] = $file; } else { throw new RuntimeException('Could not write PDF file to tmp folder'); } } catch (Exception $e) { $this->app->enqueueMessage($e->getMessage(), 'error'); } // set back to editable $model->setEditable(true); // Set the package back to what it was before rendering the module $this->app->setUserState('com_fabrik.package', $prevUserState); // Reset input foreach ($orig as $key => $val) { $input->set($key, $val); } // Reset document type $document->setType($docType); }