Passed
Pull Request — master (#17)
by Stephen
11:17
created

PdfExporter::getOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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\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
48
        $this->options = $this->setOptions($options);
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
     * Retrieve the PDF's output.
76
     *
77
     * @return string
78
     */
79
    public function getOutput(): string
80
    {
81
        return $this->output;
82
    }
83
84
    /**
85
     * Retrieve the PDF's AWS S3 path.
86
     *
87
     * @return string|null
88
     */
89
    public function getPath(): ?string
90
    {
91
        return $this->path;
92
    }
93
94
    /**
95
     * Retrieve the PDF's AWS S3 url.
96
     *
97
     * @return string|null
98
     */
99
    public function getUrl(): ?string
100
    {
101
        return $this->url;
102
    }
103
104
    /**
105
     * Retrieve a Dompdf Options instance with custom values or defaults.
106
     *
107
     * @param Options|null $options
108
     * @return Options
109
     */
110
    private static function setOptions(Options $options = null): Options
111
    {
112
        // Default options if none provided
113
        if (! isset($options)) {
114
            $options = (new Options())
115
                ->setIsPhpEnabled(config('view-export.php_enabled'))
116
                ->setIsJavascriptEnabled(config('view-export.javascript_enabled'))
117
                ->setIsHtml5ParserEnabled(config('view-export.html5_parsable'))
118
                ->setIsRemoteEnabled(config('view-export.remote_enabled'));
119
        }
120
121
        // Set file permissions
122
        $options->setChroot(config('view-export.chroot'));
123
124
        // Return the options
125
        return $options;
126
    }
127
128
    /**
129
     * Load PDF content to the Dompdf instance.
130
     *
131
     *  - $content can be a View or HTML file contents
132
     *
133
     * @param View|string $content
134
     * @return $this
135
     * @throws Exception
136
     */
137
    private function loadContent($content): self
138
    {
139
        // Create local HTML file path
140
        $localHTML = StringHelpers::joinPaths($this->options->getRootDir(), uniqid().'.html');
141
142
        // Store View (or HTML) as HTML file within Dompdf root
143
        touch($localHTML);
144
        file_put_contents($localHTML, $content);
145
146
        // Load HTML
147
        $this->pdf->loadHtmlFile($localHTML);
148
149
        // Remove temp HTML file
150
        unlink($localHTML);
151
152
        return $this;
153
    }
154
155
    /**
156
     * Render a Dompdf & store it's output in a property.
157
     *
158
     *  - storing output in a property avoids potentially calling expensive 'output()' method multiple times
159
     *
160
     * @return void
161
     */
162
    private function render(): void
163
    {
164
        $this->pdf->render();
165
        $this->output = $this->pdf->output();
166
    }
167
}
168