1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LPDFBuilder\Generation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Knp\Snappy\Exception\FileAlreadyExistsException; |
9
|
|
|
use setasign\Fpdi\Fpdi; |
10
|
|
|
|
11
|
|
|
abstract class AbstractDocumentFromImage implements DocumentGenerator |
12
|
|
|
{ |
13
|
|
|
protected string $fileExtension = 'pdf'; |
14
|
|
|
|
15
|
|
|
public function extension(string|Extension $fileExtension = 'pdf'): static |
16
|
|
|
{ |
17
|
|
|
if ($fileExtension instanceof Extension) { |
18
|
|
|
$fileExtension = $fileExtension->value; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if ($fileExtension !== Extension::PDF->value) { |
22
|
|
|
throw new \Exception('Supports only pdf extension'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
10 |
|
public function generateName(?string $defaultName = null): string |
29
|
|
|
{ |
30
|
10 |
|
if ($defaultName) { |
31
|
10 |
|
if (Str::endsWith($defaultName, ".{$this->fileExtension}")) { |
32
|
|
|
$defaultName = Str::beforeLast($defaultName, ".{$this->fileExtension}"); |
33
|
|
|
} |
34
|
|
|
|
35
|
10 |
|
return $defaultName.'.'.$this->fileExtension; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return Str::random().'.'.$this->fileExtension; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function asView(): ?\Illuminate\Contracts\View\View |
42
|
|
|
{ |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
public function temporalFile(?string $filename = null): ?string |
47
|
|
|
{ |
48
|
2 |
|
$filePath = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) |
49
|
2 |
|
.DIRECTORY_SEPARATOR |
50
|
2 |
|
.ltrim($this->generateName($filename), DIRECTORY_SEPARATOR); |
51
|
2 |
|
$this->generate()->Output('F', $filePath); |
52
|
|
|
|
53
|
2 |
|
return $filePath; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function inline(?string $filename = null): Response |
57
|
|
|
{ |
58
|
2 |
|
return new Response($this->generate()->Output('S'), 200, [ |
59
|
2 |
|
'Content-Type' => 'application/pdf', |
60
|
2 |
|
'Content-Disposition' => 'inline; filename="'.Str::afterLast($this->generateName($filename), DIRECTORY_SEPARATOR).'"', |
61
|
2 |
|
'Cache-Control' => 'private, max-age=0, must-revalidate', |
62
|
2 |
|
'Pragma' => 'public', |
63
|
2 |
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
public function download(?string $filename = null): Response |
67
|
|
|
{ |
68
|
2 |
|
return new Response($this->generate()->Output('S'), 200, [ |
69
|
2 |
|
'Content-Type' => 'application/pdf', |
70
|
2 |
|
'Content-Disposition' => 'attachment; filename="'.Str::afterLast($this->generateName($filename), DIRECTORY_SEPARATOR).'"', |
71
|
2 |
|
'Cache-Control' => 'private, max-age=0, must-revalidate', |
72
|
2 |
|
'Pragma' => 'public', |
73
|
2 |
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
public function save(?string $disc = null, ?string $filename = null, bool $overwrite = false, array $options = []): static |
77
|
|
|
{ |
78
|
4 |
|
$filename = $this->generateName($filename); |
79
|
4 |
|
$storage = Storage::disk($disc); |
80
|
4 |
|
if (!$overwrite && $storage->exists($filename)) { |
81
|
|
|
// tangled with snappy, maybe good to create internal own exception? |
82
|
|
|
throw new FileAlreadyExistsException(\sprintf('The output file \'%s\' already exists.', $storage->path($filename))); |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
$storage->put($filename, $this->generate()->Output('S')); |
86
|
|
|
|
87
|
2 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
abstract protected function generate(): Fpdi; |
91
|
|
|
} |
92
|
|
|
|