Skip to content

Commit 571714e

Browse files
committed
further improved interfaces
Signed-off-by: Christoph Massmann <[email protected]>
1 parent 7af2bf3 commit 571714e

16 files changed

+76
-145
lines changed

src/Model/PdfInterface.php renamed to src/Model/CanSave.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919

2020
namespace Vianetz\Pdf\Model;
2121

22-
interface PdfInterface
22+
interface CanSave
2323
{
24-
/** @throws \Vianetz\Pdf\NoDataException */
25-
public function getContents(): string;
26-
2724
/**
2825
* @api
2926
* @return bool true in case of success

src/Model/Generator/AbstractGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace Vianetz\Pdf\Model\Generator;
2121

2222
use Vianetz\Pdf\Model\Config;
23-
use Vianetz\Pdf\Model\GeneratorInterface;
23+
use Vianetz\Pdf\Model\Pdfable;
2424

2525
/**
2626
* A note on PDF merging:
@@ -32,21 +32,18 @@
3232
* A note on naming convention:
3333
* - "document" is the PDF contents of a source that serves as input for the generator (e.g. html string)
3434
*/
35-
abstract class AbstractGenerator implements GeneratorInterface
35+
abstract class AbstractGenerator implements Pdfable // @todo rename Generator to Converter
3636
{
3737
public const DEBUG_FILE_NAME = 'vianetz_pdf_generator_debug.html';
3838
protected Config $config;
3939

40+
abstract public function convert(string $html): self;
41+
4042
public function __construct(?Config $config = null)
4143
{
4244
$this->config = $config ?? new Config();
4345
}
4446

45-
protected function replaceSpecialChars(string $htmlContents): string
46-
{
47-
return $htmlContents;
48-
}
49-
5047
protected function writeDebugFile(string $fileContents): bool
5148
{
5249
if (! $this->config->isDebugMode()) {

src/Model/Generator/Dompdf.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
namespace Vianetz\Pdf\Model\Generator;
2121

22-
use Vianetz\Pdf\Model\HtmlDocumentInterface;
23-
2422
/**
2523
* Known limitations of Dompdf:
2624
* - colspan is not working properly (table is moved to the top of the page)
@@ -36,15 +34,22 @@ final class Dompdf extends AbstractGenerator
3634
private \Dompdf\Dompdf $domPdf;
3735

3836
/** @throws \Exception */
39-
public function renderPdfDocument(HtmlDocumentInterface $documentModel): string
37+
public function convert(string $html): self
4038
{
39+
$this->writeDebugFile($html);
40+
4141
$this->initPdf();
4242

43-
$this->domPdf->loadHtml($this->getHtmlContentsForDocument($documentModel));
43+
$this->domPdf->loadHtml($html);
4444
$this->domPdf->render();
4545

4646
$this->injectPageCount();
4747

48+
return $this;
49+
}
50+
51+
public function toPdf(): string
52+
{
4853
return $this->domPdf->output() ?? '';
4954
}
5055

@@ -60,21 +65,6 @@ protected function initPdf(): self
6065
return $this;
6166
}
6267

63-
/**
64-
* Return HTML contents for one single document that is later merged with the others.
65-
*
66-
* @throws \Exception
67-
*/
68-
protected function getHtmlContentsForDocument(HtmlDocumentInterface $documentModel): string
69-
{
70-
$htmlContents = $documentModel->getContents();
71-
72-
$htmlContents = $this->replaceSpecialChars($htmlContents);
73-
$this->writeDebugFile($htmlContents);
74-
75-
return $htmlContents;
76-
}
77-
7868
/**
7969
* @see \Dompdf\Options
8070
*

src/Model/GeneratorInterface.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Model/HtmlDocumentInterface.php renamed to src/Model/HasBackgroundPdf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace Vianetz\Pdf\Model;
2121

22-
interface HtmlDocumentInterface extends DocumentInterface
22+
interface HasBackgroundPdf
2323
{
2424
public function getPdfBackgroundFile(): ?string;
2525

src/Model/HtmlDocument.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace Vianetz\Pdf\Model;
2121

22-
class HtmlDocument implements HtmlDocumentInterface
22+
class HtmlDocument implements HasBackgroundPdf, Htmlable
2323
{
2424
private string $htmlContents;
2525
private ?string $pdfBackgroundFile = null;
@@ -32,7 +32,7 @@ public function __construct(string $htmlContents, ?string $pdfBackgroundFile = n
3232
$this->pdfBackgroundFileForFirstPage = $pdfBackgroundFileForFirstPage;
3333
}
3434

35-
public function getContents(): string
35+
public function toHtml(): string
3636
{
3737
return $this->htmlContents;
3838
}

src/Model/PdfDocumentInterface.php renamed to src/Model/Htmlable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace Vianetz\Pdf\Model;
2121

22-
interface PdfDocumentInterface extends DocumentInterface
22+
interface Htmlable
2323
{
24+
public function toHtml(): string;
2425
}

src/Model/Merger/Fpdi.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
3-
* FPDI Merger class
4-
*
55
* This class is responsible for merging individual PDF documents and eventually adding the background PDF template file.
66
*
77
* @section LICENSE
@@ -22,35 +22,30 @@
2222
namespace Vianetz\Pdf\Model\Merger;
2323

2424
use setasign\Fpdi\PdfParser\StreamReader;
25-
use setasign\Fpdi\Tcpdf\Fpdi as FpdiModel;
25+
use setasign\Fpdi\Tcpdf\Fpdi as TcpdfFpdi;
2626
use Vianetz\Pdf\Model\Config;
2727

2828
final class Fpdi extends AbstractMerger
2929
{
30-
/** @var string */
3130
private const OUTPUT_MODE_STRING = 'S';
32-
33-
/** @var string */
3431
private const OUTPUT_FORMAT_LANDSCAPE = 'L';
35-
36-
/** @var string */
3732
private const OUTPUT_FORMAT_PORTRAIT = 'P';
3833

39-
private FpdiModel $fpdiModel;
34+
private TcpdfFpdi $fpdiModel;
4035
private string $orientation = self::OUTPUT_FORMAT_PORTRAIT;
4136
private string $paper = 'a4';
4237

43-
public function __construct(?\Vianetz\Pdf\Model\Config $config = null)
38+
public function __construct(?Config $config = null)
4439
{
45-
$config ??= new \Vianetz\Pdf\Model\Config();
40+
$config ??= new Config();
4641

4742
if ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_PORTRAIT) {
4843
$this->orientation = self::OUTPUT_FORMAT_PORTRAIT;
4944
} elseif ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_LANDSCAPE) {
5045
$this->orientation = self::OUTPUT_FORMAT_LANDSCAPE;
5146
}
5247

53-
$this->fpdiModel = new FpdiModel($this->orientation, 'mm', $this->paper);
48+
$this->fpdiModel = new TcpdfFpdi($this->orientation, 'mm', $this->paper);
5449

5550
$this->fpdiModel->SetAuthor($config->getPdfAuthor());
5651
$this->fpdiModel->SetTitle($config->getPdfTitle());
@@ -81,7 +76,7 @@ public function importPageFromPdfString(string $pdfString, int $pageNumber): voi
8176
$this->importPageFromFile($this->createPdfStream($pdfString), $pageNumber);
8277
}
8378

84-
public function getPdfContents(): string
79+
public function toPdf(): string
8580
{
8681
return $this->fpdiModel->Output('', self::OUTPUT_MODE_STRING);
8782
}

src/Model/MergerInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121

2222
use setasign\Fpdi\PdfParser\StreamReader;
2323

24-
interface MergerInterface
24+
interface MergerInterface extends Pdfable
2525
{
26-
public function getPdfContents(): string;
27-
2826
/** @param string|resource|StreamReader $file */
2927
public function importPageFromFile($file, int $pageNumber): void;
3028

src/Model/Pdf.php

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Usage:
99
* 1) Instantiate (optionally with your custom generator class)
1010
* 2) addDocument($document)
11-
* 3) getContents() or saveToFile()
11+
* 3) toPdf() or saveToFile()
1212
*
1313
* @section LICENSE
1414
* This file is created by vianetz <[email protected]>.
@@ -27,18 +27,20 @@
2727

