Failed Conditions
Push — develop ( 0ef1b5...a1e8c8 )
by Mark
34:44
created

Tcpdf::createExternalWriterInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
4
5
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
6
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
7
8
class Tcpdf extends Pdf
9
{
10
    /**
11
     * Gets the implementation of external PDF library that should be used.
12
     *
13
     * @param string $orientation Page orientation
14
     * @param string $unit Unit measure
15
     * @param string $paperSize Paper size
16
     *
17
     * @return \TCPDF implementation
18
     */
19
    protected function createExternalWriterInstance($orientation, $unit, $paperSize)
20
    {
21
        return new \TCPDF($orientation, $unit, $paperSize);
22
    }
23
24
    /**
25
     * Save Spreadsheet to file.
26
     *
27
     * @param string $pFilename Name of the file to save as
28
     *
29
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
30
     */
31
    public function save($pFilename)
32
    {
33
        $fileHandle = parent::prepareForSave($pFilename);
34
35
        //  Default PDF paper size
36
        $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
37
38
        //  Check for paper size and page orientation
39
        if ($this->getSheetIndex() === null) {
40
            $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
41
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
42
            $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
43
            $printMargins = $this->spreadsheet->getSheet(0)->getPageMargins();
44
        } else {
45
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
46
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
47
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
48
            $printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins();
49
        }
50
51
        //  Override Page Orientation
52
        if ($this->getOrientation() !== null) {
53
            $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE)
54
                ? 'L'
55
                : 'P';
56
        }
57
        //  Override Paper Size
58
        if ($this->getPaperSize() !== null) {
59
            $printPaperSize = $this->getPaperSize();
60
        }
61
62
        if (isset(self::$paperSizes[$printPaperSize])) {
63
            $paperSize = self::$paperSizes[$printPaperSize];
64
        }
65
66
        //  Create PDF
67
        $pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize);
68
        $pdf->setFontSubsetting(false);
69
        //    Set margins, converting inches to points (using 72 dpi)
70
        $pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);
71
        $pdf->SetAutoPageBreak(true, $printMargins->getBottom() * 72);
72
73
        $pdf->setPrintHeader(false);
74
        $pdf->setPrintFooter(false);
75
76
        $pdf->AddPage();
77
78
        //  Set the appropriate font
79
        $pdf->SetFont($this->getFont());
80
        $pdf->writeHTML(
81
            $this->generateHTMLHeader(false) .
82
            $this->generateSheetData() .
83
            $this->generateHTMLFooter()
84
        );
85
86
        //  Document info
87
        $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
88
        $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
89
        $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
90
        $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
91
        $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
92
93
        //  Write to file
94
        fwrite($fileHandle, $pdf->output($pFilename, 'S'));
95
96
        parent::restoreStateAfterSave($fileHandle);
97
    }
98
}
99