Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractRenderer
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
9 / 9
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getFont
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFont
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPaperSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPaperSize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOrientation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOrientation
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 prepareForSave
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 restoreStateAfterSave
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PhpWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Writer\PDF;
19
20use PhpOffice\PhpWord\Exception\Exception;
21use PhpOffice\PhpWord\PhpWord;
22use PhpOffice\PhpWord\Settings;
23use PhpOffice\PhpWord\Writer\HTML;
24
25/**
26 * Abstract PDF renderer.
27 *
28 * @since 0.10.0
29 */
30abstract class AbstractRenderer extends HTML
31{
32    /**
33     * Name of renderer include file.
34     *
35     * @var string
36     */
37    protected $includeFile;
38
39    /**
40     * Temporary storage directory.
41     *
42     * @var string
43     */
44    protected $tempDir = '';
45
46    /**
47     * Font.
48     *
49     * @var string
50     */
51    protected $font;
52
53    /**
54     * Paper size.
55     *
56     * @var int
57     */
58    protected $paperSize;
59
60    /**
61     * Orientation.
62     *
63     * @var string
64     */
65    protected $orientation;
66
67    /**
68     * Paper Sizes xRef List.
69     *
70     * @var array
71     */
72    protected static $paperSizes = [
73        9 => 'A4', // (210 mm by 297 mm)
74    ];
75
76    /**
77     * Create new instance.
78     *
79     * @param PhpWord $phpWord PhpWord object
80     */
81    public function __construct(PhpWord $phpWord)
82    {
83        parent::__construct($phpWord);
84        $this->isPdf = true;
85        if ($this->includeFile != null) {
86            $includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
87            if (file_exists($includeFile)) {
88                /** @noinspection PhpIncludeInspection Dynamic includes */
89                require_once $includeFile;
90            } else {
91                // @codeCoverageIgnoreStart
92                // Can't find any test case. Uncomment when found.
93                throw new Exception('Unable to load PDF Rendering library');
94                // @codeCoverageIgnoreEnd
95            }
96        }
97
98        // Configuration
99        $options = Settings::getPdfRendererOptions();
100        if (!empty($options['font'])) {
101            $this->setFont($options['font']);
102        }
103    }
104
105    /**
106     * Get Font.
107     *
108     * @return string
109     */
110    public function getFont()
111    {
112        return $this->font;
113    }
114
115    /**
116     * Set font. Examples:
117     *      'arialunicid0-chinese-simplified'
118     *      'arialunicid0-chinese-traditional'
119     *      'arialunicid0-korean'
120     *      'arialunicid0-japanese'.
121     *
122     * @param string $fontName
123     *
124     * @return self
125     */
126    public function setFont($fontName)
127    {
128        $this->font = $fontName;
129
130        return $this;
131    }
132
133    /**
134     * Get Paper Size.
135     *
136     * @return int
137     */
138    public function getPaperSize()
139    {
140        return $this->paperSize;
141    }
142
143    /**
144     * Set Paper Size.
145     *
146     * @param int $value Paper size = PAPERSIZE_A4
147     *
148     * @return self
149     */
150    public function setPaperSize($value = 9)
151    {
152        $this->paperSize = $value;
153
154        return $this;
155    }
156
157    /**
158     * Get Orientation.
159     *
160     * @return string
161     */
162    public function getOrientation()
163    {
164        return $this->orientation;
165    }
166
167    /**
168     * Set Orientation.
169     *
170     * @param string $value Page orientation ORIENTATION_DEFAULT
171     *
172     * @return self
173     */
174    public function setOrientation($value = 'default')
175    {
176        $this->orientation = $value;
177
178        return $this;
179    }
180
181    /**
182     * Save PhpWord to PDF file, pre-save.
183     *
184     * @param string $filename Name of the file to save as
185     *
186     * @return resource
187     */
188    protected function prepareForSave($filename = null)
189    {
190        $fileHandle = fopen($filename, 'wb');
191        // @codeCoverageIgnoreStart
192        // Can't find any test case. Uncomment when found.
193        if ($fileHandle === false) {
194            throw new Exception("Could not open file $filename for writing.");
195        }
196        // @codeCoverageIgnoreEnd
197
198        return $fileHandle;
199    }
200
201    /**
202     * Save PhpWord to PDF file, post-save.
203     *
204     * @param resource $fileHandle
205     */
206    protected function restoreStateAfterSave($fileHandle): void
207    {
208        fclose($fileHandle);
209    }
210}