2828
namespace Vianetz\Pdf\Model;
2929

30+
use Vianetz\Pdf\Exception;
31+
use Vianetz\Pdf\Model\Generator\AbstractGenerator;
3032
use Vianetz\Pdf\NoDataException;
3133

32-
class Pdf implements PdfInterface
34+
class Pdf implements CanSave, Pdfable
3335
{
34-
private GeneratorInterface $generator;
36+
private AbstractGenerator $generator;
3537
private PdfMerge $pdfMerge;
36-
private ?string $contents = null;
38+
private ?string $pdfContents = null;
3739

3840
/**
3941
* Initialize empty array for PDF documents to print.
4042
*
41-
* @var array<\Vianetz\Pdf\Model\HtmlDocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface>
43+
* @var list<\Vianetz\Pdf\Model\Pdfable|\Vianetz\Pdf\Model\Htmlable>
4244
*/
4345
private array $documents = [];
4446

@@ -53,7 +55,7 @@ class Pdf implements PdfInterface
5355
final public function __construct(
5456
Config $config,
5557
EventManagerInterface $eventManager,
56-
GeneratorInterface $generator,
58+
AbstractGenerator $generator,
5759
MergerInterface $merger
5860
) {
5961
$this->generator = $generator;
@@ -63,36 +65,35 @@ final public function __construct(
6365
}
6466

6567
/** {@inheritDoc} */
66-
final public function getContents(): string
68+
final public function toPdf(): string
6769
{
68-
if ($this->contents === null) {
70+
if ($this->pdfContents === null) {
6971
$this->renderPdfContentsForAllDocuments();
70-
$this->contents = $this->pdfMerge->getContents();
72+
$this->pdfContents = $this->pdfMerge->toPdf();
7173
}
7274

73-
return $this->contents;
75+
$this->eventManager->dispatch('vianetz_pdf_get_contents', ['contents' => $this->pdfContents]);
76+
77+
return $this->pdfContents;
7478
}
7579

7680
/** {@inheritDoc} */
7781
final public function saveToFile(string $fileName): bool
7882
{
79-
$pdfContents = $this->getContents();
83+
$pdfContents = $this->toPdf();
8084

8185
return @file_put_contents($fileName, $pdfContents) !== false;
8286
}
8387

8488
/**
85-
* Add a new document to generate.
86-
*
87-
* @param \Vianetz\Pdf\Model\HtmlDocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface $documentModel
88-
*
8989
* @api
90+
* @param \Vianetz\Pdf\Model\Pdfable|\Vianetz\Pdf\Model\Htmlable $documentModel
9091
*/
9192
final public function addDocument($documentModel): self
9293
{
9394
$this->documents[] = $documentModel;
9495
// Reset cached pdf contents.
95-
$this->contents = null;
96+
$this->pdfContents = null;
9697

9798
return $this;
9899
}
@@ -114,7 +115,7 @@ final public function countDocuments(): int
114115
*/
115116
final public function render(): string
116117
{
117-
return $this->getContents();
118+
return $this->toPdf();
118119
}
119120

120121
public function getConfig(): Config
@@ -125,7 +126,7 @@ public function getConfig(): Config
125126
/**
126127
* Return merged pdf contents of all documents and save it to single temporary files.
127128
*
128-
* @throws \Vianetz\Pdf\NoDataException
129+
* @throws \Vianetz\Pdf\NoDataException|\Vianetz\Pdf\Exception
129130
*/
130131
private function renderPdfContentsForAllDocuments(): void
131132
{
@@ -136,19 +137,25 @@ private function renderPdfContentsForAllDocuments(): void
136137
'merger' => $this->pdfMerge,
137138
]);
138139

139-
if ($documentInstance instanceof HtmlDocumentInterface) {
140-
$pdfContents = $this->generator->renderPdfDocument($documentInstance);
140+
if ($documentInstance instanceof Htmlable) {
141+
$pdfContents = $this->generator->convert($documentInstance->toHtml())->toPdf();
141142
if (empty($pdfContents)) {
142143
continue;
143144
}
145+
} elseif ($documentInstance instanceof Pdfable) {
146+
$pdfContents = $documentInstance->toPdf();
147+
} else {
148+
throw new Exception('invalid document type');
149+
}
144150

145-
$hasData = true;
146-
151+
if ($documentInstance instanceof HasBackgroundPdf) {
147152
$this->pdfMerge->mergePdfString($pdfContents, $documentInstance->getPdfBackgroundFile(), $documentInstance->getPdfBackgroundFileForFirstPage());
148-
} elseif ($documentInstance instanceof PdfDocumentInterface) {
149-
$this->pdfMerge->mergePdfFile($documentInstance->getContents());
153+
} else {
154+
$this->pdfMerge->mergePdfString($pdfContents);
150155
}
151156

157+
$hasData = true;
158+
152159
$this->eventManager->dispatch('vianetz_pdf_document_render_after', [
153160
'document' => $documentInstance,
154161
'merger' => $this->pdfMerge,

0 commit comments

Comments
 (0)