diff --git a/src/UnicaenPdf/Exporter/PdfExporter.php b/src/UnicaenPdf/Exporter/PdfExporter.php index e02c6abe69b6beb89b771f6062a14c939783ff3f..ed9149bbadd76cd932d5e088db5d6648273b51ba 100644 --- a/src/UnicaenPdf/Exporter/PdfExporter.php +++ b/src/UnicaenPdf/Exporter/PdfExporter.php @@ -2,15 +2,18 @@ namespace UnicaenPdf\Exporter; -use Mpdf\Mpdf as mPDF; +use Laminas\View\Renderer\PhpRenderer; +use Laminas\View\Resolver\TemplatePathStack; use LogicException; +use Mpdf\Mpdf as mPDF; use RuntimeException; -use Laminas\View\Renderer\PhpRenderer; /** * Classe utilitaire permettant de fabriquer un document PDF à partir * de code HTML. * + * NB: bibliothèque mPDF requise (http://www.mpdf1.com/mpdf/). + * * @author Unicaen */ class PdfExporter implements ExporterInterface @@ -61,6 +64,14 @@ class PdfExporter implements ExporterInterface * @var array */ private $scriptVars = []; + /** + * @var array + */ + private $footerVars = []; + /** + * @var array + */ + private $headerVars = []; /** * @var boolean */ @@ -128,6 +139,13 @@ class PdfExporter implements ExporterInterface */ private $logo; + /** + * Chemin absolu du répertoire contenant les scripts de vue par défaut. + * + * @var string + */ + protected $defaultScriptsDirPath = __DIR__ . '/scripts'; + /** * Constructeur. * @@ -150,6 +168,9 @@ class PdfExporter implements ExporterInterface $this->orientationPaysage = $orientationPaysage; $this->defaultFontSize = $defaultFontSize; + $this->setHeaderScriptToDefault(); + $this->setFooterScriptToDefault(); + // $this->setLogo(file_get_contents(__DIR__ . "/../../../public/unicaen/app/img/logo-unicaen.png")); } @@ -161,7 +182,7 @@ class PdfExporter implements ExporterInterface * @param string|null $oddOrEven 'O', 'E' ou null * @return self */ - public function setHeaderScript(string $script = null, string $oddOrEven = null): self + public function setHeaderScript(string $script = null, string $oddOrEven = null, array $scriptVars = []): self { if (!$script) { $this->headerScripts = []; @@ -174,6 +195,36 @@ class PdfExporter implements ExporterInterface $this->headerScripts['O'] = $script; $this->headerScripts['E'] = $script; } + if ($scriptVars) { + $this->headerVars = $scriptVars; + } + + return $this; + } + + /** + * Spécifie qu'aucun script ne soit utilisé pour les entêtes de pages. + * Autrement dit, les pages du document PDF n'auront pas d'entête ! + * + * @return self + */ + public function setHeaderScriptToNone() + { + $this->headerScripts = array(); + + return $this; + } + + /** + * Restaure le script de vue par défaut pour les entêtes de pages (pages paires et impaires). + * + * @return self + */ + public function setHeaderScriptToDefault() + { + $this->headerScripts['O'] = 'header-odd.phtml'; + $this->headerScripts['E'] = 'header-even.phtml'; + return $this; } @@ -225,7 +276,7 @@ class PdfExporter implements ExporterInterface * @param string|null $oddOrEven 'O' (pages impaires seulement), 'E' (pages paires seulement) ou null (pages paires et impaires) * @return self */ - public function setFooterScript(string $script = null, string $oddOrEven = null): self + public function setFooterScript(string $script = null, string $oddOrEven = null, array $scriptVars =[]): self { if (!$script) { $this->footerScripts = []; @@ -237,6 +288,36 @@ class PdfExporter implements ExporterInterface $this->footerScripts['O'] = $script; $this->footerScripts['E'] = $script; } + if ($scriptVars) { + $this->footerVars = $scriptVars; + } + + return $this; + } + + /** + * Restaure le script de vue par défaut pour les pieds de pages (pages paires et impaires). + * + * @return self + */ + public function setFooterScriptToDefault() + { + $this->footerScripts['O'] = 'footer-odd.phtml'; + $this->footerScripts['E'] = 'footer-even.phtml'; + + return $this; + } + + /** + * Spécifie qu'aucun script ne soit utilisé pour les pieds de pages. + * Autrement dit, les pages du document PDF n'auront pas de pied de page ! + * + * @return self + */ + public function setFooterScriptToNone() + { + $this->footerScripts = array(); + return $this; } @@ -254,9 +335,10 @@ class PdfExporter implements ExporterInterface * PdfExporter::DESTINATION_STRING : * Return the document as a string. filename is ignored. * - * @param string $destination Exemple : PdfExporter::self - * @param string|null $memoryLimit + * @param string $destination Exemple : PdfExporter::DESTINATION_BROWSER + * @param string|null $memoryLimit Quantité de mémoire maximum utilisée (memory_limit PHP), ex: '256M' * @return string|null + * * @throws \Mpdf\MpdfException */ public function export(string $filename = null, string $destination = PdfExporter::DESTINATION_BROWSER, string $memoryLimit = null) @@ -392,29 +474,22 @@ class PdfExporter implements ExporterInterface { $headerOdd = $headerEven = null; - $scriptVars = array( + $scriptVars = [ 'headerTitle' => $this->headerTitle, 'headerSubtitle' => $this->headerSubtitle, - 'logo' => $this->logo); - $scriptVars = array_merge($scriptVars, current($this->scriptVars) ?: []); + 'logo' => $this->logo, + ]; + $scriptVars = array_merge($this->headerVars, $scriptVars); // le logo doit être passé ainsi pour pouvoir être référencé dans la balise <img> sous la forme "var:logo" $this->getMpdf()->imageVars['logo'] = $this->logo; if (isset($this->headerScripts['O'])) { $headerOdd = $this->getRenderer()->render($this->headerScripts['O'], $scriptVars); - } elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/header-odd.phtml')) { - ob_start(); - include $filepath; - $headerOdd = ob_get_clean(); } if (isset($this->headerScripts['E'])) { $headerEven = $this->getRenderer()->render($this->headerScripts['E'], $scriptVars); - } elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/header-even.phtml')) { - ob_start(); - include $filepath; - $headerEven = ob_get_clean(); } if ($headerOdd) { @@ -482,24 +557,17 @@ class PdfExporter implements ExporterInterface { $footerOdd = $footerEven = null; - $scriptVars = array( - 'footerTitle' => $this->footerTitle); - $scriptVars = array_merge($scriptVars, current($this->scriptVars) ?: []); + $scriptVars = [ + 'footerTitle' => $this->footerTitle, + ]; + $scriptVars = array_merge($this->footerVars, $scriptVars); if (isset($this->footerScripts['O'])) { $footerOdd = $this->getRenderer()->render($this->footerScripts['O'], $scriptVars); - } elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/footer-odd.phtml')) { - ob_start(); - include $filepath; - $footerOdd = ob_get_clean(); } if (isset($this->footerScripts['E'])) { $footerEven = $this->getRenderer()->render($this->footerScripts['E'], $scriptVars); - } elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/footer-even.phtml')) { - ob_start(); - include $filepath; - $footerEven = ob_get_clean(); } if ($footerOdd) { @@ -521,6 +589,11 @@ class PdfExporter implements ExporterInterface public function setRenderer(PhpRenderer $renderer): self { $this->renderer = $renderer; + + $this->renderer->resolver()->attach(new TemplatePathStack(['script_paths' => [ + $this->defaultScriptsDirPath, + ]])); + return $this; }