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 |
|
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->AddPageByArray([ |
74
|
1 |
|
'orientation' => $orientation, |
75
|
1 |
|
'margin-left' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getLeft()), |
76
|
1 |
|
'margin-right' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getRight()), |
77
|
1 |
|
'margin-top' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getTop()), |
78
|
1 |
|
'margin-bottom' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getBottom()), |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
// Document info |
82
|
1 |
|
$pdf->SetTitle($this->spreadsheet->getProperties()->getTitle()); |
83
|
1 |
|
$pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator()); |
84
|
1 |
|
$pdf->SetSubject($this->spreadsheet->getProperties()->getSubject()); |
85
|
1 |
|
$pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords()); |
86
|
1 |
|
$pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); |
87
|
|
|
|
88
|
1 |
|
$pdf->WriteHTML($this->generateHTMLHeader(false)); |
89
|
1 |
|
$html = $this->generateSheetData(); |
90
|
1 |
|
foreach (\array_chunk(\explode(PHP_EOL, $html), 1000) as $lines) { |
91
|
1 |
|
$pdf->WriteHTML(\implode(PHP_EOL, $lines)); |
92
|
|
|
} |
93
|
1 |
|
$pdf->WriteHTML($this->generateHTMLFooter()); |
94
|
|
|
|
95
|
|
|
// Write to file |
96
|
1 |
|
fwrite($fileHandle, $pdf->Output('', 'S')); |
97
|
|
|
|
98
|
1 |
|
parent::restoreStateAfterSave($fileHandle); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Convert inches to mm. |
103
|
|
|
* |
104
|
|
|
* @param float $inches |
105
|
|
|
* |
106
|
|
|
* @return float |
107
|
|
|
*/ |
108
|
1 |
|
private function inchesToMm($inches) |
109
|
|
|
{ |
110
|
1 |
|
return $inches * 25.4; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|