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 Tcpdf 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
|
1 |
|
public function save($pFilename) |
19
|
|
|
{ |
20
|
1 |
|
$fileHandle = parent::prepareForSave($pFilename); |
|
|
|
|
21
|
|
|
|
22
|
|
|
// Default PDF paper size |
23
|
1 |
|
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.) |
24
|
|
|
|
25
|
|
|
// Check for paper size and page orientation |
26
|
1 |
|
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
|
1 |
|
$orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() |
33
|
1 |
|
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
34
|
1 |
|
$printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize(); |
35
|
1 |
|
$printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Override Page Orientation |
39
|
1 |
|
if ($this->getOrientation() !== null) { |
40
|
|
|
$orientation = ($this->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE) |
41
|
|
|
? 'L' |
42
|
|
|
: 'P'; |
43
|
|
|
} |
44
|
|
|
// Override Paper Size |
45
|
1 |
|
if ($this->getPaperSize() !== null) { |
46
|
|
|
$printPaperSize = $this->getPaperSize(); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
if (isset(self::$paperSizes[$printPaperSize])) { |
50
|
1 |
|
$paperSize = self::$paperSizes[$printPaperSize]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Create PDF |
54
|
1 |
|
$pdf = new \TCPDF($orientation, 'pt', $paperSize); |
55
|
1 |
|
$pdf->setFontSubsetting(false); |
56
|
|
|
// Set margins, converting inches to points (using 72 dpi) |
57
|
1 |
|
$pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72); |
58
|
1 |
|
$pdf->SetAutoPageBreak(true, $printMargins->getBottom() * 72); |
59
|
|
|
|
60
|
1 |
|
$pdf->setPrintHeader(false); |
61
|
1 |
|
$pdf->setPrintFooter(false); |
62
|
|
|
|
63
|
1 |
|
$pdf->AddPage(); |
64
|
|
|
|
65
|
|
|
// Set the appropriate font |
66
|
1 |
|
$pdf->SetFont($this->getFont()); |
67
|
1 |
|
$pdf->writeHTML( |
68
|
1 |
|
$this->generateHTMLHeader(false) . |
69
|
1 |
|
$this->generateSheetData() . |
70
|
1 |
|
$this->generateHTMLFooter() |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
// Document info |
74
|
1 |
|
$pdf->SetTitle($this->spreadsheet->getProperties()->getTitle()); |
75
|
1 |
|
$pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator()); |
76
|
1 |
|
$pdf->SetSubject($this->spreadsheet->getProperties()->getSubject()); |
77
|
1 |
|
$pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords()); |
78
|
1 |
|
$pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); |
79
|
|
|
|
80
|
|
|
// Write to file |
81
|
1 |
|
fwrite($fileHandle, $pdf->output($pFilename, 'S')); |
82
|
|
|
|
83
|
1 |
|
parent::restoreStateAfterSave($fileHandle); |
|
|
|
|
84
|
1 |
|
} |
85
|
|
|
} |
86
|
|
|
|
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.