Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.30% covered (success)
97.30%
36 / 37
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TCPDF
97.30% covered (success)
97.30%
36 / 37
66.67% covered (warning)
66.67%
2 / 3
7
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
 prepareToWrite
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 save
100.00% covered (success)
100.00%
19 / 19
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\PhpWord;
21use PhpOffice\PhpWord\Settings;
22use PhpOffice\PhpWord\Style;
23use PhpOffice\PhpWord\Writer\WriterInterface;
24use TCPDF as TCPDFBase;
25
26/**
27 * TCPDF writer.
28 *
29 * @deprecated 0.13.0 Use `DomPDF` or `MPDF` instead.
30 * @see  http://www.tcpdf.org/
31 * @since 0.11.0
32 */
33class TCPDF extends AbstractRenderer implements WriterInterface
34{
35    /**
36     * Name of renderer include file.
37     *
38     * @var string
39     */
40    protected $includeFile = 'tcpdf.php';
41
42    /**
43     * Gets the implementation of external PDF library that should be used.
44     *
45     * @param string $orientation Page orientation
46     * @param string $unit Unit measure
47     * @param string $paperSize Paper size
48     *
49     * @return TCPDFBase implementation
50     */
51    protected function createExternalWriterInstance($orientation, $unit, $paperSize)
52    {
53        $instance = new TCPDFBase($orientation, $unit, $paperSize);
54
55        if ($this->getFont()) {
56            $instance->setFont($this->getFont(), $instance->getFontStyle(), $instance->getFontSizePt());
57        }
58
59        return $instance;
60    }
61
62    /**
63     * Overwriteable function to allow user to extend TCPDF.
64     * There should always be an AddPage call, preceded or followed
65     *   by code to customize TCPDF configuration.
66     * The customization below sets vertical spacing
67     *   between paragaraphs when the user has
68     *   explicitly set those values to numeric in default style.
69     */
70    protected function prepareToWrite(TCPDFBase $pdf): void
71    {
72        $pdf->AddPage();
73        $customStyles = Style::getStyles();
74        $normal = $customStyles['Normal'] ?? null;
75        if ($normal instanceof Style\Paragraph) {
76            $before = $normal->getSpaceBefore();
77            $after = $normal->getSpaceAfter();
78            if (is_numeric($before) && is_numeric($after)) {
79                $height = $normal->getLineHeight() ?? '';
80                $pdf->setHtmlVSpace([
81                    'p' => [
82                        ['n' => $before, 'h' => $height],
83                        ['n' => $after, 'h' => $height],
84                    ],
85                ]);
86            }
87        }
88    }
89
90    /**
91     * Save PhpWord to file.
92     */
93    public function save(string $filename): void
94    {
95        $fileHandle = parent::prepareForSave($filename);
96
97        //  PDF settings
98        $paperSize = strtoupper(Settings::getDefaultPaper());
99        $orientation = 'P';
100
101        // Create PDF
102        $pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize);
103        $pdf->setFontSubsetting(false);
104        $pdf->setPrintHeader(false);
105        $pdf->setPrintFooter(false);
106        $pdf->SetFont($this->getFont());
107        $this->prepareToWrite($pdf);
108        $pdf->writeHTML($this->getContent());
109
110        // Write document properties
111        $phpWord = $this->getPhpWord();
112        $docProps = $phpWord->getDocInfo();
113        $pdf->SetTitle($docProps->getTitle());
114        $pdf->SetAuthor($docProps->getCreator());
115        $pdf->SetSubject($docProps->getSubject());
116        $pdf->SetKeywords($docProps->getKeywords());
117        $pdf->SetCreator($docProps->getCreator());
118
119        //  Write to file
120        fwrite($fileHandle, $pdf->Output($filename, 'S'));
121
122        parent::restoreStateAfterSave($fileHandle);
123    }
124}