File Source for PDF.php

PHPExcel_Writer

# Source for file PDF.php

Documentation is available at PDF.php

1. <?php
2. /**
3. * PHPExcel
4. *
5. * Copyright (c) 2006 - 2011 PHPExcel
6. *
7. * This library is free software; you can redistribute it and/or
8. * modify it under the terms of the GNU Lesser General Public
9. * License as published by the Free Software Foundation; either
10. * version 2.1 of the License, or (at your option) any later version.
11. *
12. * This library is distributed in the hope that it will be useful,
13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15. * Lesser General Public License for more details.
16. *
17. * You should have received a copy of the GNU Lesser General Public
18. * License along with this library; if not, write to the Free Software
19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20. *
21. * @category PHPExcel
22. * @package PHPExcel_Writer
23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25. * @version 1.7.6, 2011-02-27
26. */
29. /** Require FPDF library */
30. $k_path_url = dirname(__FILE__) . '/PDF';
31. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PDF/tcpdf.php';
33. /**
34. * PHPExcel_Writer_PDF
35. *
36. * @category PHPExcel
37. * @package PHPExcel_Writer
38. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
39. */
40. class PHPExcel_Writer_PDF extends PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter {
41. /**
42. * Temporary storage directory
43. *
44. * @var string
45. */
46. private $_tempDir = '';
48. /**
49. * Font
50. *
51. * @var string
52. */
53. private $_font = 'freesans';
55. /**
56. * Orientation (Over-ride)
57. *
58. * @var string
59. */
60. private $_orientation = null;
62. /**
63. * Paper size (Over-ride)
64. *
65. * @var int
66. */
67. private $_paperSize = null;
70. /**
71. * Paper Sizes xRef List
72. *
73. * @var array
74. */
75. private static $_paperSizes = array(
76. // Excel Paper Size TCPDF Paper Size
77. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LETTER => 'LETTER', // (8.5 in. by 11 in.)
78. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LETTER_SMALL => 'LETTER', // (8.5 in. by 11 in.)
79. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_TABLOID => array(792.00,1224.00), // (11 in. by 17 in.)
80. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LEDGER => array(1224.00,792.00), // (17 in. by 11 in.)
81. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LEGAL => 'LEGAL', // (8.5 in. by 14 in.)
82. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_STATEMENT => array(396.00,612.00), // (5.5 in. by 8.5 in.)
83. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_EXECUTIVE => 'EXECUTIVE', // (7.25 in. by 10.5 in.)
84. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A3 => 'A3', // (297 mm by 420 mm)
85. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A4 => 'A4', // (210 mm by 297 mm)
86. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A4_SMALL => 'A4', // (210 mm by 297 mm)
87. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A5 => 'A5', // (148 mm by 210 mm)
88. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_B4 => 'B4', // (250 mm by 353 mm)
89. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_B5 => 'B5', // (176 mm by 250 mm)
90. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_FOLIO => 'FOLIO', // (8.5 in. by 13 in.)
91. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_QUARTO => array(609.45,779.53), // (215 mm by 275 mm)
92. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_STANDARD_1 => array(720.00,1008.00), // (10 in. by 14 in.)
93. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_STANDARD_2 => array(792.00,1224.00), // (11 in. by 17 in.)
94. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_NOTE => 'LETTER', // (8.5 in. by 11 in.)
95. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_NO9_ENVELOPE => array(279.00,639.00), // (3.875 in. by 8.875 in.)
96. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_NO10_ENVELOPE => array(297.00,684.00), // (4.125 in. by 9.5 in.)
97. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_NO11_ENVELOPE => array(324.00,747.00), // (4.5 in. by 10.375 in.)
98. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_NO12_ENVELOPE => array(342.00,792.00), // (4.75 in. by 11 in.)
99. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_NO14_ENVELOPE => array(360.00,828.00), // (5 in. by 11.5 in.)
100. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_C => array(1224.00,1584.00), // (17 in. by 22 in.)
101. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_D => array(1584.00,2448.00), // (22 in. by 34 in.)
102. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_E => array(2448.00,3168.00), // (34 in. by 44 in.)
103. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_DL_ENVELOPE => array(311.81,623.62), // (110 mm by 220 mm)
104. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_C5_ENVELOPE => 'C5', // (162 mm by 229 mm)
105. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_C3_ENVELOPE => 'C3', // (324 mm by 458 mm)
106. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_C4_ENVELOPE => 'C4', // (229 mm by 324 mm)
107. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_C6_ENVELOPE => 'C6', // (114 mm by 162 mm)
108. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_C65_ENVELOPE => array(323.15,649.13), // (114 mm by 229 mm)
109. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_B4_ENVELOPE => 'B4', // (250 mm by 353 mm)
110. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_B5_ENVELOPE => 'B5', // (176 mm by 250 mm)
111. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_B6_ENVELOPE => array(498.90,354.33), // (176 mm by 125 mm)
112. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_ITALY_ENVELOPE => array(311.81,651.97), // (110 mm by 230 mm)
113. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_MONARCH_ENVELOPE => array(279.00,540.00), // (3.875 in. by 7.5 in.)
114. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_6_3_4_ENVELOPE => array(261.00,468.00), // (3.625 in. by 6.5 in.)
115. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_US_STANDARD_FANFOLD => array(1071.00,792.00), // (14.875 in. by 11 in.)
116. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_GERMAN_STANDARD_FANFOLD => array(612.00,864.00), // (8.5 in. by 12 in.)
117. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_GERMAN_LEGAL_FANFOLD => 'FOLIO', // (8.5 in. by 13 in.)
118. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_ISO_B4 => 'B4', // (250 mm by 353 mm)
119. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_JAPANESE_DOUBLE_POSTCARD => array(566.93,419.53), // (200 mm by 148 mm)
120. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_STANDARD_PAPER_1 => array(648.00,792.00), // (9 in. by 11 in.)
121. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_STANDARD_PAPER_2 => array(720.00,792.00), // (10 in. by 11 in.)
122. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_STANDARD_PAPER_3 => array(1080.00,792.00), // (15 in. by 11 in.)
123. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_INVITE_ENVELOPE => array(623.62,623.62), // (220 mm by 220 mm)
124. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LETTER_EXTRA_PAPER => array(667.80,864.00), // (9.275 in. by 12 in.)
125. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LEGAL_EXTRA_PAPER => array(667.80,1080.00), // (9.275 in. by 15 in.)
126. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_TABLOID_EXTRA_PAPER => array(841.68,1296.00), // (11.69 in. by 18 in.)
127. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A4_EXTRA_PAPER => array(668.98,912.76), // (236 mm by 322 mm)
128. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LETTER_TRANSVERSE_PAPER => array(595.80,792.00), // (8.275 in. by 11 in.)
129. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A4_TRANSVERSE_PAPER => 'A4', // (210 mm by 297 mm)
130. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER => array(667.80,864.00), // (9.275 in. by 12 in.)
131. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_SUPERA_SUPERA_A4_PAPER => array(643.46,1009.13), // (227 mm by 356 mm)
132. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_SUPERB_SUPERB_A3_PAPER => array(864.57,1380.47), // (305 mm by 487 mm)
133. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LETTER_PLUS_PAPER => array(612.00,913.68), // (8.5 in. by 12.69 in.)
134. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A4_PLUS_PAPER => array(595.28,935.43), // (210 mm by 330 mm)
135. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A5_TRANSVERSE_PAPER => 'A5', // (148 mm by 210 mm)
136. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_JIS_B5_TRANSVERSE_PAPER => array(515.91,728.50), // (182 mm by 257 mm)
137. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A3_EXTRA_PAPER => array(912.76,1261.42), // (322 mm by 445 mm)
138. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A5_EXTRA_PAPER => array(493.23,666.14), // (174 mm by 235 mm)
139. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_ISO_B5_EXTRA_PAPER => array(569.76,782.36), // (201 mm by 276 mm)
140. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A2_PAPER => 'A2', // (420 mm by 594 mm)
141. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A3_TRANSVERSE_PAPER => 'A3', // (297 mm by 420 mm)
142. PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER => array(912.76,1261.42) // (322 mm by 445 mm)
143. );
145. /**
146. * Create a new PHPExcel_Writer_PDF
147. *
148. * @param PHPExcel $phpExcel PHPExcel object
149. */
150. public function __construct(PHPExcel $phpExcel) {
151. parent::__construct($phpExcel);
152. $this-> setUseInlineCss(true);
153. $this->_tempDir = PHPExcel_Shared_File:: sys_get_temp_dir();
154. }
156. /**
157. * Set font. Examples:
158. * 'arialunicid0-chinese-simplified'
159. * 'arialunicid0-chinese-traditional'
160. * 'arialunicid0-korean'
161. * 'arialunicid0-japanese'
162. *
163. * @param string $fontName
164. */
165. public function setFont($fontName) {
166. $this->_font = $fontName;
167. return $this;
168. }
170. /**
171. * Get Paper Size
172. *
173. * @return int
174. */
175. public function getPaperSize() {
176. return $this->_paperSize;
177. }
179. /**
180. * Set Paper Size
181. *
182. * @param int $pValue
183. * @return PHPExcel_Writer_PDF
184. */
185. public function setPaperSize($pValue = PHPExcel_Worksheet_PageSetup:: PAPERSIZE_LETTER) {
186. $this->_paperSize = $pValue;
187. return $this;
188. }
190. /**
191. * Get Orientation
192. *
193. * @return string
194. */
195. public function getOrientation() {
196. return $this->_orientation;
197. }
199. /**
200. * Set Orientation
201. *
202. * @param string $pValue
203. * @return PHPExcel_Writer_PDF
204. */
205. public function setOrientation($pValue = PHPExcel_Worksheet_PageSetup:: ORIENTATION_DEFAULT) {
206. $this->_orientation = $pValue;
207. return $this;
208. }
210. /**
211. * Save PHPExcel to file
212. *
213. * @param string $pFileName
214. * @throws Exception
215. */
216. public function save($pFilename = null) {
217. // garbage collect
218. $this->_phpExcel->garbageCollect();
220. $saveArrayReturnType = PHPExcel_Calculation:: getArrayReturnType();
221. PHPExcel_Calculation:: setArrayReturnType( PHPExcel_Calculation:: RETURN_ARRAY_AS_VALUE);
223. // Open file
224. $fileHandle = fopen($pFilename, 'w');
225. if ($fileHandle === false) {
226. throw new Exception("Could not open file $pFilename for writing.");
227. }
229. // Set PDF
230. $this->_isPdf = true;
232. // Build CSS
233. $this-> buildCSS(true);
235. // Generate HTML
236. $html = '';
237. //$html .= $this->generateHTMLHeader(false);
238. $html .= $this-> generateSheetData();
239. //$html .= $this->generateHTMLFooter();
241. // Default PDF paper size
242. $paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
244. // Check for paper size and page orientation
245. if ( is_null($this-> getSheetIndex())) {
246. $orientation = ($this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup:: ORIENTATION_LANDSCAPE) ? 'L' : 'P';
247. $printPaperSize = $this->_phpExcel->getSheet(0)->getPageSetup()->getPaperSize();
248. $printMargins = $this->_phpExcel->getSheet(0)->getPageMargins();
249. } else {
250. $orientation = ($this->_phpExcel->getSheet($this-> getSheetIndex())->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup:: ORIENTATION_LANDSCAPE) ? 'L' : 'P';
251. $printPaperSize = $this->_phpExcel->getSheet($this-> getSheetIndex())->getPageSetup()->getPaperSize();
252. $printMargins = $this->_phpExcel->getSheet($this-> getSheetIndex())->getPageMargins();
253. }
255. // Override Page Orientation
256. if (! is_null($this->_orientation)) {
257. $orientation = ($this->_orientation == PHPExcel_Worksheet_PageSetup:: ORIENTATION_LANDSCAPE) ? 'L' : 'P';
258. }
259. // Override Paper Size
260. if (! is_null($this->_paperSize)) {
261. $printPaperSize = $this->_paperSize;
262. }
265. if (isset(self::$_paperSizes[$printPaperSize])) {
266. $paperSize = self::$_paperSizes[$printPaperSize];
267. }
270. // Create PDF
271. $pdf = new TCPDF($orientation, 'pt', $paperSize);
272. $pdf->setFontSubsetting(false);
273. // Set margins, converting inches to points (using 72 dpi)
274. $pdf->SetMargins($printMargins->getLeft() * 72,$printMargins->getTop() * 72,$printMargins->getRight() * 72);
275. $pdf->SetAutoPageBreak(true,$printMargins->getBottom() * 72);
276. // $pdf->setHeaderMargin($printMargins->getHeader() * 72);
277. // $pdf->setFooterMargin($printMargins->getFooter() * 72);
279. $pdf->setPrintHeader(false);
280. $pdf->setPrintFooter(false);
282. $pdf->AddPage();
284. // Set the appropriate font
285. $pdf->SetFont($this->_font);
286. $pdf->writeHTML($html);
288. // Document info
289. $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
290. $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
291. $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
292. $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
293. $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
295. // Write to file
296. fwrite($fileHandle, $pdf->output($pFilename, 'S'));
298. // Close file
299. fclose($fileHandle);
301. PHPExcel_Calculation:: setArrayReturnType($saveArrayReturnType);
302. }
304. /**
305. * Get temporary storage directory
306. *
307. * @return string
308. */
309. public function getTempDir() {
310. return $this->_tempDir;
311. }
313. /**
314. * Set temporary storage directory
315. *
316. * @param string $pValue Temporary storage directory
317. * @throws Exception Exception when directory does not exist
318. * @return PHPExcel_Writer_PDF
319. */
320. public function setTempDir($pValue = '') {
321. if ( is_dir($pValue)) {
322. $this->_tempDir = $pValue;
323. } else {
324. throw new Exception("Directory does not exist: $pValue");
325. }
326. return $this;
327. }
328. }

Documentation generated on Sun, 27 Feb 2011 16:33:21 -0800 by phpDocumentor 1.4.3