mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Implement getting raw HTML out of sections
This commit is contained in:
parent
8a23ed35ea
commit
e99bd59aa0
@ -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'];
|
||||
}
|
||||
}
|
||||
// ..
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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(
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user