Passed
Push — master ( 9eb67a...b09c8e )
by Stephen
52s queued 12s
created

Renderer::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 24
rs 10
c 0
b 0
f 0
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\Contracts\View\View;
9
use Sfneal\Helpers\Strings\StringHelpers;
10
use Sfneal\Queueables\AbstractJob;
11
12
class Renderer extends AbstractJob
13
{
14
    /**
15
     * @var View|string PDF content (either a View or HTML string)
16
     */
17
    private $content;
18
19
    /**
20
     * @var string|null AWS S3 path to upload PDF to after render (if provided)
21
     */
22
    private $uploadPath;
23
24
    /**
25
     * @var Options
26
     */
27
    public $options;
28
29
    /**
30
     * @var Metadata
31
     */
32
    public $metadata;
33
34
    /**
35
     * @var Dompdf
36
     */
37
    private $pdf;
38
39
    /**
40
     * PdfExporter constructor.
41
     *
42
     * - $content can be a View or HTML file contents
43
     *
44
     * @param View|string $content
45
     * @param string|null $uploadPath
46
     */
47
    public function __construct($content, string $uploadPath = null)
48
    {
49
        // Content of the PDF
50
        $this->content = $content;
51
52
        // Upload PDF after rendering (defaults to false)
53
        $this->uploadPath = $uploadPath;
54
55
        // Declare PDF options (use DefaultOptions) if none provided
56
        $this->options = new DefaultOptions();
57
58
        // Instantiate Metadata
59
        $this->metadata = new Metadata();
60
    }
61
62
    /**
63
     * Load PDF content to the Dompdf instance and render the output.
64
     *
65
     *  - storing output in a property avoids potentially calling expensive 'output()' method multiple times
66
     *
67
     * @return Exporter
68
     * @throws Exception
69
     */
70
    public function handle(): Exporter
71
    {
72
        // Instantiate dompdf
73
        $this->pdf = new Dompdf($this->options);
74
75
        // Add metadata
76
        $this->applyMetadata();
77
78
        // Load content
79
        $this->loadContent();
80
81
        // Render the PDF
82
        $this->pdf->render();
83
84
        // Initialize the PdfExporter
85
        $exporter = new Exporter($this->pdf);
86
87
        // Upload after rendering if an upload path was provided
88
        if ($this->uploadPath) {
89
            $exporter->upload($this->uploadPath);
90
        }
91
92
        // Return a PdfExporter
93
        return $exporter;
94
    }
95
96
    /**
97
     * Add Metadata to the PDF.
98
     *
99
     * @return bool
100
     */
101
    private function applyMetadata(): bool
102
    {
103
        // Add Metadata if the array isn't empty
104
        if ($hasMetadata = ! empty($this->metadata->get())) {
105
            foreach ($this->metadata->get() as $key => $value) {
106
                $this->pdf->add_info($key, $value);
107
            }
108
        }
109
110
        return $hasMetadata;
111
    }
112
113
    /**
114
     * Load content into the Dompdf instance.
115
     *
116
     * @throws Exception
117
     */
118
    private function loadContent(): void
119
    {
120
        // Create local HTML file path
121
        $localHTML = StringHelpers::joinPaths($this->options->getRootDir(), uniqid().'.html');
122
123
        // Store View (or HTML) as HTML file within Dompdf root
124
        touch($localHTML);
125
        file_put_contents($localHTML, $this->content);
126
127
        // Load HTML
128
        $this->pdf->loadHtmlFile($localHTML);
129
130
        // Remove temp HTML file
131
        unlink($localHTML);
132
    }
133
}
134