Passed
Push — master ( 5ce395...6de31f )
by Stephen
01:07 queued 11s
created

PdfRenderer::applyMetadata()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 2
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
        // Create local HTML file path
73
        $localHTML = StringHelpers::joinPaths($this->options->getRootDir(), uniqid().'.html');
74
75
        // Store View (or HTML) as HTML file within Dompdf root
76
        touch($localHTML);
77
        file_put_contents($localHTML, $this->content);
78
79
        // Load HTML
80
        $this->pdf->loadHtmlFile($localHTML);
81
82
        // Remove temp HTML file
83
        unlink($localHTML);
84
85
        // Render the PDF
86
        $this->pdf->render();
87
88
        // Return a PdfExporter
89
        return new PdfExporter($this->pdf);
90
    }
91
92
    /**
93
     * Add Metadata to the PDF.
94
     *
95
     * @return bool
96
     */
97
    private function applyMetadata(): bool
98
    {
99
        // Add Metadata if the array isn't empty
100
        if ($hasMetadata = ! empty($this->metadata->get())) {
101
            foreach ($this->metadata->get() as $key => $value) {
102
                $this->pdf->add_info($key, $value);
103
            }
104
        }
105
106
        return $hasMetadata;
107
    }
108
}
109