From eaad980f1e0031baa2a2bf49b29675cab993b385 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 11 Sep 2023 15:54:20 +1000 Subject: [PATCH] Working on twig --- app/Services/PdfMaker/PdfMaker.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/Services/PdfMaker/PdfMaker.php b/app/Services/PdfMaker/PdfMaker.php index 26c375c5671e..505327c126c8 100644 --- a/app/Services/PdfMaker/PdfMaker.php +++ b/app/Services/PdfMaker/PdfMaker.php @@ -74,6 +74,28 @@ class PdfMaker $this->updateElementProperties($this->data['template']); } + if(isset($this->data['template'])) { + $contents= $this->document->getElementsByTagName('twig'); + + foreach ($contents as $content) { + + $template = $content->ownerDocument->saveHTML($content); + + $loader = new \Twig\Loader\FilesystemLoader(storage_path()); + $twig = new \Twig\Environment($loader); + $template = $twig->createTemplate($template); + $template = $template->render([ + 'invoice' => \App\Models\Invoice::first()->toArray(), + ]); + + $f = $this->document->createDocumentFragment(); + $f->appendXML($template); + + $content->parentNode->replaceChild($f, $content); + } + + } + if (isset($this->data['variables'])) { $this->updateVariables($this->data['variables']); } @@ -89,8 +111,9 @@ class PdfMaker */ public function getCompiledHTML($final = false) { - $html = $this->document->saveHTML(); + $html = $this->document->saveHTML(); + nlog($html); return str_replace('%24', '$', $html); } }