Passed
Pull Request — master (#23)
by Stephen
02:22 queued 10s
created

PdfExporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 4
c 3
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 2
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\Aws\S3\S3;
10
use Sfneal\Helpers\Strings\StringHelpers;
11
12
class PdfExporter
13
{
14
    /**
15
     * @var Options
16
     */
17
    private $options;
18
19
    /**
20
     * @var Dompdf
21
     */
22
    private $pdf;
23
24
    /**
25
     * @var string|null AWS S3 file path
26
     */
27
    private $path;
28
29
    /**
30
     * @var string|null AWS S3 file URL
31
     */
32
    private $url;
33
34
    /**
35
     * @var string|null
36
     */
37
    private $output;
38
39
    /**
40
     * PdfExporter constructor.
41
     * @param View|string $content
42
     * @param Options|null $options
43
     * @throws Exception
44
     */
45
    public function __construct($content, Options $options = null)
46
    {
47
        // Declare PDF options (use DefaultOptions) if none provided
48
        $this->options = $options ?? new DefaultOptions();
49
50
        // Instantiate dompdf
51
        $this->pdf = new Dompdf($this->options);
52
53
        // Load content
54
        $this->loadContent($content);
55
56
        // Render the PDF
57
        $this->render();
58
    }
59
60
    /**
61
     * Upload a rendered PDF to an AWS S3 file store.
62
     *
63
     * @param string $path
64
     * @return $this
65
     */
66
    public function upload(string $path): self
67
    {
68
        $this->path = $path;
69
        $this->url = (new S3($path))->upload_raw($this->getOutput());
70
71
        return $this;
72
    }
73
74
    /**
75
     * View the PDF in the clients browser.
76
     *
77
     * @param string $filename
78
     */
79
    public function view(string $filename = 'output.pdf')
80
    {
81
        $this->pdf->stream($filename, ['Attachment' => false]);
82
    }
83
84
    /**
85
     * Download the PDF using the clients browser.
86
     *
87
     * @param string $filename
88
     */
89
    public function download(string $filename = 'output.pdf')
90
    {
91
        $this->pdf->stream($filename, ['Attachment' => true]);
92
    }
93
94
    /**
95
     * Retrieve the PDF's output.
96
     *
97
     * @return string
98
     */
99
    public function getOutput(): string
100
    {
101
        return $this->output;
102
    }
103
104
    /**
105
     * Retrieve the PDF's AWS S3 path.
106
     *
107
     * @return string|null
108
     */
109
    public function getPath(): ?string
110
    {
111
        return $this->path;
112
    }
113
114
    /**
115
     * Retrieve the PDF's AWS S3 url.
116
     *
117
     * @return string|null
118
     */
119
    public function getUrl(): ?string
120
    {
121
        return $this->url;
122
    }
123
124
    /**
125
     * Load PDF content to the Dompdf instance.
126
     *
127
     *  - $content can be a View or HTML file contents
128
     *
129
     * @param View|string $content
130
     * @return $this
131
     * @throws Exception
132
     */
133
    private function loadContent($content): self
134
    {
135
        // Create local HTML file path
136
        $localHTML = StringHelpers::joinPaths($this->options->getRootDir(), uniqid().'.html');
137
138
        // Store View (or HTML) as HTML file within Dompdf root
139
        touch($localHTML);
140
        file_put_contents($localHTML, $content);
141
142
        // Load HTML
143
        $this->pdf->loadHtmlFile($localHTML);
144
145
        // Remove temp HTML file
146
        unlink($localHTML);
147
148
        return $this;
149
    }
150
151
    /**
152
     * Render a Dompdf & store it's output in a property.
153
     *
154
     *  - storing output in a property avoids potentially calling expensive 'output()' method multiple times
155
     *
156
     * @return void
157
     */
158
    private function render(): void
159
    {
160
        $this->pdf->render();
161
        $this->output = $this->pdf->output();
162
    }
163
}
164