PdfRenderer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
eloc 26
c 8
b 1
f 0
dl 0
loc 124
rs 10
wmc 11

6 Methods

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