Skip to content

Commit d8641d8

Browse files
committed
simplified html document interface, removed superfluous events
Signed-off-by: Christoph Massmann <[email protected]>
1 parent aa8a6c0 commit d8641d8

File tree

5 files changed

+14
-30
lines changed

5 files changed

+14
-30
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ Internally it uses the DomPDF library for PDF generation and TCPDF for merging.
1212
$pdf = Vianetz\Pdf\Model\PdfFactory::general()->create();
1313

1414
// Create the document. You can return any kind of HTML content here.
15-
$document = new \Vianetz\Pdf\Model\HtmlDocument();
16-
$document->setHtmlContents('<strong>Hello</strong> World!');
15+
$document = new \Vianetz\Pdf\Model\HtmlDocument('<strong>Hello</strong> World!');
1716

1817
// Add our document to the pdf. You can add as many documents as you like
1918
// as they will all be merged into one PDF file.

src/Model/HtmlDocument.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@
2222
class HtmlDocument implements HtmlDocumentInterface
2323
{
2424
private string $htmlContents;
25-
private string $pdfBackgroundFile = '';
26-
private string $pdfBackgroundFileForFirstPage = '';
25+
private ?string $pdfBackgroundFile = null;
26+
private ?string $pdfBackgroundFileForFirstPage = null;
2727

28-
public function setHtmlContents(string $htmlContents): self
28+
public function __construct(string $htmlContents, ?string $pdfBackgroundFile = null, ?string $pdfBackgroundFileForFirstPage = null)
2929
{
3030
$this->htmlContents = $htmlContents;
31-
32-
return $this;
31+
$this->pdfBackgroundFile = $pdfBackgroundFile;
32+
$this->pdfBackgroundFileForFirstPage = $pdfBackgroundFileForFirstPage;
3333
}
3434

3535
public function getHtmlContents(): string
3636
{
3737
return $this->htmlContents;
3838
}
3939

40-
public function getPdfBackgroundFile(): string
40+
public function getPdfBackgroundFile(): ?string
4141
{
4242
return $this->pdfBackgroundFile;
4343
}
@@ -47,7 +47,7 @@ public function setPdfBackgroundFile(string $pdfBackgroundFile): void
4747
$this->pdfBackgroundFile = $pdfBackgroundFile;
4848
}
4949

50-
public function getPdfBackgroundFileForFirstPage(): string
50+
public function getPdfBackgroundFileForFirstPage(): ?string
5151
{
5252
return $this->pdfBackgroundFileForFirstPage;
5353
}
@@ -56,9 +56,4 @@ public function setPdfBackgroundFileForFirstPage(string $pdfBackgroundFileForFir
5656
{
5757
$this->pdfBackgroundFileForFirstPage = $pdfBackgroundFileForFirstPage;
5858
}
59-
60-
public function getDocumentType(): string
61-
{
62-
return '';
63-
}
6459
}

src/Model/HtmlDocumentInterface.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ interface HtmlDocumentInterface
2323
{
2424
public function getHtmlContents(): string;
2525

26-
public function getPdfBackgroundFile(): string;
26+
public function getPdfBackgroundFile(): ?string;
2727

28-
public function setPdfBackgroundFile(string $pdfFile): void;
29-
30-
public function getPdfBackgroundFileForFirstPage(): string;
31-
32-
public function setPdfBackgroundFileForFirstPage(string $pdfFile): void;
33-
34-
public function getDocumentType(): string;
28+
public function getPdfBackgroundFileForFirstPage(): ?string;
3529
}

src/Model/Pdf.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,16 @@ private function renderPdfContentsForAllDocuments(): void
134134
$this->eventManager->dispatch('vianetz_pdf_document_render_before', ['document' => $documentInstance]);
135135

136136
if ($documentInstance instanceof HtmlDocumentInterface) {
137-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_before', ['document' => $documentInstance]);
138-
139137
$pdfContents = $this->generator->renderPdfDocument($documentInstance);
140138
if (empty($pdfContents)) {
141139
continue;
142140
}
143141

144142
$hasData = true;
145143

146-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_after', ['document' => $documentInstance]);
147-
148144
$this->pdfMerge->mergePdfString($pdfContents, $documentInstance->getPdfBackgroundFile(), $documentInstance->getPdfBackgroundFileForFirstPage());
149145

150-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_merge_after', [
146+
$this->eventManager->dispatch('vianetz_pdf_document_merge_after', [
151147
'merger' => $this->pdfMerge,
152148
'document' => $documentInstance,
153149
]);

src/Test/PdfTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
use PHPUnit\Framework\TestCase;
2323
use Vianetz\Pdf\Model\Config;
2424
use Vianetz\Pdf\Model\Generator\AbstractGenerator;
25+
use Vianetz\Pdf\Model\HtmlDocument;
2526
use Vianetz\Pdf\Model\PdfFactory;
2627
use Vianetz\Pdf\NoDataException;
2728

2829
final class PdfTest extends TestCase
2930
{
3031
private const TMP_DIR = './tmp_dir/';
3132

32-
private function getDocumentMock(): \Vianetz\Pdf\Model\HtmlDocument
33+
private function getDocumentMock(): HtmlDocument
3334
{
3435
/** @var \Vianetz\Pdf\Model\HtmlDocument $document */
35-
$document = new \Vianetz\Pdf\Model\HtmlDocument();
36-
$document->setHtmlContents('<html><body>This is the <strong>pdf-generator</strong> test!</body></html>');
36+
$document = new HtmlDocument('<html><body>This is the <strong>pdf-generator</strong> test!</body></html>');
3737

3838
return $document;
3939
}

0 commit comments

Comments
 (0)