1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\ViewExport\Support; |
4
|
|
|
|
5
|
|
|
use Dompdf\Exception; |
6
|
|
|
use Illuminate\Support\Facades\Bus; |
7
|
|
|
use Sfneal\Queueables\AbstractJob; |
8
|
|
|
|
9
|
|
|
abstract class Renderer extends AbstractJob |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string PDF content (either a rendered View or HTML string) |
13
|
|
|
*/ |
14
|
|
|
protected $content; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string|null AWS S3 path to upload PDF to after render (if provided) |
18
|
|
|
*/ |
19
|
|
|
protected $uploadPath; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Renderer constructor. |
23
|
|
|
* |
24
|
|
|
* - $content can be a View or HTML file contents |
25
|
|
|
* |
26
|
|
|
* @param string $content |
27
|
|
|
* @param string|null $uploadPath |
28
|
|
|
*/ |
29
|
|
|
public function __construct(string $content, string $uploadPath = null) |
30
|
|
|
{ |
31
|
|
|
// Content of the PDF |
32
|
|
|
$this->content = $content; |
33
|
|
|
|
34
|
|
|
// Upload PDF after rendering (defaults to false) |
35
|
|
|
$this->uploadPath = $uploadPath; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Render the content to a exportable object. |
40
|
|
|
* |
41
|
|
|
* @throws Exception |
42
|
|
|
* @return object |
43
|
|
|
*/ |
44
|
|
|
abstract protected function render(): object; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retrieve in an Exporter instance created from the $exportable. |
48
|
|
|
* |
49
|
|
|
* @param $exportable |
50
|
|
|
* @return Exporter |
51
|
|
|
*/ |
52
|
|
|
abstract protected function exporter($exportable): Exporter; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Dispatch this renderer instance to the Job queue without having to construct it statically. |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function handleJob() |
60
|
|
|
{ |
61
|
|
|
return Bus::dispatch($this); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Load renderable content to an Exporter instance and render the output. |
66
|
|
|
* |
67
|
|
|
* - storing output in a property avoids potentially calling expensive 'output()' method multiple times |
68
|
|
|
* |
69
|
|
|
* @return Exporter |
70
|
|
|
* @throws Exception |
71
|
|
|
*/ |
72
|
|
|
public function handle(): Exporter |
73
|
|
|
{ |
74
|
|
|
// Render the PDF |
75
|
|
|
$exportable = $this->render(); |
76
|
|
|
|
77
|
|
|
// Instantiate the Exporter |
78
|
|
|
return $this->export($exportable); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create & return an Exporter instance. |
83
|
|
|
* |
84
|
|
|
* @param $exportable |
85
|
|
|
* @return Exporter |
86
|
|
|
*/ |
87
|
|
|
private function export($exportable): Exporter |
88
|
|
|
{ |
89
|
|
|
// Initialize the PdfExporter |
90
|
|
|
$exporter = $this->exporter($exportable); |
91
|
|
|
|
92
|
|
|
// Upload after rendering if an upload path was provided |
93
|
|
|
if ($this->uploadPath) { |
94
|
|
|
$exporter->upload($this->uploadPath); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Return a PdfExporter |
98
|
|
|
return $exporter; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|