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