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\Pdf; |
8
|
|
|
|
9
|
|
|
class Mpdf extends Pdf |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Gets the implementation of external PDF library that should be used. |
13
|
|
|
* |
14
|
|
|
* @param array $config Configuration array |
15
|
|
|
* |
16
|
|
|
* @return \Mpdf\Mpdf implementation |
17
|
|
|
*/ |
18
|
1 |
|
protected function createExternalWriterInstance($config) |
19
|
|
|
{ |
20
|
1 |
|
return new \Mpdf\Mpdf($config); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Save Spreadsheet to file. |
25
|
|
|
* |
26
|
|
|
* @param string $pFilename Name of the file to save as |
27
|
|
|
* |
28
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
29
|
|
|
* @throws PhpSpreadsheetException |
30
|
|
|
*/ |
31
|
1 |
|
public function save($pFilename) |
32
|
|
|
{ |
33
|
1 |
|
$fileHandle = parent::prepareForSave($pFilename); |
34
|
|
|
|
35
|
|
|
// Default PDF paper size |
36
|
1 |
|
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.) |
37
|
|
|
|
38
|
|
|
// Check for paper size and page orientation |
39
|
1 |
View Code Duplication |
if (null === $this->getSheetIndex()) { |
|
|
|
|
40
|
|
|
$orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation() |
41
|
|
|
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
42
|
|
|
$printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize(); |
43
|
|
|
} else { |
44
|
1 |
|
$orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() |
45
|
1 |
|
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
46
|
1 |
|
$printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize(); |
47
|
|
|
} |
48
|
1 |
|
$this->setOrientation($orientation); |
49
|
|
|
|
50
|
|
|
// Override Page Orientation |
51
|
1 |
|
if (null !== $this->getOrientation()) { |
52
|
1 |
|
$orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT) |
53
|
|
|
? PageSetup::ORIENTATION_PORTRAIT |
54
|
1 |
|
: $this->getOrientation(); |
55
|
|
|
} |
56
|
1 |
|
$orientation = strtoupper($orientation); |
57
|
|
|
|
58
|
|
|
// Override Paper Size |
59
|
1 |
|
if (null !== $this->getPaperSize()) { |
60
|
|
|
$printPaperSize = $this->getPaperSize(); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
if (isset(self::$paperSizes[$printPaperSize])) { |
64
|
1 |
|
$paperSize = self::$paperSizes[$printPaperSize]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Create PDF |
68
|
1 |
|
$config = ['tempDir' => $this->tempDir]; |
69
|
1 |
|
$pdf = $this->createExternalWriterInstance($config); |
70
|
1 |
|
$ortmp = $orientation; |
71
|
1 |
|
$pdf->_setPageSize(strtoupper($paperSize), $ortmp); |
72
|
1 |
|
$pdf->DefOrientation = $orientation; |
73
|
1 |
|
$pdf->AddPage($orientation); |
74
|
|
|
|
75
|
|
|
// Document info |
76
|
1 |
|
$pdf->SetTitle($this->spreadsheet->getProperties()->getTitle()); |
77
|
1 |
|
$pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator()); |
78
|
1 |
|
$pdf->SetSubject($this->spreadsheet->getProperties()->getSubject()); |
79
|
1 |
|
$pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords()); |
80
|
1 |
|
$pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); |
81
|
|
|
|
82
|
1 |
|
$pdf->WriteHTML( |
83
|
1 |
|
$this->generateHTMLHeader(false) . |
84
|
1 |
|
$this->generateSheetData() . |
85
|
1 |
|
$this->generateHTMLFooter() |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
// Write to file |
89
|
1 |
|
fwrite($fileHandle, $pdf->Output('', 'S')); |
90
|
|
|
|
91
|
1 |
|
parent::restoreStateAfterSave($fileHandle); |
92
|
1 |
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.