Passed
Push — master ( 6de31f...9eb67a )
by Stephen
58s queued 12s
created

PdfRenderer::loadContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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