Skip to content

Commit

Permalink
[TASK] make comaptible with flow 6
Browse files Browse the repository at this point in the history
  • Loading branch information
kaystrobach committed Nov 16, 2021
1 parent b1c81b3 commit 847b9a7
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 173 deletions.
147 changes: 76 additions & 71 deletions Classes/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '') ;
}
/**
*
*/
abstract protected function convert($html = '');
}
93 changes: 48 additions & 45 deletions Classes/Renderer/DomPdfRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
32 changes: 17 additions & 15 deletions Classes/Renderer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
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;
}
}
14 changes: 10 additions & 4 deletions Classes/Renderer/MPdfRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions Classes/View/PdfTemplateView.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace KayStrobach\Pdf\View;

use KayStrobach\Pdf\Renderer\Factory;
Expand Down Expand Up @@ -129,7 +130,8 @@ class PdfTemplateView extends StandaloneView implements ViewInterface
* @param string $actionName
* @return string
*/
public function render($actionName = null) {
public function render($actionName = null)
{
$this->assign('PAGENO', '{PAGENO}');
$this->assign('nbpg', '{nbpg}');

Expand All @@ -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"');
}
}
Loading

0 comments on commit 847b9a7

Please sign in to comment.