8
8
* Usage:
9
9
* 1) Instantiate (optionally with your custom generator class)
10
10
* 2) addDocument($document)
11
- * 3) getContents () or saveToFile()
11
+ * 3) toPdf () or saveToFile()
12
12
*
13
13
* @section LICENSE
14
14
* This file is created by vianetz <[email protected] >.
27
27
28
28
namespace Vianetz \Pdf \Model ;
29
29
30
+ use Vianetz \Pdf \Exception ;
31
+ use Vianetz \Pdf \Model \Generator \AbstractGenerator ;
30
32
use Vianetz \Pdf \NoDataException ;
31
33
32
- class Pdf implements PdfInterface
34
+ class Pdf implements CanSave, Pdfable
33
35
{
34
- private GeneratorInterface $ generator ;
36
+ private AbstractGenerator $ generator ;
35
37
private PdfMerge $ pdfMerge ;
36
- private ?string $ contents = null ;
38
+ private ?string $ pdfContents = null ;
37
39
38
40
/**
39
41
* Initialize empty array for PDF documents to print.
40
42
*
41
- * @var array <\Vianetz\Pdf\Model\HtmlDocumentInterface |\Vianetz\Pdf\Model\PdfDocumentInterface >
43
+ * @var list <\Vianetz\Pdf\Model\Pdfable |\Vianetz\Pdf\Model\Htmlable >
42
44
*/
43
45
private array $ documents = [];
44
46
@@ -53,7 +55,7 @@ class Pdf implements PdfInterface
53
55
final public function __construct (
54
56
Config $ config ,
55
57
EventManagerInterface $ eventManager ,
56
- GeneratorInterface $ generator ,
58
+ AbstractGenerator $ generator ,
57
59
MergerInterface $ merger
58
60
) {
59
61
$ this ->generator = $ generator ;
@@ -63,36 +65,35 @@ final public function __construct(
63
65
}
64
66
65
67
/** {@inheritDoc} */
66
- final public function getContents (): string
68
+ final public function toPdf (): string
67
69
{
68
- if ($ this ->contents === null ) {
70
+ if ($ this ->pdfContents === null ) {
69
71
$ this ->renderPdfContentsForAllDocuments ();
70
- $ this ->contents = $ this ->pdfMerge ->getContents ();
72
+ $ this ->pdfContents = $ this ->pdfMerge ->toPdf ();
71
73
}
72
74
73
- return $ this ->contents ;
75
+ $ this ->eventManager ->dispatch ('vianetz_pdf_get_contents ' , ['contents ' => $ this ->pdfContents ]);
76
+
77
+ return $ this ->pdfContents ;
74
78
}
75
79
76
80
/** {@inheritDoc} */
77
81
final public function saveToFile (string $ fileName ): bool
78
82
{
79
- $ pdfContents = $ this ->getContents ();
83
+ $ pdfContents = $ this ->toPdf ();
80
84
81
85
return @file_put_contents ($ fileName , $ pdfContents ) !== false ;
82
86
}
83
87
84
88
/**
85
- * Add a new document to generate.
86
- *
87
- * @param \Vianetz\Pdf\Model\HtmlDocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface $documentModel
88
- *
89
89
* @api
90
+ * @param \Vianetz\Pdf\Model\Pdfable|\Vianetz\Pdf\Model\Htmlable $documentModel
90
91
*/
91
92
final public function addDocument ($ documentModel ): self
92
93
{
93
94
$ this ->documents [] = $ documentModel ;
94
95
// Reset cached pdf contents.
95
- $ this ->contents = null ;
96
+ $ this ->pdfContents = null ;
96
97
97
98
return $ this ;
98
99
}
@@ -114,7 +115,7 @@ final public function countDocuments(): int
114
115
*/
115
116
final public function render (): string
116
117
{
117
- return $ this ->getContents ();
118
+ return $ this ->toPdf ();
118
119
}
119
120
120
121
public function getConfig (): Config
@@ -125,7 +126,7 @@ public function getConfig(): Config
125
126
/**
126
127
* Return merged pdf contents of all documents and save it to single temporary files.
127
128
*
128
- * @throws \Vianetz\Pdf\NoDataException
129
+ * @throws \Vianetz\Pdf\NoDataException|\Vianetz\Pdf\Exception
129
130
*/
130
131
private function renderPdfContentsForAllDocuments (): void
131
132
{
@@ -136,19 +137,25 @@ private function renderPdfContentsForAllDocuments(): void
136
137
'merger ' => $ this ->pdfMerge ,
137
138
]);
138
139
139
- if ($ documentInstance instanceof HtmlDocumentInterface ) {
140
- $ pdfContents = $ this ->generator ->renderPdfDocument ($ documentInstance );
140
+ if ($ documentInstance instanceof Htmlable ) {
141
+ $ pdfContents = $ this ->generator ->convert ($ documentInstance-> toHtml ())-> toPdf ( );
141
142
if (empty ($ pdfContents )) {
142
143
continue ;
143
144
}
145
+ } elseif ($ documentInstance instanceof Pdfable) {
146
+ $ pdfContents = $ documentInstance ->toPdf ();
147
+ } else {
148
+ throw new Exception ('invalid document type ' );
149
+ }
144
150
145
- $ hasData = true ;
146
-
151
+ if ($ documentInstance instanceof HasBackgroundPdf) {
147
152
$ 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 );
150
155
}
151
156
157
+ $ hasData = true ;
158
+
152
159
$ this ->eventManager ->dispatch ('vianetz_pdf_document_render_after ' , [
153
160
'document ' => $ documentInstance ,
154
161
'merger ' => $ this ->pdfMerge ,
0 commit comments