1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\IWriter; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf; |
9
|
|
|
|
10
|
|
|
class Mpdf extends Pdf implements IWriter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Save Spreadsheet to file. |
14
|
|
|
* |
15
|
|
|
* @param string $pFilename Name of the file to save as |
16
|
|
|
* |
17
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
18
|
|
|
* @throws PhpSpreadsheetException |
19
|
|
|
*/ |
20
|
1 |
|
public function save($pFilename) |
21
|
|
|
{ |
22
|
1 |
|
$fileHandle = parent::prepareForSave($pFilename); |
|
|
|
|
23
|
|
|
|
24
|
|
|
// Default PDF paper size |
25
|
1 |
|
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.) |
26
|
|
|
|
27
|
|
|
// Check for paper size and page orientation |
28
|
1 |
|
if (null === $this->getSheetIndex()) { |
29
|
|
|
$orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation() |
30
|
|
|
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
31
|
|
|
$printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize(); |
32
|
|
|
$printMargins = $this->spreadsheet->getSheet(0)->getPageMargins(); |
|
|
|
|
33
|
|
|
} else { |
34
|
1 |
|
$orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() |
35
|
1 |
|
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
36
|
1 |
|
$printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize(); |
37
|
1 |
|
$printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins(); |
|
|
|
|
38
|
|
|
} |
39
|
1 |
|
$this->setOrientation($orientation); |
40
|
|
|
|
41
|
|
|
// Override Page Orientation |
42
|
1 |
|
if (null !== $this->getOrientation()) { |
43
|
1 |
|
$orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT) |
44
|
|
|
? PageSetup::ORIENTATION_PORTRAIT |
45
|
1 |
|
: $this->getOrientation(); |
46
|
|
|
} |
47
|
1 |
|
$orientation = strtoupper($orientation); |
48
|
|
|
|
49
|
|
|
// Override Paper Size |
50
|
1 |
|
if (null !== $this->getPaperSize()) { |
51
|
|
|
$printPaperSize = $this->getPaperSize(); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
if (isset(self::$paperSizes[$printPaperSize])) { |
55
|
1 |
|
$paperSize = self::$paperSizes[$printPaperSize]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Create PDF |
59
|
1 |
|
$config = ['tempDir' => $this->tempDir]; |
60
|
1 |
|
$pdf = new \Mpdf\Mpdf($config); |
61
|
1 |
|
$ortmp = $orientation; |
62
|
1 |
|
$pdf->_setPageSize(strtoupper($paperSize), $ortmp); |
63
|
1 |
|
$pdf->DefOrientation = $orientation; |
64
|
1 |
|
$pdf->AddPage($orientation); |
65
|
|
|
|
66
|
|
|
// Document info |
67
|
1 |
|
$pdf->SetTitle($this->spreadsheet->getProperties()->getTitle()); |
68
|
1 |
|
$pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator()); |
69
|
1 |
|
$pdf->SetSubject($this->spreadsheet->getProperties()->getSubject()); |
70
|
1 |
|
$pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords()); |
71
|
1 |
|
$pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); |
72
|
|
|
|
73
|
1 |
|
$pdf->WriteHTML( |
74
|
1 |
|
$this->generateHTMLHeader(false) . |
75
|
1 |
|
$this->generateSheetData() . |
76
|
1 |
|
$this->generateHTMLFooter() |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
// Write to file |
80
|
1 |
|
fwrite($fileHandle, $pdf->Output('', 'S')); |
81
|
|
|
|
82
|
1 |
|
parent::restoreStateAfterSave($fileHandle); |
|
|
|
|
83
|
1 |
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.