Passed
Pull Request — master (#4590)
by Owen
15:56
created

Dompdf::save()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 8.6506
c 0
b 0
f 0
cc 7
nc 32
nop 2
crap 7
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
4
5
require_once __DIR__ . '/DompdfImageDestroy1.php';
6
require_once __DIR__ . '/DompdfImageDestroy2.php';
7
require_once __DIR__ . '/DompdfImageDestroy3.php';
8
require_once __DIR__ . '/DompdfImageDestroy4.php';
9
10
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
11
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
12
13
class Dompdf extends Pdf
14
{
15
    /**
16
     * embed images, or link to images.
17
     */
18
    protected bool $embedImages = true;
19
20 6
    /**
21
     * Gets the implementation of external PDF library that should be used.
22 6
     *
23
     * @return \Dompdf\Dompdf implementation
24
     */
25
    protected function createExternalWriterInstance(): \Dompdf\Dompdf
26
    {
27
        return new \Dompdf\Dompdf();
28
    }
29
30 6
    /**
31
     * Save Spreadsheet to file.
32 6
     *
33
     * @param string $filename Name of the file to save as
34
     */
35 6
    public function save($filename, int $flags = 0): void
36 6
    {
37 6
        $fileHandle = parent::prepareForSave($filename);
38 6
39 6
        //  Check for paper size and page orientation
40 6
        $setup = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageSetup();
41 1
        $orientation = $this->getOrientation() ?? $setup->getOrientation();
42
        $orientation = ($orientation === PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
43
        $printPaperSize = $this->getPaperSize() ?? $setup->getPaperSize();
44 6
        $paperSize = self::$paperSizes[$printPaperSize] ?? self::$paperSizes[PageSetup::getPaperSizeDefault()] ?? 'LETTER';
45
        if (is_array($paperSize) && count($paperSize) === 2) {
46
            $paperSize = [0.0, 0.0, $paperSize[0], $paperSize[1]];
47 6
        }
48 6
49
        $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
50 6
51 6
        //  Create PDF
52
        $restoreHandler = false;
53
        if (PHP_VERSION_ID >= self::$temporaryVersionCheck) {
54 6
            set_error_handler(self::specialErrorHandler(...));
55
            $restoreHandler = true;
56 6
        }
57
        $pdf = $this->createExternalWriterInstance();
58
        $pdf->setPaper($paperSize, $orientation);
59
60
        $pdf->loadHtml($this->generateHTMLAll());
61
        $pdf->render();
62
63
        //  Write to file
64
        fwrite($fileHandle, $pdf->output() ?? '');
65
66
        if ($restoreHandler) {
67
            restore_error_handler();
68
        }
69
        parent::restoreStateAfterSave();
70
    }
71
72
    protected static int $temporaryVersionCheck = 80500;
73
74
    public function specialErrorHandler(int $errno, string $errstr, string $filename, int $lineno): bool
75
    {
76
        if ($errno === E_DEPRECATED) {
77
            if (str_ends_with($filename, 'AttributeTranslator.php') && $lineno === 506) {
78
                return true;
79
            }
80
        }
81
82
        return false; // continue error handling
83
    }
84
}
85