Passed
Pull Request — master (#25)
by Stephen
03:16
created

PdfExporter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 8
eloc 26
c 8
b 2
f 0
dl 0
loc 146
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 3 1
A getPath() 0 3 1
A download() 0 3 1
A render() 0 25 1
A getOutput() 0 3 1
A view() 0 3 1
A __construct() 0 7 1
A upload() 0 6 1
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
    public $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
     * @var View|string
41
     */
42
    private $content;
43
44
    /**
45
     * PdfExporter constructor.
46
     *
47
     * - $content can be a View or HTML file contents
48
     *
49
     * @param View|string $content
50
     * @param Options|null $options
51
     */
52
    public function __construct($content, Options $options = null)
53
    {
54
        // Declare PDF options (use DefaultOptions) if none provided
55
        $this->options = $options ?? new DefaultOptions();
56
57
        // Content of the PDF
58
        $this->content = $content;
59
    }
60
61
    /**
62
     * Load PDF content to the Dompdf instance and render the output.
63
     *
64
     *  - storing output in a property avoids potentially calling expensive 'output()' method multiple times
65
     *
66
     * @return $this
67
     * @throws Exception
68
     */
69
    public function render(): self
70
    {
71
        // Instantiate dompdf
72
        $this->pdf = new Dompdf($this->options);
73
74
        // Create local HTML file path
75
        $localHTML = StringHelpers::joinPaths($this->options->getRootDir(), uniqid().'.html');
76
77
        // Store View (or HTML) as HTML file within Dompdf root
78
        touch($localHTML);
79
        file_put_contents($localHTML, $this->content);
80
81
        // Load HTML
82
        $this->pdf->loadHtmlFile($localHTML);
83
84
        // Remove temp HTML file
85
        unlink($localHTML);
86
87
        // Render the PDF
88
        $this->pdf->render();
89
90
        // Store output to a property to avoid retrieving twice
91
        $this->output = $this->pdf->output();
92
93
        return $this;
94
    }
95
96
    /**
97
     * Upload a rendered PDF to an AWS S3 file store.
98
     *
99
     * @param string $path
100
     * @return $this
101
     */
102
    public function upload(string $path): self
103
    {
104
        $this->path = $path;
105
        $this->url = (new S3($path))->upload_raw($this->getOutput());
106
107
        return $this;
108
    }
109
110
    /**
111
     * View the PDF in the clients browser.
112
     *
113
     * @param string $filename
114
     */
115
    public function view(string $filename = 'output.pdf')
116
    {
117
        $this->pdf->stream($filename, ['Attachment' => false]);
118
    }
119
120
    /**
121
     * Download the PDF using the clients browser.
122
     *
123
     * @param string $filename
124
     */
125
    public function download(string $filename = 'output.pdf')
126
    {
127
        $this->pdf->stream($filename, ['Attachment' => true]);
128
    }
129
130
    /**
131
     * Retrieve the PDF's output.
132
     *
133
     * @return string
134
     */
135
    public function getOutput(): string
136
    {
137
        return $this->output;
138
    }
139
140
    /**
141
     * Retrieve the PDF's AWS S3 path.
142
     *
143
     * @return string|null
144
     */
145
    public function getPath(): ?string
146
    {
147
        return $this->path;
148
    }
149
150
    /**
151
     * Retrieve the PDF's AWS S3 url.
152
     *
153
     * @return string|null
154
     */
155
    public function getUrl(): ?string
156
    {
157
        return $this->url;
158
    }
159
}
160