Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
SpreadsheetWriter | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 1 |
toPdfString | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Utils\Parser\Spreadsheet |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Utils\Parser\Spreadsheet; |
16 | |
17 | use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
18 | use PhpOffice\PhpSpreadsheet\Writer\Html; |
19 | use PhpOffice\PhpSpreadsheet\Writer\Pdf; |
20 | |
21 | /** |
22 | * Spreadsheet writer |
23 | * |
24 | * @package phpOMS\Utils\Parser\Spreadsheet |
25 | * @license OMS License 2.0 |
26 | * @link https://jingga.app |
27 | * @since 1.0.0 |
28 | */ |
29 | class SpreadsheetWriter extends Pdf |
30 | { |
31 | /** |
32 | * Save Spreadsheet to file. |
33 | * |
34 | * @return string |
35 | * |
36 | * @since 1.0.0 |
37 | */ |
38 | public function toPdfString() : string |
39 | { |
40 | $this->isMPdf = true; |
41 | |
42 | $pdf = new \Mpdf\Mpdf(); |
43 | |
44 | // Check for paper size and page orientation |
45 | $setup = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageSetup(); |
46 | $orientation = $this->getOrientation() ?? $setup->getOrientation(); |
47 | $orientation = ($orientation === PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
48 | $printPaperSize = $this->getPaperSize() ?? $setup->getPaperSize(); |
49 | $paperSize = self::$paperSizes[$printPaperSize] ?? PageSetup::getPaperSizeDefault(); |
50 | |
51 | $ortmp = $orientation; |
52 | $pdf->_setPageSize($paperSize, $ortmp); |
53 | $pdf->DefOrientation = $orientation; |
54 | $pdf->AddPageByArray([ |
55 | 'orientation' => $orientation, |
56 | 'margin-left' => $this->spreadsheet->getActiveSheet()->getPageMargins()->getLeft() * 25.4, |
57 | 'margin-right' => $this->spreadsheet->getActiveSheet()->getPageMargins()->getRight() * 25.4, |
58 | 'margin-top' => $this->spreadsheet->getActiveSheet()->getPageMargins()->getTop() * 25.4, |
59 | 'margin-bottom' => $this->spreadsheet->getActiveSheet()->getPageMargins()->getBottom() * 25.4, |
60 | ]); |
61 | |
62 | // Document info |
63 | $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle()); |
64 | $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator()); |
65 | $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject()); |
66 | $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords()); |
67 | $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); |
68 | |
69 | $html = $this->generateHTMLAll(); |
70 | $bodyLocation = \strpos($html, Html::BODY_LINE); |
71 | |
72 | // Make sure first data presented to Mpdf includes body tag |
73 | // so that Mpdf doesn't parse it as content. Issue 2432. |
74 | if ($bodyLocation !== false) { |
75 | $bodyLocation += \strlen(Html::BODY_LINE); |
76 | $pdf->WriteHTML(\substr($html, 0, $bodyLocation)); |
77 | $html = \substr($html, $bodyLocation); |
78 | } |
79 | |
80 | foreach (\array_chunk(\explode(\PHP_EOL, $html), 1000) as $lines) { |
81 | $pdf->WriteHTML(\implode(\PHP_EOL, $lines)); |
82 | } |
83 | |
84 | $html = $pdf->Output('', 'S'); |
85 | |
86 | parent::restoreStateAfterSave(); |
87 | |
88 | return $html; |
89 | } |
90 | } |