1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace PhpSpreadsheet\Writer\PDF; |
4
|
|
|
|
5
|
|
|
/* Require DomPDF library */ |
6
|
|
|
$pdfRendererClassFile = \PhpSpreadsheet\Settings::getPdfRendererPath() . '/dompdf_config.inc.php'; |
7
|
|
|
if (file_exists($pdfRendererClassFile)) { |
8
|
|
|
require_once $pdfRendererClassFile; |
9
|
|
|
} else { |
10
|
|
|
throw new \PhpSpreadsheet\Writer\Exception('Unable to load PDF Rendering library'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Copyright (c) 2006 - 2015 PhpSpreadsheet |
15
|
|
|
* |
16
|
|
|
* This library is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU Lesser General Public |
18
|
|
|
* License as published by the Free Software Foundation; either |
19
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This library is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24
|
|
|
* Lesser General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Lesser General Public |
27
|
|
|
* License along with this library; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
29
|
|
|
* |
30
|
|
|
* @category PhpSpreadsheet |
31
|
|
|
* @copyright Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
32
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
33
|
|
|
* @version ##VERSION##, ##DATE## |
34
|
|
|
*/ |
35
|
|
|
class DomPDF extends Core implements \PhpSpreadsheet\Writer\IWriter |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Create a new DomPDF Writer instance |
39
|
|
|
* |
40
|
|
|
* @param \PhpSpreadsheet\Spreadsheet $spreadsheet Spreadsheet object |
41
|
|
|
*/ |
42
|
|
|
public function __construct(\PhpSpreadsheet\Spreadsheet $spreadsheet) |
43
|
|
|
{ |
44
|
|
|
parent::__construct($spreadsheet); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Save Spreadsheet to file |
49
|
|
|
* |
50
|
|
|
* @param string $pFilename Name of the file to save as |
51
|
|
|
* @throws \PhpSpreadsheet\Writer\Exception |
52
|
|
|
*/ |
53
|
|
|
public function save($pFilename = null) |
54
|
|
|
{ |
55
|
|
|
$fileHandle = parent::prepareForSave($pFilename); |
|
|
|
|
56
|
|
|
|
57
|
|
|
// Default PDF paper size |
58
|
|
|
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.) |
59
|
|
|
|
60
|
|
|
// Check for paper size and page orientation |
61
|
|
View Code Duplication |
if (is_null($this->getSheetIndex())) { |
|
|
|
|
62
|
|
|
$orientation = ($this->phpSpreadsheet->getSheet(0)->getPageSetup()->getOrientation() |
63
|
|
|
== \PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
64
|
|
|
$printPaperSize = $this->phpSpreadsheet->getSheet(0)->getPageSetup()->getPaperSize(); |
65
|
|
|
$printMargins = $this->phpSpreadsheet->getSheet(0)->getPageMargins(); |
|
|
|
|
66
|
|
|
} else { |
67
|
|
|
$orientation = ($this->phpSpreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() |
68
|
|
|
== \PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
69
|
|
|
$printPaperSize = $this->phpSpreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize(); |
70
|
|
|
$printMargins = $this->phpSpreadsheet->getSheet($this->getSheetIndex())->getPageMargins(); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$orientation = ($orientation == 'L') ? 'landscape' : 'portrait'; |
74
|
|
|
|
75
|
|
|
// Override Page Orientation |
76
|
|
View Code Duplication |
if (!is_null($this->getOrientation())) { |
|
|
|
|
77
|
|
|
$orientation = ($this->getOrientation() == \PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_DEFAULT) |
78
|
|
|
? \PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_PORTRAIT |
79
|
|
|
: $this->getOrientation(); |
80
|
|
|
} |
81
|
|
|
// Override Paper Size |
82
|
|
|
if (!is_null($this->getPaperSize())) { |
83
|
|
|
$printPaperSize = $this->getPaperSize(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (isset(self::$paperSizes[$printPaperSize])) { |
87
|
|
|
$paperSize = self::$paperSizes[$printPaperSize]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Create PDF |
91
|
|
|
$pdf = new self(); |
92
|
|
|
$pdf->set_paper(strtolower($paperSize), $orientation); |
93
|
|
|
|
94
|
|
|
$pdf->load_html( |
95
|
|
|
$this->generateHTMLHeader(false) . |
96
|
|
|
$this->generateSheetData() . |
97
|
|
|
$this->generateHTMLFooter() |
98
|
|
|
); |
99
|
|
|
$pdf->render(); |
100
|
|
|
|
101
|
|
|
// Write to file |
102
|
|
|
fwrite($fileHandle, $pdf->output()); |
103
|
|
|
|
104
|
|
|
parent::restoreStateAfterSave($fileHandle); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.