|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\DocumentGenerator\Helpers; |
|
6
|
|
|
use ByTIC\DocumentGenerator\PdfLetters\Models\Downloads\DownloadsTrait; |
|
7
|
|
|
use ByTIC\DocumentGenerator\PdfLetters\Models\Fields\FieldTrait; |
|
8
|
|
|
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded\FileUnacceptableForCollection; |
|
9
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait; |
|
10
|
|
|
use ByTIC\MediaLibrary\HasMedia\Interfaces\HasMedia; |
|
11
|
|
|
use ByTIC\MediaLibrary\Media\Media; |
|
12
|
|
|
use ByTIC\MediaLibrary\MediaRepository\MediaRepository; |
|
13
|
|
|
use DateTime; |
|
14
|
|
|
use Nip\Records\Record; |
|
15
|
|
|
use Nip\Records\Traits\AbstractTrait\RecordTrait as AbstractRecordTrait; |
|
16
|
|
|
use setasign\Fpdi; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
18
|
|
|
use TCPDF; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class PdfLetterTrait |
|
22
|
|
|
* @package ByTIC\DocumentGenerator\PdfLetters |
|
23
|
|
|
* |
|
24
|
|
|
* @method FieldTrait[] getCustomFields() |
|
25
|
|
|
* |
|
26
|
|
|
* @property int $id_item |
|
27
|
|
|
* @property string $type |
|
28
|
|
|
* @property string $orientation |
|
29
|
|
|
* @property string $format |
|
30
|
|
|
*/ |
|
31
|
|
|
trait PdfLetterTrait |
|
32
|
|
|
{ |
|
33
|
|
|
use AbstractRecordTrait; |
|
34
|
|
|
use HasMediaTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getName() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->getManager()->getLabel('title.singular') . ' #' . $this->id; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
public function hasFile() |
|
48
|
|
|
{ |
|
49
|
|
|
$file = $this->getFile(); |
|
50
|
|
|
|
|
51
|
|
|
return $file instanceof Media; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return bool|Media |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function getFile() |
|
58
|
|
|
{ |
|
59
|
2 |
|
$files = $this->getFiles(); |
|
60
|
2 |
|
if (count($files) < 1) { |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
return $files->getDefaultMedia(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
public function delete() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->deleteLetters(); |
|
73
|
|
|
|
|
74
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
75
|
|
|
return parent::delete(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function deleteLetters() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->getFiles()->delete(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function downloadExample() |
|
84
|
|
|
{ |
|
85
|
|
|
$result = $this->getModelExample(); |
|
86
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
|
87
|
|
|
$result->demo = true; |
|
88
|
|
|
|
|
89
|
|
|
$this->download($result); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param AbstractRecordTrait $model |
|
94
|
|
|
*/ |
|
95
|
|
|
public function download($model) |
|
96
|
|
|
{ |
|
97
|
|
|
$pdf = $this->generatePdfObj($model); |
|
98
|
|
|
|
|
99
|
|
|
$pdf->Output($this->getFileNameFromModel($model) . '.pdf', 'D'); |
|
100
|
|
|
die(); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function downloadBlank() |
|
104
|
|
|
{ |
|
105
|
|
|
$file = $this->getFile(); |
|
106
|
|
|
$model = $this->getModelExample(); |
|
107
|
|
|
$name = $this->getFileNameFromModel($model) . '.pdf'; |
|
108
|
|
|
|
|
109
|
|
|
header('Content-Type: application/pdf'); |
|
110
|
|
|
header('Content-Description: File Transfer'); |
|
111
|
|
|
header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1'); |
|
112
|
|
|
header('Pragma: public'); |
|
113
|
|
|
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past |
|
114
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
|
115
|
|
|
header('Content-Disposition: attachment; filename="' . $name . '";'); |
|
116
|
|
|
header("Content-Transfer-Encoding: Binary"); |
|
117
|
|
|
echo $file->read(); |
|
118
|
|
|
die(); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param Record $recipient |
|
123
|
|
|
*/ |
|
124
|
|
|
public function addDownload($recipient) |
|
125
|
|
|
{ |
|
126
|
|
|
/** @var DownloadsTrait $downloadsManager */ |
|
127
|
|
|
$downloadsManager = $this->getManager()->getRelation('Downloads')->getWith(); |
|
128
|
|
|
$downloadsManager->createForRecipient($recipient, $this); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param $model |
|
133
|
|
|
* @param $output |
|
134
|
|
|
* @return bool|Fpdi|TCPDF |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function generateFile($model, $output = null) |
|
137
|
|
|
{ |
|
138
|
1 |
|
$pdf = $this->generatePdfObj($model); |
|
139
|
|
|
|
|
140
|
1 |
|
if ($model->demo === true) { |
|
141
|
|
|
$this->pdfDrawGuidelines($pdf); |
|
142
|
|
|
} |
|
143
|
1 |
|
$fileName = $this->getFileNameFromModel($model) . '.pdf'; |
|
144
|
1 |
|
if (is_string($output) && is_dir($output)) { |
|
145
|
|
|
return $pdf->Output($output . $fileName, 'F'); |
|
146
|
|
|
} |
|
147
|
1 |
|
if ($output instanceof HasMedia) { |
|
148
|
|
|
/** @var HasMediaTrait $output */ |
|
149
|
1 |
|
$output->addFileFromContent( |
|
150
|
1 |
|
$pdf->Output($fileName, 'S'), |
|
151
|
1 |
|
$fileName |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
1 |
|
return $pdf; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param AbstractRecordTrait $model |
|
159
|
|
|
* @return FPDI|TCPDF |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function generatePdfObj($model) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$pdf = $this->generateNewPdfObj(); |
|
164
|
|
|
|
|
165
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
|
166
|
1 |
|
if ($model->demo === true) { |
|
167
|
|
|
$this->pdfDrawGuidelines($pdf); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
$this->addFieldsToPDF($pdf, $model); |
|
171
|
|
|
|
|
172
|
1 |
|
return $pdf; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return Fpdi\Tcpdf\Fpdi|TCPDF |
|
177
|
|
|
* @throws Fpdi\PdfParser\CrossReference\CrossReferenceException |
|
178
|
|
|
* @throws Fpdi\PdfParser\Filter\FilterException |
|
179
|
|
|
* @throws Fpdi\PdfParser\PdfParserException |
|
180
|
|
|
* @throws Fpdi\PdfParser\Type\PdfTypeException |
|
181
|
|
|
* @throws Fpdi\PdfReader\PdfReaderException |
|
182
|
|
|
*/ |
|
183
|
2 |
|
public function generateNewPdfObj() |
|
184
|
|
|
{ |
|
185
|
2 |
|
$pdf = static::newPdfBuilder('L'); |
|
186
|
|
|
|
|
187
|
2 |
|
$mediaFile = $this->getFile(); |
|
188
|
2 |
|
$pageCount = $pdf->setSourceFile($mediaFile->getFile()->readStream()); |
|
189
|
2 |
|
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) { |
|
190
|
2 |
|
$tplidx = $pdf->importPage($pageNo, '/MediaBox'); |
|
191
|
|
|
|
|
192
|
2 |
|
$pdf->AddPage(ucfirst($this->orientation), $this->format); |
|
193
|
2 |
|
$pdf->useTemplate($tplidx); |
|
194
|
2 |
|
$pdf->endPage(); |
|
195
|
|
|
} |
|
196
|
2 |
|
$pdf->setPage(1); |
|
197
|
|
|
|
|
198
|
2 |
|
return $pdf; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param mixed ...$params |
|
203
|
|
|
* @return Fpdi\Tcpdf\Fpdi |
|
204
|
|
|
*/ |
|
205
|
3 |
|
public static function newPdfBuilder(...$params) |
|
206
|
|
|
{ |
|
207
|
3 |
|
$pdf = new Fpdi\Tcpdf\Fpdi(...$params); |
|
208
|
3 |
|
$pdf->setPrintHeader(false); |
|
209
|
3 |
|
$pdf->SetCreator(PDF_CREATOR); |
|
210
|
3 |
|
$pdf->SetAuthor(Helpers::author()); |
|
211
|
|
|
|
|
212
|
3 |
|
return $pdf; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return AbstractRecordTrait |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getItem() |
|
219
|
|
|
{ |
|
220
|
|
|
$manager = $this->getItemsManager(); |
|
221
|
|
|
|
|
222
|
|
|
return $manager->findOne($this->id_item); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param UploadedFile $uploadedFile |
|
227
|
|
|
* @return string|boolean |
|
228
|
|
|
*/ |
|
229
|
2 |
|
public function uploadFromRequest($uploadedFile) |
|
230
|
|
|
{ |
|
231
|
2 |
|
$fileCollection = $this->getFiles(); |
|
232
|
|
|
|
|
233
|
2 |
|
if (!$uploadedFile->isValid()) { |
|
234
|
|
|
return $uploadedFile->getErrorMessage(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
try { |
|
238
|
2 |
|
$fileAdder = $this->addFile($uploadedFile); |
|
239
|
1 |
|
$newMedia = $fileAdder->getMedia(); |
|
240
|
1 |
|
} catch (FileUnacceptableForCollection $exception) { |
|
241
|
1 |
|
return $exception->violations->getMessageString(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
1 |
|
foreach ($fileCollection as $name => $media) { |
|
245
|
|
|
if ($name != $newMedia->getName()) { |
|
246
|
|
|
$media->delete(); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
1 |
|
return $newMedia; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return AbstractRecordTrait |
|
254
|
|
|
*/ |
|
255
|
|
|
abstract public function getItemsManager(); |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @return AbstractRecordTrait |
|
259
|
|
|
*/ |
|
260
|
|
|
abstract public function getModelExample(); |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @return DateTime |
|
264
|
|
|
*/ |
|
265
|
|
|
abstract public function getIssueDate(): DateTime; |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @param $pdf |
|
269
|
|
|
* @param $model |
|
270
|
|
|
*/ |
|
271
|
1 |
|
protected function addFieldsToPDF($pdf, $model) |
|
272
|
|
|
{ |
|
273
|
|
|
/** @var FieldTrait[] $fields */ |
|
274
|
1 |
|
$fields = $this->getCustomFields(); |
|
275
|
1 |
|
foreach ($fields as $field) { |
|
276
|
|
|
$field->addToPdf($pdf, $model); |
|
277
|
|
|
} |
|
278
|
1 |
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @return string |
|
282
|
|
|
*/ |
|
283
|
1 |
|
protected function getFileNameDefault() |
|
284
|
|
|
{ |
|
285
|
1 |
|
return 'letter'; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param FPDI|TCPDF $pdf |
|
290
|
|
|
*/ |
|
291
|
|
|
protected function pdfDrawGuidelines($pdf) |
|
292
|
|
|
{ |
|
293
|
|
|
for ($pos = 5; $pos < 791; $pos = $pos + 5) { |
|
294
|
|
|
if (($pos % 100) == 0) { |
|
295
|
|
|
$pdf->SetDrawColor(0, 0, 200); |
|
296
|
|
|
$pdf->SetLineWidth(.7); |
|
297
|
|
|
} elseif (($pos % 50) == 0) { |
|
298
|
|
|
$pdf->SetDrawColor(200, 0, 0); |
|
299
|
|
|
$pdf->SetLineWidth(.4); |
|
300
|
|
|
} else { |
|
301
|
|
|
$pdf->SetDrawColor(128, 128, 128); |
|
302
|
|
|
$pdf->SetLineWidth(.05); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
$pdf->Line(0, $pos, 611, $pos); |
|
306
|
|
|
if ($pos < 611) { |
|
307
|
|
|
$pdf->Line($pos, 0, $pos, 791); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** @noinspection PhpUnusedParameterInspection |
|
313
|
|
|
* @param $model |
|
314
|
|
|
* @return string |
|
315
|
|
|
*/ |
|
316
|
1 |
|
protected function getFileNameFromModel($model) |
|
317
|
|
|
{ |
|
318
|
1 |
|
return $this->getFileNameDefault(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* @param MediaRepository $mediaRepository |
|
323
|
|
|
* @return MediaRepository |
|
324
|
|
|
*/ |
|
325
|
4 |
|
protected function hydrateMediaRepositoryCustom($mediaRepository) |
|
326
|
|
|
{ |
|
327
|
4 |
|
$filesCollection = $mediaRepository->getCollection('files'); |
|
328
|
4 |
|
$filesCollection->getConstraint()->mimeTypes = ['application/pdf']; |
|
329
|
4 |
|
return $mediaRepository; |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.