1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\ViewExport\Pdf\Utils; |
4
|
|
|
|
5
|
|
|
use Dompdf\Dompdf; |
6
|
|
|
use Dompdf\Exception; |
7
|
|
|
use Dompdf\Options; |
8
|
|
|
use Illuminate\Bus\Queueable as QueueableTrait; |
9
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
10
|
|
|
use Illuminate\Queue\SerializesModels; |
11
|
|
|
use Illuminate\Support\Facades\Bus; |
12
|
|
|
use Sfneal\Helpers\Strings\StringHelpers; |
13
|
|
|
|
14
|
|
|
class Renderer |
15
|
|
|
{ |
16
|
|
|
use InteractsWithQueue, QueueableTrait, SerializesModels; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string PDF content (either a rendered View or HTML string) |
20
|
|
|
*/ |
21
|
|
|
private $content; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string|null AWS S3 path to upload PDF to after render (if provided) |
25
|
|
|
*/ |
26
|
|
|
private $uploadPath; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Options |
30
|
|
|
*/ |
31
|
|
|
public $options; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Metadata |
35
|
|
|
*/ |
36
|
|
|
public $metadata; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Dompdf |
40
|
|
|
*/ |
41
|
|
|
private $pdf; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* PdfExporter constructor. |
45
|
|
|
* |
46
|
|
|
* - $content can be a View or HTML file contents |
47
|
|
|
* |
48
|
|
|
* @param string $content |
49
|
|
|
* @param string|null $uploadPath |
50
|
|
|
*/ |
51
|
|
|
public function __construct(string $content, string $uploadPath = null) |
52
|
|
|
{ |
53
|
|
|
// Content of the PDF |
54
|
|
|
$this->content = $content; |
55
|
|
|
|
56
|
|
|
// Upload PDF after rendering (defaults to false) |
57
|
|
|
$this->uploadPath = $uploadPath; |
58
|
|
|
|
59
|
|
|
// Declare PDF options (use DefaultOptions) if none provided |
60
|
|
|
$this->options = new DefaultOptions(); |
61
|
|
|
|
62
|
|
|
// Instantiate Metadata |
63
|
|
|
$this->metadata = new Metadata(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Dispatch this renderer instance to the Job queue without having to construct it statically. |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function dispatch() |
72
|
|
|
{ |
73
|
|
|
return Bus::dispatch($this); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Load PDF content to the Dompdf instance and render the output. |
78
|
|
|
* |
79
|
|
|
* - storing output in a property avoids potentially calling expensive 'output()' method multiple times |
80
|
|
|
* |
81
|
|
|
* @return Exporter |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
|
|
public function handle(): Exporter |
85
|
|
|
{ |
86
|
|
|
// Instantiate dompdf |
87
|
|
|
$this->pdf = new Dompdf($this->options); |
88
|
|
|
|
89
|
|
|
// Add metadata |
90
|
|
|
$this->applyMetadata(); |
91
|
|
|
|
92
|
|
|
// Load content |
93
|
|
|
$this->loadContent(); |
94
|
|
|
|
95
|
|
|
// Render the PDF |
96
|
|
|
$this->pdf->render(); |
97
|
|
|
|
98
|
|
|
// Initialize the PdfExporter |
99
|
|
|
$exporter = new Exporter($this->pdf); |
100
|
|
|
|
101
|
|
|
// Upload after rendering if an upload path was provided |
102
|
|
|
if ($this->uploadPath) { |
103
|
|
|
$exporter->upload($this->uploadPath); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Return a PdfExporter |
107
|
|
|
return $exporter; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Add Metadata to the PDF. |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
private function applyMetadata(): bool |
116
|
|
|
{ |
117
|
|
|
// Add Metadata if the array isn't empty |
118
|
|
|
if ($hasMetadata = ! empty($this->metadata->get())) { |
119
|
|
|
foreach ($this->metadata->get() as $key => $value) { |
120
|
|
|
$this->pdf->add_info($key, $value); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $hasMetadata; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Load content into the Dompdf instance. |
129
|
|
|
* |
130
|
|
|
* @throws Exception |
131
|
|
|
*/ |
132
|
|
|
private function loadContent(): void |
133
|
|
|
{ |
134
|
|
|
// Create local HTML file path |
135
|
|
|
$localHTML = StringHelpers::joinPaths($this->options->getRootDir(), uniqid().'.html'); |
136
|
|
|
|
137
|
|
|
// Store View (or HTML) as HTML file within Dompdf root |
138
|
|
|
touch($localHTML); |
139
|
|
|
file_put_contents($localHTML, $this->content); |
140
|
|
|
|
141
|
|
|
// Load HTML |
142
|
|
|
$this->pdf->loadHtmlFile($localHTML); |
143
|
|
|
|
144
|
|
|
// Remove temp HTML file |
145
|
|
|
unlink($localHTML); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|