Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DomPDF
92.31% covered (success)
92.31%
12 / 13
50.00% covered (danger)
50.00%
1 / 2
3.00
0.00% covered (danger)
0.00%
0 / 1
 createExternalWriterInstance
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 save
100.00% covered (success)
100.00%
9 / 9
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 Dompdf\Dompdf as DompdfLib;
21use Dompdf\Options;
22use PhpOffice\PhpWord\Writer\WriterInterface;
23
24/**
25 * DomPDF writer.
26 *
27 * @see  https://github.com/dompdf/dompdf
28 * @since 0.10.0
29 */
30class DomPDF extends AbstractRenderer implements WriterInterface
31{
32    /**
33     * Name of renderer include file.
34     *
35     * @var string
36     */
37    protected $includeFile;
38
39    /**
40     * Gets the implementation of external PDF library that should be used.
41     *
42     * @return Dompdf implementation
43     */
44    protected function createExternalWriterInstance()
45    {
46        $options = new Options();
47        if ($this->getFont()) {
48            $options->set('defaultFont', $this->getFont());
49        }
50
51        return new DompdfLib($options);
52    }
53
54    /**
55     * Save PhpWord to file.
56     */
57    public function save(string $filename): void
58    {
59        $fileHandle = parent::prepareForSave($filename);
60
61        //  PDF settings
62        $paperSize = 'A4';
63        $orientation = 'portrait';
64
65        //  Create PDF
66        $pdf = $this->createExternalWriterInstance();
67        $pdf->setPaper(strtolower($paperSize), $orientation);
68        $pdf->loadHtml(str_replace(PHP_EOL, '', $this->getContent()));
69        $pdf->render();
70
71        //  Write to file
72        fwrite($fileHandle, $pdf->output());
73
74        parent::restoreStateAfterSave($fileHandle);
75    }
76}