1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\IWriter; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf; |
8
|
|
|
|
9
|
|
|
class Dompdf extends Pdf implements IWriter |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Save Spreadsheet to file. |
13
|
|
|
* |
14
|
|
|
* @param string $pFilename Name of the file to save as |
15
|
|
|
* |
16
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
17
|
|
|
*/ |
18
|
2 |
|
public function save($pFilename) |
19
|
|
|
{ |
20
|
2 |
|
$fileHandle = parent::prepareForSave($pFilename); |
|
|
|
|
21
|
|
|
|
22
|
|
|
// Default PDF paper size |
23
|
2 |
|
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.) |
24
|
|
|
|
25
|
|
|
// Check for paper size and page orientation |
26
|
2 |
|
if ($this->getSheetIndex() === null) { |
27
|
|
|
$orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation() |
28
|
|
|
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
29
|
|
|
$printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize(); |
30
|
|
|
$printMargins = $this->spreadsheet->getSheet(0)->getPageMargins(); |
|
|
|
|
31
|
|
|
} else { |
32
|
2 |
|
$orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() |
33
|
2 |
|
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
34
|
2 |
|
$printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize(); |
35
|
2 |
|
$printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
$orientation = ($orientation == 'L') ? 'landscape' : 'portrait'; |
39
|
|
|
|
40
|
|
|
// Override Page Orientation |
41
|
2 |
|
if ($this->getOrientation() !== null) { |
42
|
|
|
$orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT) |
43
|
|
|
? PageSetup::ORIENTATION_PORTRAIT |
44
|
|
|
: $this->getOrientation(); |
45
|
|
|
} |
46
|
|
|
// Override Paper Size |
47
|
2 |
|
if ($this->getPaperSize() !== null) { |
48
|
|
|
$printPaperSize = $this->getPaperSize(); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
if (isset(self::$paperSizes[$printPaperSize])) { |
52
|
2 |
|
$paperSize = self::$paperSizes[$printPaperSize]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Create PDF |
56
|
2 |
|
$pdf = new \Dompdf\Dompdf(); |
57
|
2 |
|
$pdf->setPaper(strtolower($paperSize), $orientation); |
58
|
|
|
|
59
|
2 |
|
$pdf->loadHtml( |
60
|
2 |
|
$this->generateHTMLHeader(false) . |
61
|
2 |
|
$this->generateSheetData() . |
62
|
2 |
|
$this->generateHTMLFooter() |
63
|
|
|
); |
64
|
2 |
|
$pdf->render(); |
65
|
|
|
|
66
|
|
|
// Write to file |
67
|
2 |
|
fwrite($fileHandle, $pdf->output()); |
68
|
|
|
|
69
|
2 |
|
parent::restoreStateAfterSave($fileHandle); |
|
|
|
|
70
|
2 |
|
} |
71
|
|
|
} |
72
|
|
|
|
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.