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

PHPOffice / PHPWord / 13461426829

21 Feb 2025 04:52PM UTC coverage: 96.905% (+0.1%) from 96.767%
13461426829

Pull #2567

github

web-flow
Merge e64a82db6 into 6ca8c9ff6
Pull Request #2567: WIP Do Not Install

300 of 307 new or added lines in 26 files covered. (97.72%)

161 existing lines in 21 files now uncovered.

12681 of 13086 relevant lines covered (96.91%)

37.43 hits per line

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

96.77
/src/PhpWord/Writer/PDF/MPDF.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\Writer\WriterInterface;
24

25
/**
26
 * MPDF writer.
27
 *
28
 * @see  http://www.mpdf1.com/
29
 * @since 0.11.0
30
 */
31
class MPDF extends AbstractRenderer implements WriterInterface
32
{
33
    public const SIMULATED_BODY_START = '<!-- simulated body start -->';
34
    private const BODY_TAG = '<body>';
35

36
    /**
37
     * Overridden to set the correct includefile, only needed for MPDF 5.
38
     *
39
     * @codeCoverageIgnore
40
     */
41
    public function __construct(PhpWord $phpWord)
42
    {
43
        if (file_exists(Settings::getPdfRendererPath() . '/mpdf.php')) {
44
            // MPDF version 5.* needs this file to be included, later versions not
45
            $this->includeFile = 'mpdf.php';
46
        }
47
        parent::__construct($phpWord);
48
    }
49

50
    /**
51
     * Gets the implementation of external PDF library that should be used.
52
     *
53
     * @return \Mpdf\Mpdf implementation
54
     */
55
    protected function createExternalWriterInstance()
56
    {
57
        $mPdfClass = $this->getMPdfClassName();
2✔
58

59
        $options = [];
2✔
60
        if ($this->getFont()) {
2✔
UNCOV
61
            $options['default_font'] = $this->getFont();
×
62
        }
63

64
        return new $mPdfClass($options);
2✔
65
    }
66

67
    /**
68
     * Save PhpWord to file.
69
     */
70
    public function save(string $filename): void
71
    {
72
        $fileHandle = parent::prepareForSave($filename);
2✔
73

74
        //  PDF settings
75
        $paperSize = strtoupper('A4');
2✔
76
        $orientation = strtoupper('portrait');
2✔
77

78
        //  Create PDF
79
        $pdf = $this->createExternalWriterInstance();
2✔
80
        $pdf->_setPageSize($paperSize, $orientation);
2✔
81
        $pdf->addPage($orientation);
2✔
82

83
        // Write document properties
84
        $phpWord = $this->getPhpWord();
2✔
85
        $docProps = $phpWord->getDocInfo();
2✔
86
        $pdf->setTitle($docProps->getTitle());
2✔
87
        $pdf->setAuthor($docProps->getCreator());
2✔
88
        $pdf->setSubject($docProps->getSubject());
2✔
89
        $pdf->setKeywords($docProps->getKeywords());
2✔
90
        $pdf->setCreator($docProps->getCreator());
2✔
91

92
        $html = $this->getContent();
2✔
93
        $bodyLocation = strpos($html, self::SIMULATED_BODY_START);
2✔
94
        if ($bodyLocation === false) {
2✔
95
            $bodyLocation = strpos($html, self::BODY_TAG);
1✔
96
            if ($bodyLocation !== false) {
1✔
97
                $bodyLocation += strlen(self::BODY_TAG);
1✔
98
            }
99
        }
100
        // Make sure first data presented to Mpdf includes body tag
101
        //   (and any htmlpageheader/htmlpagefooter tags)
102
        //   so that Mpdf doesn't parse it as content. Issue 2432.
103
        if ($bodyLocation !== false) {
2✔
104
            $pdf->WriteHTML(substr($html, 0, $bodyLocation));
2✔
105
            $html = substr($html, $bodyLocation);
2✔
106
        }
107
        foreach (explode("\n", $html) as $line) {
2✔
108
            $pdf->WriteHTML("$line\n");
2✔
109
        }
110

111
        //  Write to file
112
        fwrite($fileHandle, $pdf->output($filename, 'S'));
2✔
113

114
        parent::restoreStateAfterSave($fileHandle);
2✔
115
    }
116

117
    /**
118
     * Return classname of MPDF to instantiate.
119
     *
120
     * @codeCoverageIgnore
121
     *
122
     * @return string
123
     */
124
    private function getMPdfClassName()
125
    {
126
        if ($this->includeFile != null) {
127
            // MPDF version 5.*
128
            return '\mpdf';
129
        }
130

131
        // MPDF version > 6.*
132
        return '\Mpdf\Mpdf';
133
    }
134
}
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