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