Test Failed
Push — 1.0.0-dev ( 000a84...a27cf6 )
by nguereza
02:52
created

PDF::setHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
    defined('ROOT_PATH') or exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
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
     * PDF library to generate PDF document using the library DOMPDF
33
     */
34
    class PDF extends BaseClass {
35
		
36
        /**
37
         * The dompdf instance
38
         * @var Dompdf
39
         */
40
        private $dompdf = null;
41
42
        /**
43
         * The PDF generated filename
44
         * @var string
45
         */
46
        private $filename = 'output.pdf';
47
48
        /**
49
         * The HTML content
50
         * @var string
51
         */
52
        private $html = null;
53
54
        /**
55
         * The PDF document paper type like 'A4', 'A5', 'letter', etc.
56
         * @var string
57
         */
58
        private $paper = 'A4';
59
60
        /**
61
         * The PDF document orientation like 'portrait', 'landscape'
62
         * @var string
63
         */
64
        private $orientation = 'portrait';
65
		
66
        /**
67
         * Create PDF library instance
68
         */
69
        public function __construct() {
70
            parent::__construct();
71
            require_once VENDOR_PATH . 'dompdf/dompdf_config.inc.php';
72
            $this->dompdf = new Dompdf();
73
        }
74
75
        /**
76
         * Return the instance of Dompdf
77
         *
78
         * @return object the dompdf instance
79
         */
80
        public function getDompdf() {
81
            return $this->dompdf;
82
        }
83
84
        /**
85
         * This method is the shortcut to Dompdf::render
86
         * @return object the current instance
87
         */
88
        public function render() {
89
           $this->dompdf->load_html($this->html);
90
           $this->dompdf->set_paper($this->paper, $this->orientation);
91
           $this->dompdf->render(); 
92
           return $this;
93
        }
94
95
        /**
96
         * Set the filename of generated PDF document
97
         * @param string $filename the filename
98
         *
99
         * @return object the current instance
100
         */
101
        public function setFilename($filename) {
102
            if(stripos($filename, '.pdf') === false) {
103
                $filename .= '.pdf';     
104
            }
105
            $this->filename = $filename;
106
            return $this;
107
        }
108
109
        /**
110
         * Set the HTML content to use to generate the PDF
111
         * @param string $html the content of HTML
112
         *
113
         * @return object the current instance
114
         */
115
        public function setHtml($html) {
116
            $this->html = $html; 
117
            return $this;
118
        }
119
120
        /**
121
         * Set the page paper of the generated PDF
122
         * @param string $paper the page paper like "A4", "letter", etc.
123
         *
124
         * @return object the current instance
125
         */
126
        public function setPaper($paper) {
127
            $this->paper = $paper; 
128
            return $this;
129
        }
130
131
        /**
132
         * Set the page orientation of the generated PDF to "portrait"
133
         *
134
         * @return object the current instance
135
         */
136
        public function portrait() {
137
            $this->orientation = 'portrait'; 
138
            return $this;
139
        }
140
141
        /**
142
         * Set the page orientation of the generated PDF to "portrait"
143
         *
144
         * @return object the current instance
145
         */
146
        public function landscape() {
147
            $this->orientation = 'landscape'; 
148
            return $this;
149
        }
150
151
        /**
152
         * Download the generated PDF document
153
         * @codeCoverageIgnore
154
         * 
155
         * @return void
156
         */
157
        public function download() {
158
            $this->logger->info('Download of PDF document: filename [' . $this->filename . '], '
159
                                . 'paper [' . $this->paper . '], orientation [' . $this->orientation . ']');
160
            $this->prepare();
161
            $this->dompdf->stream($this->filename);
162
        }
163
164
        /**
165
         * Return the content of the generated PDF document as string
166
         * @return string
167
         */
168
        public function content() {
169
            $this->logger->info('Return of PDF document as string: paper '
170
                                . '[' . $this->paper . '], orientation [' . $this->orientation . ']');
171
            $this->prepare();
172
            return $this->dompdf->output();
173
        }
174
175
        /**
176
         * Save the content of the generated PDF document on the server filesystem
177
         * @return void
178
         */
179
        public function save() {
180
            $this->logger->info('Saving PDF document : filename path [' . $this->filename . '], paper '
181
                                . '[' . $this->paper . '], orientation [' . $this->orientation . ']');
182
            file_put_contents($this->filename, $this->content());
183
        }
184
185
        /**
186
         * Prepare the PDF to generate 
187
         * @return void
188
         */
189
        protected function prepare() {
190
            //If the canvas instance is null so means the method "render"
191
            // not yet called
192
            if ($this->dompdf->get_canvas() === null) {
193
                $this->render();  
194
            }
195
        }
196
		
197
    }
198