diff --git a/app/Services/PdfMaker/Designs/Utilities/BaseDesign.php b/app/Services/PdfMaker/Designs/Utilities/BaseDesign.php index 9a01ee42da63..c567f20f235c 100644 --- a/app/Services/PdfMaker/Designs/Utilities/BaseDesign.php +++ b/app/Services/PdfMaker/Designs/Utilities/BaseDesign.php @@ -14,14 +14,5 @@ namespace App\Services\PdfMaker\Designs\Utilities; class BaseDesign { - public function setup(): void - { - if (isset($this->context['client'])) { - $this->client = $this->context['client']; - } - - if (isset($this->context['entity'])) { - $this->entity = $this->context['entity']; - } - } + // .. } diff --git a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php index c9694aae58cb..d7f82cb2357d 100644 --- a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php +++ b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php @@ -12,8 +12,79 @@ namespace App\Services\PdfMaker\Designs\Utilities; +use DOMDocument; +use DOMXPath; + trait DesignHelpers { + public $document; + + public $xpath; + + public function setup(): self + { + if (isset($this->context['client'])) { + $this->client = $this->context['client']; + } + + if (isset($this->context['entity'])) { + $this->entity = $this->context['entity']; + } + + $this->document(); + + return $this; + } + + /** + * Initialize local dom document instance. Used for getting raw HTML out of template. + * + * @return $this + */ + public function document(): self + { + $document = new DOMDocument(); + + $document->validateOnParse = true; + @$document->loadHTML($this->html()); + + $this->document = $document; + $this->xpath = new DOMXPath($document); + + return $this; + } + + /** + * Get specific section HTML. + * + * @param string $section + * @param bool $id + * @return null|string + */ + public function getSectionHTML(string $section, $id = true): ?string + { + if ($id) { + $element = $this->document->getElementById($section); + } else { + $elements = $this->document->getElementsByTagName($section); + $element = $elements[0]; + } + + $document = new DOMDocument(); + $document->preserveWhiteSpace = false; + $document->formatOutput = true; + + if ($element) { + $document->appendChild( + $document->importNode($element, true) + ); + + return $document->saveHTML(); + } + + return null; + } + /** * This method will help us decide either we show * one "tax rate" column in the table or 3 custom tax rates. diff --git a/tests/Feature/PdfMaker/ExampleDesign.php b/tests/Feature/PdfMaker/ExampleDesign.php index a20d0681a1b3..df493b96b624 100644 --- a/tests/Feature/PdfMaker/ExampleDesign.php +++ b/tests/Feature/PdfMaker/ExampleDesign.php @@ -2,8 +2,18 @@ namespace Tests\Feature\PdfMaker; +use App\Services\PdfMaker\Designs\Utilities\DesignHelpers; + class ExampleDesign { + use DesignHelpers; + + public $client; + + public $entity; + + public $context; + public function html() { return file_get_contents( diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index 73ad3c8aef17..083899c84d4a 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -2,8 +2,8 @@ namespace Tests\Feature\PdfMaker; +use App\Services\PdfMaker\Designs\Plain; use App\Services\PdfMaker\PdfMaker; -use Spatie\Browsershot\Browsershot; use Tests\TestCase; class PdfMakerTest extends TestCase @@ -322,4 +322,15 @@ class PdfMakerTest extends TestCase $this->assertTrue(true); } + + public function testGetSectionHTMLWorks() + { + $design = new ExampleDesign(); + + $html = $design + ->document() + ->getSectionHTML('product-table'); + + $this->assertStringContainsString('id="product-table"', $html); + } }