diff --git a/app/Services/PdfMaker/PdfMaker.php b/app/Services/PdfMaker/PdfMaker.php index 7640b4d95c1b..6118c6854685 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) @@ -61,14 +67,16 @@ class PdfMaker public function getCompiledHTML($final = false) { + $this->processOptions(); + 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..f0f16e09a180 100644 --- a/app/Services/PdfMaker/PdfMakerUtilities.php +++ b/app/Services/PdfMaker/PdfMakerUtilities.php @@ -47,7 +47,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 +108,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 +152,32 @@ trait PdfMakerUtilities return $element; } + + public function processOptions() + { + if (isset($this->options['print_css']) && $this->options['print_css']) { + $this->insertPrintCSS(); + } + } + + public function insertPrintCSS() + { + $css = '.page-header,.page-header-space{height:100px}.page-footer,.page-footer-space{height:50px}.page-footer{position:fixed;bottom:0;width:100%;border-top:1px solid #000;background:#ff0}.page-header{position:fixed;top:0;width:100%;border-bottom:1px solid #000;background:#ff0}.page{page-break-after:always}@page{margin:20mm}@media print{thead{display:table-header-group}tfoot{display:table-footer-group}button{display:none}body{margin:0}}'; + + $css_node = $this->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); + } + } } diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index cbe58711d325..09f30e16ce6f 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -18,7 +18,7 @@ class PdfMakerTest extends TestCase public function testDesignLoadsCorrectly() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $maker = new PdfMaker($this->state); @@ -29,7 +29,7 @@ class PdfMakerTest extends TestCase public function testHtmlDesignLoadsCorrectly() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $maker = new PdfMaker($this->state); @@ -42,7 +42,7 @@ class PdfMakerTest extends TestCase public function testGetSectionUtility() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $maker = new PdfMaker($this->state); @@ -55,7 +55,7 @@ class PdfMakerTest extends TestCase public function testTableAttributesAreInjected() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $state = [ 'template' => [ @@ -93,7 +93,7 @@ class PdfMakerTest extends TestCase public function testVariablesAreReplaced() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $state = [ @@ -133,7 +133,7 @@ class PdfMakerTest extends TestCase public function testElementContentIsGenerated() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $state = [ @@ -184,7 +184,7 @@ class PdfMakerTest extends TestCase public function testConditionalRenderingOfElements() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $maker1 = new PdfMaker([ @@ -226,7 +226,7 @@ class PdfMakerTest extends TestCase public function testOrderingElements() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $maker = new PdfMaker([ @@ -286,7 +286,7 @@ class PdfMakerTest extends TestCase public function testGeneratingPdf() { - $this->markTestSkipped('STUB broken tests'); + $this->markTestSkipped('STUB broken tests'); $state = [ @@ -356,4 +356,34 @@ class PdfMakerTest extends TestCase $this->assertStringContainsString('id="product-table"', $html); } + + public function testWrapperHTMLWorks() + { + $design = new ExampleDesign(); + + $html = $design + ->document() + ->getSectionHTML('product-table'); + + $state = [ + 'template' => [], + 'variables' => [ + 'labels' => [], + 'values' => [], + ], + 'options' => [ + 'print_css' => true, + ], + ]; + + $maker = new PdfMaker($state); + + $maker + ->design(ExampleDesign::class) + ->build(); + + exec('echo "" > storage/logs/laravel.log'); + + info($maker->getCompiledHTML(true)); + } }