Passed
Push — master ( c67edb...a841dd )
by Stephen
01:37 queued 11s
created

PdfRenderer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 24
c 5
b 0
f 0
dl 0
loc 122
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A applyMetadata() 0 10 3
A loadContent() 0 18 2
A render() 0 16 1
A handle() 0 3 1
A __construct() 0 10 1
A exporter() 0 3 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 Sfneal\Helpers\Strings\StringHelpers;
9
use Sfneal\ViewExport\Support\Exporter;
10
use Sfneal\ViewExport\Support\Renderer;
11
12
class PdfRenderer extends Renderer
13
{
14
    /**
15
     * @var Options
16
     */
17
    public $options;
18
19
    /**
20
     * @var Metadata
21
     */
22
    public $metadata;
23
24
    /**
25
     * @var Dompdf
26
     */
27
    private $pdf;
28
29
    /**
30
     * PdfRenderer constructor.
31
     *
32
     * @param string $content
33
     * @param string|null $uploadPath
34
     */
35
    public function __construct(string $content, string $uploadPath = null)
36
    {
37
        // Call Parent constructor
38
        parent::__construct($content, $uploadPath);
39
40
        // Declare PDF options (use DefaultOptions) if none provided
41
        $this->options = new DefaultOptions();
42
43
        // Instantiate Metadata
44
        $this->metadata = new Metadata();
45
    }
46
47
    /**
48
     * Render the PDF & return a Dompdf instance.
49
     *
50
     * @return Dompdf
51
     * @throws Exception
52
     */
53
    protected function render(): Dompdf
54
    {
55
        // Instantiate dompdf
56
        $this->pdf = new Dompdf($this->options);
57
58
        // Add metadata
59
        $this->applyMetadata();
60
61
        // Load content
62
        $this->loadContent();
63
64
        // Render the PDF
65
        $this->pdf->render();
66
67
        // Return the PDF
68
        return $this->pdf;
69
    }
70
71
    /**
72
     * Initialize the Exporter.
73
     *
74
     * @param $exportable
75
     * @return PdfExporter
76
     */
77
    protected function exporter($exportable): PdfExporter
78
    {
79
        return new PdfExporter($exportable);
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
        // todo: implement this if it improves performance
107
//        $this->pdf->loadHtml($this->content);
108
109
        // Create local HTML file path
110
        $localHTML = StringHelpers::joinPaths($this->options->getRootDir(), uniqid().'.html');
111
112
        // Store View (or HTML) as HTML file within Dompdf root
113
        touch($localHTML);
114
        file_put_contents($localHTML, $this->content);
115
116
        // Load HTML
117
        $this->pdf->loadHtmlFile($localHTML);
118
119
        // Remove temp HTML file if app is NOT in 'debug' mode
120
        if (! config('app.debug')) {
121
            unlink($localHTML);
122
        }
123
    }
124
125
    /**
126
     * Load renderable content to an Exporter instance and render the output.
127
     *
128
     * @return Exporter|PdfExporter
129
     * @throws Exception
130
     */
131
    public function handle(): PdfExporter
132
    {
133
        return parent::handle();
134
    }
135
}
136