PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/public/assets/javascript/gelSheet/server/export/PHPExcel/Classes/PHPExcel/Writer/PDF.php

https://github.com/libersoft/fengoffice-ls
PHP | 461 lines | 276 code | 52 blank | 133 comment | 52 complexity | 470d71df59e65f0a6dcf10c3e14d72ff MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.6.3, 2008-08-25
  26. */
  27. /**
  28. * PHPExcel_Writer_PDF
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer
  32. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_PDF implements PHPExcel_Writer_IWriter {
  35. /**
  36. * PHPExcel object
  37. *
  38. * @var PHPExcel
  39. */
  40. private $_phpExcel;
  41. /**
  42. * Sheet index to write
  43. *
  44. * @var int
  45. */
  46. private $_sheetIndex;
  47. /**
  48. * Pre-calculate formulas
  49. *
  50. * @var boolean
  51. */
  52. private $_preCalculateFormulas = true;
  53. /**
  54. * Temporary storage directory
  55. *
  56. * @var string
  57. */
  58. private $_tempDir = '';
  59. /**
  60. * Create a new PHPExcel_Writer_PDF
  61. *
  62. * @param PHPExcel $phpExcel PHPExcel object
  63. */
  64. public function __construct(PHPExcel $phpExcel) {
  65. $this->_phpExcel = $phpExcel;
  66. $this->_sheetIndex = 0;
  67. $this->_tempDir = sys_get_temp_dir();
  68. }
  69. /**
  70. * Save PHPExcel to file
  71. *
  72. * @param string $pFileName
  73. * @throws Exception
  74. */
  75. public function save($pFilename = null) {
  76. // Open file
  77. global $cnf;
  78. $pFilename= $cnf['path']['Temp'] . $pFilename;
  79. $fileHandle = fopen($pFilename, 'w');
  80. if ($fileHandle === false) {
  81. throw new Exception("Could not open file $pFilename for writing.");
  82. }
  83. // Fetch sheets
  84. $sheets = array();
  85. if (is_null($this->_sheetIndex)) {
  86. $sheets = $this->_phpExcel->getAllSheets();
  87. } else {
  88. $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
  89. }
  90. // PDF paper size
  91. $paperSize = 'A4';
  92. // Create PDF
  93. $pdf = new FPDF('P', 'pt', $paperSize);
  94. // Loop all sheets
  95. foreach ($sheets as $sheet) {
  96. // PDF orientation
  97. $orientation = 'P';
  98. if ($sheet->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) {
  99. $orientation = 'L';
  100. }
  101. // Start sheet
  102. $pdf->SetAutoPageBreak(true);
  103. $pdf->SetFont('Arial', '', 10);
  104. $pdf->AddPage($orientation);
  105. // Get worksheet dimension
  106. $dimension = explode(':', $sheet->calculateWorksheetDimension());
  107. $dimension[0] = PHPExcel_Cell::coordinateFromString($dimension[0]);
  108. $dimension[0][0] = PHPExcel_Cell::columnIndexFromString($dimension[0][0]) - 1;
  109. $dimension[1] = PHPExcel_Cell::coordinateFromString($dimension[1]);
  110. $dimension[1][0] = PHPExcel_Cell::columnIndexFromString($dimension[1][0]) - 1;
  111. // Calculate column widths
  112. $sheet->calculateColumnWidths();
  113. // Loop trough cells
  114. for ($row = $dimension[0][1]; $row <= $dimension[1][1]; $row++) {
  115. // Line height
  116. $lineHeight = 0;
  117. // Calulate line height
  118. for ($column = $dimension[0][0]; $column <= $dimension[1][0]; $column++) {
  119. $rowDimension = $sheet->getRowDimension($row);
  120. $cellHeight = PHPExcel_Shared_Drawing::pixelsToPoints(
  121. PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight())
  122. );
  123. if ($cellHeight <= 0) {
  124. $cellHeight = PHPExcel_Shared_Drawing::pixelsToPoints(
  125. PHPExcel_Shared_Drawing::cellDimensionToPixels($sheet->getDefaultRowDimension()->getRowHeight())
  126. );
  127. }
  128. if ($cellHeight <= 0) {
  129. $cellHeight = $sheet->getStyleByColumnAndRow($column, $row)->getFont()->getSize();
  130. }
  131. if ($cellHeight > $lineHeight) {
  132. $lineHeight = $cellHeight;
  133. }
  134. }
  135. // Output values
  136. for ($column = $dimension[0][0]; $column <= $dimension[1][0]; $column++) {
  137. // Start with defaults...
  138. $pdf->SetFont('Arial', '', 10);
  139. $pdf->SetTextColor(0, 0, 0);
  140. $pdf->SetDrawColor(100, 100, 100);
  141. $pdf->SetFillColor(255, 255, 255);
  142. // Coordinates
  143. $startX = $pdf->GetX();
  144. $startY = $pdf->GetY();
  145. // Cell exists?
  146. $cellData = '';
  147. if ($sheet->cellExistsByColumnAndRow($column, $row)) {
  148. if ($sheet->getCellByColumnAndRow($column, $row)->getValue() instanceof PHPExcel_RichText) {
  149. $cellData = $sheet->getCellByColumnAndRow($column, $row)->getValue()->getPlainText();
  150. } else {
  151. if ($this->_preCalculateFormulas) {
  152. $cellData = PHPExcel_Style_NumberFormat::ToFormattedString(
  153. $sheet->getCellByColumnAndRow($column, $row)->getCalculatedValue(),
  154. $sheet->getstyle( $sheet->getCellByColumnAndRow($column, $row)->getCoordinate() )->getNumberFormat()->getFormatCode()
  155. );
  156. } else {
  157. $cellData = PHPExcel_Style_NumberFormat::ToFormattedString(
  158. $sheet->getCellByColumnAndRow($column, $row)->getValue(),
  159. $sheet->getstyle( $sheet->getCellByColumnAndRow($column, $row)->getCoordinate() )->getNumberFormat()->getFormatCode()
  160. );
  161. }
  162. }
  163. }
  164. // Style information
  165. $style = $sheet->getStyleByColumnAndRow($column, $row);
  166. // Cell width
  167. $columnDimension = $sheet->getColumnDimensionByColumn($column);
  168. if ($columnDimension->getWidth() == -1) {
  169. $columnDimension->setAutoSize(true);
  170. $sheet->calculateColumnWidths(false);
  171. }
  172. $cellWidth = PHPExcel_Shared_Drawing::pixelsToPoints(
  173. PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth())
  174. );
  175. // Cell height
  176. $rowDimension = $sheet->getRowDimension($row);
  177. $cellHeight = PHPExcel_Shared_Drawing::pixelsToPoints(
  178. PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight())
  179. );
  180. if ($cellHeight <= 0) {
  181. $cellHeight = $style->getFont()->getSize();
  182. }
  183. // Column span? Rowspan?
  184. $singleCellWidth = $cellWidth;
  185. $singleCellHeight = $cellHeight;
  186. foreach ($sheet->getMergeCells() as $cells) {
  187. if ($sheet->getCellByColumnAndRow($column, $row)->isInRange($cells)) {
  188. list($first, ) = PHPExcel_Cell::splitRange($cells);
  189. if ($first == $sheet->getCellByColumnAndRow($column, $row)->getCoordinate()) {
  190. list($colSpan, $rowSpan) = PHPExcel_Cell::rangeDimension($cells);
  191. $cellWidth = $cellWidth * $colSpan;
  192. $cellHeight = $cellHeight * $rowSpan;
  193. }
  194. break;
  195. }
  196. }
  197. // Cell height OK?
  198. if ($cellHeight < $lineHeight) {
  199. $cellHeight = $lineHeight;
  200. $singleCellHeight = $cellHeight;
  201. }
  202. // Font formatting
  203. $fontStyle = '';
  204. if ($style->getFont()->getBold()) {
  205. $fontStyle .= 'B';
  206. }
  207. if ($style->getFont()->getItalic()) {
  208. $fontStyle .= 'I';
  209. }
  210. if ($style->getFont()->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE) {
  211. $fontStyle .= 'U';
  212. }
  213. $pdf->SetFont('Arial', $fontStyle, $style->getFont()->getSize());
  214. // Text alignment
  215. $alignment = 'L';
  216. switch ($style->getAlignment()->getHorizontal()) {
  217. case PHPExcel_Style_Alignment::HORIZONTAL_CENTER:
  218. $alignment = 'C'; break;
  219. case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT:
  220. $alignment = 'R'; break;
  221. case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY:
  222. $alignment = 'J'; break;
  223. case PHPExcel_Style_Alignment::HORIZONTAL_LEFT:
  224. case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL:
  225. default:
  226. $alignment = 'L'; break;
  227. }
  228. // Text color
  229. $pdf->SetTextColor(
  230. hexdec(substr($style->getFont()->getColor()->getRGB(), 0, 2)),
  231. hexdec(substr($style->getFont()->getColor()->getRGB(), 2, 2)),
  232. hexdec(substr($style->getFont()->getColor()->getRGB(), 4, 2))
  233. );
  234. // Fill color
  235. if ($style->getFill()->getFillType() != PHPExcel_Style_Fill::FILL_NONE) {
  236. $pdf->SetFillColor(
  237. hexdec(substr($style->getFill()->getStartColor()->getRGB(), 0, 2)),
  238. hexdec(substr($style->getFill()->getStartColor()->getRGB(), 2, 2)),
  239. hexdec(substr($style->getFill()->getStartColor()->getRGB(), 4, 2))
  240. );
  241. }
  242. // Border color
  243. $borders = '';
  244. if ($style->getBorders()->getLeft()->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
  245. $borders .= 'L';
  246. $pdf->SetDrawColor(
  247. hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 0, 2)),
  248. hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 2, 2)),
  249. hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 4, 2))
  250. );
  251. }
  252. if ($style->getBorders()->getRight()->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
  253. $borders .= 'R';
  254. $pdf->SetDrawColor(
  255. hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 0, 2)),
  256. hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 2, 2)),
  257. hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 4, 2))
  258. );
  259. }
  260. if ($style->getBorders()->getTop()->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
  261. $borders .= 'T';
  262. $pdf->SetDrawColor(
  263. hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 0, 2)),
  264. hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 2, 2)),
  265. hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 4, 2))
  266. );
  267. }
  268. if ($style->getBorders()->getBottom()->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
  269. $borders .= 'B';
  270. $pdf->SetDrawColor(
  271. hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 0, 2)),
  272. hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 2, 2)),
  273. hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 4, 2))
  274. );
  275. }
  276. if ($borders == '') {
  277. $borders = 0;
  278. }
  279. if ($sheet->getShowGridlines()) {
  280. $borders = 'LTRB';
  281. }
  282. // Image?
  283. $iterator = $sheet->getDrawingCollection()->getIterator();
  284. while ($iterator->valid()) {
  285. if ($iterator->current()->getCoordinates() == PHPExcel_Cell::stringFromColumnIndex($column) . ($row + 1)) {
  286. try {
  287. $pdf->Image(
  288. $iterator->current()->getPath(),
  289. $pdf->GetX(),
  290. $pdf->GetY(),
  291. $iterator->current()->getWidth(),
  292. $iterator->current()->getHeight(),
  293. '',
  294. $this->_tempDir
  295. );
  296. } catch (Exception $ex) { }
  297. }
  298. $iterator->next();
  299. }
  300. // Print cell
  301. $pdf->MultiCell(
  302. $cellWidth,
  303. $cellHeight,
  304. $cellData,
  305. $borders,
  306. $alignment,
  307. ($style->getFill()->getFillType() == PHPExcel_Style_Fill::FILL_NONE ? 0 : 1)
  308. );
  309. // Coordinates
  310. $endX = $pdf->GetX();
  311. $endY = $pdf->GetY();
  312. // Revert to original Y location
  313. if ($endY > $startY) {
  314. $pdf->SetY($startY);
  315. if ($lineHeight < $lineHeight + ($endY - $startY)) {
  316. $lineHeight = $lineHeight + ($endY - $startY);
  317. }
  318. }
  319. $pdf->SetX($startX + $singleCellWidth);
  320. // Hyperlink?
  321. if ($sheet->getCellByColumnAndRow($column, $row)->hasHyperlink()) {
  322. if (!$sheet->getCellByColumnAndRow($column, $row)->getHyperlink()->isInternal()) {
  323. $pdf->Link(
  324. $startX,
  325. $startY,
  326. $endX - $startX,
  327. $endY - $startY,
  328. $sheet->getCellByColumnAndRow($column, $row)->getHyperlink()->getUrl()
  329. );
  330. }
  331. }
  332. }
  333. // Garbage collect!
  334. $sheet->garbageCollect();
  335. // Next line...
  336. $pdf->Ln($lineHeight);
  337. }
  338. }
  339. // Document info
  340. $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
  341. $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
  342. $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
  343. $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
  344. $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
  345. // Write to file
  346. fwrite($fileHandle, $pdf->output($pFilename, 'S'));
  347. // Close file
  348. fclose($fileHandle);
  349. }
  350. /**
  351. * Get sheet index
  352. *
  353. * @return int
  354. */
  355. public function getSheetIndex() {
  356. return $this->_sheetIndex;
  357. }
  358. /**
  359. * Set sheet index
  360. *
  361. * @param int $pValue Sheet index
  362. */
  363. public function setSheetIndex($pValue = 0) {
  364. $this->_sheetIndex = $pValue;
  365. }
  366. /**
  367. * Write all sheets (resets sheetIndex to NULL)
  368. */
  369. public function writeAllSheets() {
  370. $this->_sheetIndex = null;
  371. }
  372. /**
  373. * Get Pre-Calculate Formulas
  374. *
  375. * @return boolean
  376. */
  377. public function getPreCalculateFormulas() {
  378. return $this->_preCalculateFormulas;
  379. }
  380. /**
  381. * Set Pre-Calculate Formulas
  382. *
  383. * @param boolean $pValue Pre-Calculate Formulas?
  384. */
  385. public function setPreCalculateFormulas($pValue = true) {
  386. $this->_preCalculateFormulas = $pValue;
  387. }
  388. /**
  389. * Get temporary storage directory
  390. *
  391. * @return string
  392. */
  393. public function getTempDir() {
  394. return $this->_tempDir;
  395. }
  396. /**
  397. * Set temporary storage directory
  398. *
  399. * @param string $pValue Temporary storage directory
  400. * @throws Exception Exception when directory does not exist
  401. */
  402. public function setTempDir($pValue = '') {
  403. if (is_dir($pValue)) {
  404. $this->_tempDir = $pValue;
  405. } else {
  406. throw new Exception("Directory does not exist: $pValue");
  407. }
  408. }
  409. }