diff --git a/Classes/Renderer/AbstractRenderer.php b/Classes/Renderer/AbstractRenderer.php index 9a96b74..5d93568 100644 --- a/Classes/Renderer/AbstractRenderer.php +++ b/Classes/Renderer/AbstractRenderer.php @@ -2,85 +2,90 @@ namespace KayStrobach\Pdf\Renderer; -abstract class AbstractRenderer { - /** - * @var array - */ - protected $options = array(); +abstract class AbstractRenderer +{ + /** + * @var array + */ + protected $options = array(); - /** - * @var integer - */ - protected $errorReporting = null; + /** + * @var integer + */ + protected $errorReporting; - /** - * @param array $options - */ - public function init($options) { - $this->options = $options; - } + /** + * @param array $options + */ + public function init(array $options): void + { + $this->options = $options; + } - /** - * @param $name - * @param null $default - * @return null - */ - public function getOption($name, $default = null) { - if(array_key_exists($name, $this->options)) { - return $this->options[$name]; - } + /** + * @param $name + * @param null $default + * @return null + */ + public function getOption($name, $default = null) + { + if (array_key_exists($name, $this->options)) { + return $this->options[$name]; + } return $default; - } + } - /** - * @param string $html - */ - public function render($html = '') { - $this->prepareEnvironment(); - $buffer = $this->convert($html); - $this->cleanupEnvironment(); - return $buffer; - } + public function render(string $html = '') + { + $this->prepareEnvironment(); + $buffer = $this->convert($html); + $this->cleanupEnvironment(); + return $buffer; + } - /** - * - */ - protected function prepareEnvironment() { - ini_set('memory_limit', '512M'); - $this->disableErrorReporting(); - ob_end_clean(); - $this->initLibrary(); - } + /** + * + */ + protected function prepareEnvironment(): void + { + ini_set('memory_limit', '512M'); + $this->disableErrorReporting(); + ob_end_clean(); + $this->initLibrary(); + } - /** - * - */ - protected function disableErrorReporting() { - $this->errorReporting = error_reporting(); - error_reporting(0); - } + /** + * + */ + protected function disableErrorReporting(): void + { + $this->errorReporting = error_reporting(); + error_reporting(0); + } - /** - * - */ - protected function enableErrorReporting() { - error_reporting($this->errorReporting); - } + /** + * + */ + protected function enableErrorReporting(): void + { + error_reporting($this->errorReporting); + } - /** - * - */ - protected function cleanupEnvironment() { - $this->enableErrorReporting(); - } + /** + * + */ + protected function cleanupEnvironment(): void + { + $this->enableErrorReporting(); + } - /** - * - */ - abstract protected function initLibrary(); + /** + * + */ + abstract protected function initLibrary(); - /** - * - */ - abstract protected function convert($html = '') ; -} \ No newline at end of file + /** + * + */ + abstract protected function convert($html = ''); +} diff --git a/Classes/Renderer/DomPdfRenderer.php b/Classes/Renderer/DomPdfRenderer.php index aa57df0..eab2161 100644 --- a/Classes/Renderer/DomPdfRenderer.php +++ b/Classes/Renderer/DomPdfRenderer.php @@ -2,53 +2,56 @@ namespace KayStrobach\Pdf\Renderer; -class DomPdfRenderer extends AbstractRenderer { +class DomPdfRenderer extends AbstractRenderer +{ - /** - * - */ - protected function initLibrary() { - // the flow way - // https://github.com/dompdf/dompdf/wiki/DOMPDF-and-Composer-Quick-start-guide - define('DOMPDF_ENABLE_AUTOLOAD', false); - $autoloadDir = 'Libraries/dompdf/dompdf/'; - if(is_dir($autoloadDir)) { - require_once(FLOW_PATH_PACKAGES . $autoloadDir . 'dompdf_config.inc.php'); - require_once(FLOW_PATH_PACKAGES . $autoloadDir . 'include/autoload.inc.php'); - } else { - throw new \Exception('please add dompdf/dompdf to your composer.json and install it'); - } - } + /** + * + */ + protected function initLibrary() + { + // the flow way + // https://github.com/dompdf/dompdf/wiki/DOMPDF-and-Composer-Quick-start-guide + define('DOMPDF_ENABLE_AUTOLOAD', false); + $autoloadDir = 'Libraries/dompdf/dompdf/'; + if (is_dir($autoloadDir)) { + require_once(FLOW_PATH_PACKAGES . $autoloadDir . 'dompdf_config.inc.php'); + require_once(FLOW_PATH_PACKAGES . $autoloadDir . 'include/autoload.inc.php'); + } else { + throw new \Exception('please add dompdf/dompdf to your composer.json and install it'); + } + } - /** - * - */ - protected function convert($html = '') { - $domPdf = new \DOMPDF(); - $domPdf->set_options( - array( - 'dpi' => $this->options['dpi'], - 'enable_html5_parser' => $this->options['enableHtml5Parser'], - 'enable_css_float' => $this->options['enableCssFloat'], - 'enable_unicode' => TRUE, - // add some more options - ) - ); - $domPdf->set_paper( - $this->options['papersize'], - $this->options['orientation'] - ); - $domPdf->set_base_path($this->options['basepath']); - $domPdf->load_html($html); - $domPdf->render(); + /** + * + */ + protected function convert($html = '') + { + $domPdf = new \DOMPDF(); + $domPdf->set_options( + array( + 'dpi' => $this->options['dpi'], + 'enable_html5_parser' => $this->options['enableHtml5Parser'], + 'enable_css_float' => $this->options['enableCssFloat'], + 'enable_unicode' => true, + // add some more options + ) + ); + $domPdf->set_paper( + $this->options['papersize'], + $this->options['orientation'] + ); + $domPdf->set_base_path($this->options['basepath']); + $domPdf->load_html($html); + $domPdf->render(); - $domPdf->stream( - $this->getOption('filename'), - array( - 'Attachment' => $this->getOption('forceDownload', 0) - ) - ); + $domPdf->stream( + $this->getOption('filename'), + array( + 'Attachment' => $this->getOption('forceDownload', 0) + ) + ); - $this->cleanupDompdf($domPdf); - } + $this->cleanupDompdf($domPdf); + } } diff --git a/Classes/Renderer/Factory.php b/Classes/Renderer/Factory.php index 1788c6b..8c97df6 100644 --- a/Classes/Renderer/Factory.php +++ b/Classes/Renderer/Factory.php @@ -2,18 +2,20 @@ namespace KayStrobach\Pdf\Renderer; -class Factory { - /** - * @param string $rendererName - * - * @return DomPdfRenderer|AbstractRenderer - */ - public static function get($rendererName) { - if(strtolower($rendererName) === 'mpdf') { - $renderer = new \KayStrobach\Pdf\Renderer\MPdfRenderer(); - } else { - $renderer = new \KayStrobach\Pdf\Renderer\DomPdfRenderer(); - } - return $renderer; - } -} \ No newline at end of file +class Factory +{ + /** + * @param string $rendererName + * + * @return DomPdfRenderer|AbstractRenderer + */ + public static function get($rendererName) + { + if (strtolower($rendererName) === 'mpdf') { + $renderer = new \KayStrobach\Pdf\Renderer\MPdfRenderer(); + } else { + $renderer = new \KayStrobach\Pdf\Renderer\DomPdfRenderer(); + } + return $renderer; + } +} diff --git a/Classes/Renderer/MPdfRenderer.php b/Classes/Renderer/MPdfRenderer.php index d6bf558..0156d7d 100644 --- a/Classes/Renderer/MPdfRenderer.php +++ b/Classes/Renderer/MPdfRenderer.php @@ -60,22 +60,28 @@ protected function convert($html = '') ] ); - $this->systemLogger->debug($this->settings['Renderers']['Mpdf']['WatermarkText']); - if ($this->settings['Renderers']['Mpdf']['WatermarkText'] !== '') { $mpdf->SetWatermarkText($this->settings['Renderers']['Mpdf']['WatermarkText']); $mpdf->showWatermarkText = true; } $mpdf->showWatermarkImage = true; - + $mpdf->debug = $this->getOption('debug'); $mpdf->showImageErrors = true; $mpdf->setAutoTopMargin = true; $mpdf->setAutoBottomMargin = true; - $this->systemLogger->debug('Paperorientation: ' . $orientation); + $this->systemLogger->debug( + 'Pdf settings: ', + [ + 'watermark' => $this->settings['Renderers']['Mpdf']['WatermarkText'], + 'tempDir' => $tempDir, + 'debug' => $mpdf->debug, + 'orientation' => $mpdf->CurOrientation + ] + ); $mpdf->WriteHTML($html); return $mpdf->Output( diff --git a/Classes/View/PdfTemplateView.php b/Classes/View/PdfTemplateView.php index f722df6..4dd278f 100644 --- a/Classes/View/PdfTemplateView.php +++ b/Classes/View/PdfTemplateView.php @@ -1,4 +1,5 @@ assign('PAGENO', '{PAGENO}'); $this->assign('nbpg', '{nbpg}'); @@ -151,8 +153,9 @@ protected function modifyHeader() $filename = $this->getOption('filename'); - $response = $this->controllerContext->getResponse(); - $response->setHeader('Content-Disposition', 'inline; filename="' . $filename . '"'); - $response->setHeader('Content-Type', 'application/pdf; name="fileName.pdf"'); + $response = $this->controllerContext->getResponse()->setContentType('application/pdf; name="' . $filename . '"'); + + #$response->setHeader('Content-Disposition', 'inline; filename="' . $filename . '"'); + #$response->setHeader('Content-Type', 'application/pdf; name="fileName.pdf"'); } } diff --git a/Classes/ViewHelpers/PdfViewHelper.php b/Classes/ViewHelpers/PdfViewHelper.php index d77123b..763ca4a 100644 --- a/Classes/ViewHelpers/PdfViewHelper.php +++ b/Classes/ViewHelpers/PdfViewHelper.php @@ -1,49 +1,50 @@ registerArgument('debug', 'boolean', 'debug or not', 0, 0); - $this->registerArgument('disable', 'boolean', 'disable PDF, output html', 0, 0); - $this->registerArgument('filename', 'string', 'filename for download', 0, 'pdf-' . time() . '.pdf'); - $this->registerArgument('papersize', 'string', 'set the papersize', 0, 'A4'); - $this->registerArgument('orientation', 'string', 'set the orientation', 0, 'portrait'); - $this->registerArgument('basepath', 'string', 'set the basepath', 0, ''); - $this->registerArgument('dpi', 'integer', 'set the quality of the pdf', 0, '96'); - $this->registerArgument('enableHtml5Parser', 'boolean', 'html5parser or not', 0, 1); - $this->registerArgument('enableCssFloat', 'boolean', 'css floating or not', 0, 1); - $this->registerArgument('renderer', 'string', 'define the pdf renderer', 0, 'MPdf'); - } - - /** - * - * @throws \Neos\Flow\Mvc\Exception\StopActionException - * @return string the rendered string - */ - public function render() { - if($this->arguments['disable']) { + public function initializeArguments() + { + $this->registerArgument('debug', 'boolean', 'debug or not', 0, 0); + $this->registerArgument('disable', 'boolean', 'disable PDF, output html', 0, 0); + $this->registerArgument('filename', 'string', 'filename for download', 0, 'pdf-' . time() . '.pdf'); + $this->registerArgument('papersize', 'string', 'set the papersize', 0, 'A4'); + $this->registerArgument('orientation', 'string', 'set the orientation', 0, 'portrait'); + $this->registerArgument('basepath', 'string', 'set the basepath', 0, ''); + $this->registerArgument('dpi', 'integer', 'set the quality of the pdf', 0, '96'); + $this->registerArgument('enableHtml5Parser', 'boolean', 'html5parser or not', 0, 1); + $this->registerArgument('enableCssFloat', 'boolean', 'css floating or not', 0, 1); + $this->registerArgument('renderer', 'string', 'define the pdf renderer', 0, 'MPdf'); + } + + /** + * + * @return string the rendered string + * @throws \Neos\Flow\Mvc\Exception\StopActionException + */ + public function render() + { + if ($this->arguments['disable']) { return $this->renderChildren(); } $renderer = Factory::get($this->arguments['renderer']); $renderer->init($this->arguments); - $renderer->render($this->renderChildren()); - - //use instead of exit ;) - throw new StopActionException(); - } + return $renderer->render($this->renderChildren()); + } } diff --git a/README.md b/README.md index 94bdcf7..a77c1c8 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,12 @@ To achieve the on the fly rendering easily it uses a fluid viewhelper. You need one of these package installed via composer, the package itself checks if the library is missing and asks you to install it. -* dompdf/dompdf +* ~~dompdf/dompdf~~ (deprecated) * mpdf/mpdf -Controller Usage ----------------- +Controller Usage - do not use the viewHelper anymore, stick to the dedicated view! +----------------------------------------------------------------------------------- The recent version of the package contains a new view. This view can be included directly in the controller e.g. like this: @@ -133,4 +133,4 @@ Rendering barcodes ``` -``` \ No newline at end of file +```