Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.30% |
36 / 37 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TCPDF | |
97.30% |
36 / 37 |
|
66.67% |
2 / 3 |
7 | |
0.00% |
0 / 1 |
| createExternalWriterInstance | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| prepareToWrite | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| save | |
100.00% |
19 / 19 |
|
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 | |
| 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); |
| 55 | |
| 56 | if ($this->getFont()) { |
| 57 | $instance->setFont($this->getFont(), $instance->getFontStyle(), $instance->getFontSizePt()); |
| 58 | } |
| 59 | |
| 60 | return $instance; |
| 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(); |
| 74 | $customStyles = Style::getStyles(); |
| 75 | $normal = $customStyles['Normal'] ?? null; |
| 76 | if ($normal instanceof Style\Paragraph) { |
| 77 | $before = $normal->getSpaceBefore(); |
| 78 | $after = $normal->getSpaceAfter(); |
| 79 | if (is_numeric($before) && is_numeric($after)) { |
| 80 | $height = $normal->getLineHeight() ?? ''; |
| 81 | $pdf->setHtmlVSpace([ |
| 82 | 'p' => [ |
| 83 | ['n' => $before, 'h' => $height], |
| 84 | ['n' => $after, 'h' => $height], |
| 85 | ], |
| 86 | ]); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Save PhpWord to file. |
| 93 | */ |
| 94 | public function save(string $filename): void |
| 95 | { |
| 96 | $fileHandle = parent::prepareForSave($filename); |
| 97 | |
| 98 | // PDF settings |
| 99 | $paperSize = strtoupper(Settings::getDefaultPaper()); |
| 100 | $orientation = 'P'; |
| 101 | |
| 102 | // Create PDF |
| 103 | $pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize); |
| 104 | $pdf->setFontSubsetting(false); |
| 105 | $pdf->setPrintHeader(false); |
| 106 | $pdf->setPrintFooter(false); |
| 107 | $pdf->SetFont($this->getFont()); |
| 108 | $this->prepareToWrite($pdf); |
| 109 | $pdf->writeHTML($this->getContent()); |
| 110 | |
| 111 | // Write document properties |
| 112 | $phpWord = $this->getPhpWord(); |
| 113 | $docProps = $phpWord->getDocInfo(); |
| 114 | $pdf->SetTitle($docProps->getTitle()); |
| 115 | $pdf->SetAuthor($docProps->getCreator()); |
| 116 | $pdf->SetSubject($docProps->getSubject()); |
| 117 | $pdf->SetKeywords($docProps->getKeywords()); |
| 118 | $pdf->SetCreator($docProps->getCreator()); |
| 119 | |
| 120 | // Write to file |
| 121 | fwrite($fileHandle, $pdf->Output($filename, 'S')); |
| 122 | |
| 123 | parent::restoreStateAfterSave($fileHandle); |
| 124 | } |
| 125 | } |