Passed
Pull Request — master (#27)
by Stephen
02:38
created

PdfExporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 3
c 5
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 3
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
    // todo: make dispatchable
15
    use Accessors;
16
17
    /**
18
     * @var Options
19
     */
20
    public $options;
21
22
    /**
23
     * @var Metadata
24
     */
25
    public $metadata;
26
27
    /**
28
     * @var Dompdf
29
     */
30
    private $pdf;
31
32
    /**
33
     * @var View|string
34
     */
35
    private $content;
36
37
    /**
38
     * PdfExporter constructor.
39
     *
40
     * - $content can be a View or HTML file contents
41
     *
42
     * @param View|string $content
43
     * @param Options|null $options
44
     * @param array|null $metadata
45
     */
46
    public function __construct($content, Options $options = null, array $metadata = null)
47
    {
48
        // Declare PDF options (use DefaultOptions) if none provided
49
        $this->options = $options ?? new DefaultOptions();
50
51
        // Instantiate Metadata
52
        $this->metadata = new Metadata($metadata);
53
54
        // Content of the PDF
55
        $this->content = $content;
56
    }
57
58
    /**
59
     * Load PDF content to the Dompdf instance and render the output.
60
     *
61
     *  - storing output in a property avoids potentially calling expensive 'output()' method multiple times
62
     *
63
     * @return $this
64
     * @throws Exception
65
     */
66
    public function render(): self
67
    {
68
        // Instantiate dompdf
69
        $this->pdf = new Dompdf($this->options);
70
71
        // Add metadata
72
        $this->applyMetadata();
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
     * Add Metadata to the PDF.
132
     *
133
     * @return bool
134
     */
135
    private function applyMetadata(): bool
136
    {
137
        // Add Metadata if the array isn't empty
138
        if ($hasMetadata = ! empty($this->metadata->get())) {
139
            foreach ($this->metadata->get() as $key => $value) {
140
                $this->pdf->add_info($key, $value);
141
            }
142
        }
143
144
        return $hasMetadata;
145
    }
146
}
147