diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php index 5b3ecbdfe724..2d81fcdd7a66 100644 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ b/app/Jobs/Invoice/CreateInvoicePdf.php @@ -99,6 +99,10 @@ class CreateInvoicePdf implements ShouldQueue 'product-table-columns' => $pdf_variables['product_columns'], ]), 'variables' => $html->generateLabelsAndValues(), + 'options' => [ + 'all_pages_header' => $this->invoice->client->getSetting('all_pages_header'), + 'all_pages_footer' => $this->invoice->client->getSetting('all_pages_footer'), + ], ]; $maker = new PdfMakerService($state); @@ -110,6 +114,8 @@ class CreateInvoicePdf implements ShouldQueue //todo - move this to the client creation stage so we don't keep hitting this unnecessarily Storage::makeDirectory($path, 0775); + info($maker->getCompiledHTML()); + $pdf = $this->makePdf(null, null, $maker->getCompiledHTML()); $instance = Storage::disk($this->disk)->put($file_path, $pdf); diff --git a/app/Jobs/Quote/CreateQuotePdf.php b/app/Jobs/Quote/CreateQuotePdf.php index d1b759d73ef5..e7bb0d2381ea 100644 --- a/app/Jobs/Quote/CreateQuotePdf.php +++ b/app/Jobs/Quote/CreateQuotePdf.php @@ -97,6 +97,10 @@ class CreateQuotePdf implements ShouldQueue 'product-table-columns' => $pdf_variables['product_columns'], ]), 'variables' => $html->generateLabelsAndValues(), + 'options' => [ + 'all_pages_header' => $this->quote->client->getSetting('all_pages_header'), + 'all_pages_footer' => $this->quote->client->getSetting('all_pages_footer'), + ], ]; $maker = new PdfMakerService($state); diff --git a/app/Services/PdfMaker/PdfMaker.php b/app/Services/PdfMaker/PdfMaker.php index 7640b4d95c1b..7e0e793fcbcd 100644 --- a/app/Services/PdfMaker/PdfMaker.php +++ b/app/Services/PdfMaker/PdfMaker.php @@ -32,9 +32,15 @@ class PdfMaker '' => '', ]; + private $options; + public function __construct(array $data) { $this->data = $data; + + if (array_key_exists('options', $data)) { + $this->options = $data['options']; + } } public function design(string $design) @@ -56,6 +62,8 @@ class PdfMaker $this->updateVariables($this->data['variables']); } + $this->processOptions(); + return $this; } @@ -63,12 +71,12 @@ class PdfMaker { if ($final) { $html = $this->document->saveXML(); - + $filtered = strtr($html, $this->filters); - + return $filtered; } - + return $this->document->saveXML(); } } diff --git a/app/Services/PdfMaker/PdfMakerUtilities.php b/app/Services/PdfMaker/PdfMakerUtilities.php index 6a20c3fe66a5..af2444478e7b 100644 --- a/app/Services/PdfMaker/PdfMakerUtilities.php +++ b/app/Services/PdfMaker/PdfMakerUtilities.php @@ -13,6 +13,7 @@ namespace App\Services\PdfMaker; use DOMDocument; +use DOMDomError; use DOMXPath; trait PdfMakerUtilities @@ -47,7 +48,11 @@ trait PdfMakerUtilities public function updateElementProperties(array $elements) { foreach ($elements as $element) { - $node = $this->document->getElementById($element['id']); + if (isset($element['tag'])) { + $node = $this->document->getElementsByTagName($element['tag'])->item(0); + } else { + $node = $this->document->getElementById($element['id']); + } if (isset($element['properties'])) { foreach ($element['properties'] as $property => $value) { @@ -104,7 +109,6 @@ trait PdfMakerUtilities public function createElementContent($element, $children) { foreach ($children as $child) { - $_child = $this->document->createElement($child['element'], $child['content']); $element->appendChild($_child); @@ -149,4 +153,134 @@ trait PdfMakerUtilities return $element; } + + public function processOptions() + { + if (!isset($this->options['all_pages_header']) && !isset($this->options['all_pages_footer'])) { + return; + } + + $this->insertPrintCSS(); + $this->wrapIntoTable(); + } + + public function insertPrintCSS() + { + $css = <<document->createTextNode($css); + + $style = $this->document->getElementsByTagName('style')->item(0); + + if ($style) { + return $style->appendChild($css_node); + } + + $head = $this->document->getElementsByTagName('head')->item(0); + + if ($head) { + $style_node = $this->document->createElement('style', $css); + + return $head->appendChild($style_node); + } + } + + public function wrapIntoTable() + { + $markup = << + + + + + + + + + + + + + + + + + + + + + + + EOT; + + $document = new DOMDocument(); + $document->loadHTML($markup); + + $table = $document->getElementById('page-container'); + + $body = $this->document->getElementsByTagName('body') + ->item(0); + + $body->appendChild( + $this->document->importNode($table, true) + ); + + for ($i = 0; $i < $body->childNodes->length; $i++) { + $element = $body->childNodes->item($i); + + if ($element->nodeType !== 1) { + continue; + } + + if ( + $element->getAttribute('id') == 'header' || + $element->getAttribute('id') == 'footer' || + $element->getAttribute('id') === 'page-container' + ) { + continue; + } + + $clone = $element->cloneNode(true); + $element->parentNode->removeChild($element); + + $this->document->getElementById('repeat-content')->appendChild($clone); + } + + if ( + $header = $this->document->getElementById('header') && + isset($this->data['options']['all_pages_header']) && + $this->data['options']['all_pages_header'] + ) { + + $header = $this->document->getElementById('header'); + $clone = $header->cloneNode(true); + + $header->parentNode->removeChild($header); + $this->document->getElementById('repeat-header')->appendChild($clone); + } + + if ( + $footer = $this->document->getElementById('footer') && + isset($this->data['options']['all_pages_footer']) && + $this->data['options']['all_pages_footer'] + ) { + $footer = $this->document->getElementById('footer'); + $clone = $footer->cloneNode(true); + + $footer->parentNode->removeChild($footer); + $this->document->getElementById('repeat-footer')->appendChild($clone); + } + } } diff --git a/resources/views/pdf-designs/bold.html b/resources/views/pdf-designs/bold.html index 522236b12649..20ad3a369a34 100644 --- a/resources/views/pdf-designs/bold.html +++ b/resources/views/pdf-designs/bold.html @@ -26,7 +26,7 @@ -
+