• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 13025401639

29 Jan 2025 05:48AM UTC coverage: 96.825% (-0.4%) from 97.217%
13025401639

Pull #2562

github

web-flow
Merge ebe3e5dac into 2d2759585
Pull Request #2562: TemplateProcessor SetComplexBlock/Value and Sections

6 of 7 new or added lines in 1 file covered. (85.71%)

245 existing lines in 40 files now uncovered.

12227 of 12628 relevant lines covered (96.82%)

34.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.3
/src/PhpWord/Writer/PDF/TCPDF.php
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

19
namespace PhpOffice\PhpWord\Writer\PDF;
20

21
use PhpOffice\PhpWord\PhpWord;
22
use PhpOffice\PhpWord\Settings;
23
use PhpOffice\PhpWord\Style;
24
use PhpOffice\PhpWord\Writer\WriterInterface;
25
use TCPDF as TCPDFBase;
26

27
/**
28
 * TCPDF writer.
29
 *
30
 * @deprecated 0.13.0 Use `DomPDF` or `MPDF` instead.
31
 * @see  http://www.tcpdf.org/
32
 * @since 0.11.0
33
 */
34
class TCPDF extends AbstractRenderer implements WriterInterface
35
{
36
    /**
37
     * Name of renderer include file.
38
     *
39
     * @var string
40
     */
41
    protected $includeFile = 'tcpdf.php';
42

43
    /**
44
     * Gets the implementation of external PDF library that should be used.
45
     *
46
     * @param string $orientation Page orientation
47
     * @param string $unit Unit measure
48
     * @param string $paperSize Paper size
49
     *
50
     * @return TCPDFBase implementation
51
     */
52
    protected function createExternalWriterInstance($orientation, $unit, $paperSize)
53
    {
54
        $instance = new TCPDFBase($orientation, $unit, $paperSize);
1✔
55

56
        if ($this->getFont()) {
1✔
UNCOV
57
            $instance->setFont($this->getFont(), $instance->getFontStyle(), $instance->getFontSizePt());
×
58
        }
59

60
        return $instance;
1✔
61
    }
62

63
    /**
64
     * Overwriteable function to allow user to extend TCPDF.
65
     * There should always be an AddPage call, preceded or followed
66
     *   by code to customize TCPDF configuration.
67
     * The customization below sets vertical spacing
68
     *   between paragaraphs when the user has
69
     *   explicitly set those values to numeric in default style.
70
     */
71
    protected function prepareToWrite(TCPDFBase $pdf): void
72
    {
73
        $pdf->AddPage();
1✔
74
        $customStyles = Style::getStyles();
1✔
75
        $normal = $customStyles['Normal'] ?? null;
1✔
76
        if ($normal instanceof Style\Paragraph) {
1✔
77
            $before = $normal->getSpaceBefore();
1✔
78
            $after = $normal->getSpaceAfter();
1✔
79
            if (is_numeric($before) && is_numeric($after)) {
1✔
80
                $height = $normal->getLineHeight() ?? '';
1✔
81
                $pdf->setHtmlVSpace([
1✔
82
                    'p' => [
1✔
83
                        ['n' => $before, 'h' => $height],
1✔
84
                        ['n' => $after, 'h' => $height],
1✔
85
                    ],
1✔
86
                ]);
1✔
87
            }
88
        }
89
    }
90

91
    /**
92
     * Save PhpWord to file.
93
     */
94
    public function save(string $filename): void
95
    {
96
        $fileHandle = parent::prepareForSave($filename);
1✔
97

98
        //  PDF settings
99
        $paperSize = strtoupper(Settings::getDefaultPaper());
1✔
100
        $orientation = 'P';
1✔
101

102
        // Create PDF
103
        $pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize);
1✔
104
        $pdf->setFontSubsetting(false);
1✔
105
        $pdf->setPrintHeader(false);
1✔
106
        $pdf->setPrintFooter(false);
1✔
107
        $pdf->SetFont($this->getFont());
1✔
108
        $this->prepareToWrite($pdf);
1✔
109
        $pdf->writeHTML($this->getContent());
1✔
110

111
        // Write document properties
112
        $phpWord = $this->getPhpWord();
1✔
113
        $docProps = $phpWord->getDocInfo();
1✔
114
        $pdf->SetTitle($docProps->getTitle());
1✔
115
        $pdf->SetAuthor($docProps->getCreator());
1✔
116
        $pdf->SetSubject($docProps->getSubject());
1✔
117
        $pdf->SetKeywords($docProps->getKeywords());
1✔
118
        $pdf->SetCreator($docProps->getCreator());
1✔
119

120
        //  Write to file
121
        fwrite($fileHandle, $pdf->Output($filename, 'S'));
1✔
122

123
        parent::restoreStateAfterSave($fileHandle);
1✔
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc