DOMPDFGenerator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
/**
4
 * Platine PDF
5
 *
6
 * Platine PDF is the lightweight for generating PDF documents
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine PDF
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file DOMPDFGenerator.php
33
 *
34
 *  The DOMPDF generator class
35
 *
36
 *  @package    Platine\PDF\Generator
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\PDF\Generator;
48
49
use Dompdf\Dompdf;
50
use Platine\Filesystem\Filesystem;
51
use Platine\PDF\PDFGeneratorInterface;
52
use RuntimeException;
53
54
/**
55
 * @class DOMPDFGenerator
56
 * @package Platine\PDF\Generator
57
 */
58
class DOMPDFGenerator implements PDFGeneratorInterface
59
{
60
    /**
61
    * Whether the document already rendered call
62
     * mean to render() method
63
    * @var bool
64
    */
65
    protected $rendered = false;
66
67
    /**
68
    * The PDF generated filename
69
    * @var string
70
    */
71
    protected $filename = 'dompdf.pdf';
72
73
    /**
74
     * Create new instance
75
     * @param Dompdf $dompdf
76
     * @param Filesystem $filesystem
77
     */
78
    public function __construct(protected Dompdf $dompdf, protected Filesystem $filesystem)
79
    {
80
    }
81
82
    /**
83
     * Return the DOMPDF
84
     * @return Dompdf
85
     */
86
    public function getDompdf(): Dompdf
87
    {
88
        return $this->dompdf;
89
    }
90
91
    /**
92
     * Set the DOMPDF instance
93
     * @param Dompdf $dompdf
94
     * @return $this
95
     */
96
    public function setDompdf(Dompdf $dompdf): self
97
    {
98
        $this->dompdf = $dompdf;
99
        return $this;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function generate(
106
        string $content,
107
        string $filename = 'output.pdf',
108
        string $format = 'A4',
109
        string $orientation = 'portrait'
110
    ): void {
111
        $this->filename = $filename;
112
        $this->dompdf->loadHtml($content);
113
        $this->dompdf->setPaper($format, $orientation);
114
        $this->dompdf->render();
115
116
        $this->rendered = true;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function raw(): string
123
    {
124
        $this->checkIfAlreadyRendered();
125
126
        return (string) $this->dompdf->output();
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function save(): void
133
    {
134
        $this->checkIfAlreadyRendered();
135
136
        $this->filesystem->file($this->filename)
137
                         ->write($this->raw());
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function download(): void
144
    {
145
        $this->checkIfAlreadyRendered();
146
        $this->dompdf->stream($this->filename);
147
    }
148
149
    /**
150
     * Disable the SSL verification
151
     * @return $this
152
     */
153
    public function disableSslVerify(): self
154
    {
155
        $context = stream_context_create([
156
            'ssl' => [
157
                'verify_peer' => false,
158
                'verify_peer_name' => false,
159
                'allow_self_signed' => true
160
            ]
161
        ]);
162
163
        $this->dompdf->setHttpContext($context);
164
165
        return $this;
166
    }
167
168
    /**
169
     * Check if the document already rendered
170
     * @return void
171
     * @throws RuntimeException
172
     */
173
    protected function checkIfAlreadyRendered(): void
174
    {
175
        if ($this->rendered === false) {
176
            throw new RuntimeException('You must render the document first');
177
        }
178
    }
179
}
180