ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
PDF.php
Go to the documentation of this file.
1 <?php
30 $k_path_url = dirname(__FILE__) . '/PDF';
31 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PDF/tcpdf.php';
32 
46  private $_tempDir = '';
47 
53  private $_font = 'freesans';
54 
60  private $_orientation = null;
61 
67  private $_paperSize = null;
68 
69 
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.)
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)
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)
142  PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER => array(912.76,1261.42) // (322 mm by 445 mm)
143  );
144 
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  }
155 
165  public function setFont($fontName) {
166  $this->_font = $fontName;
167  return $this;
168  }
169 
175  public function getPaperSize() {
176  return $this->_paperSize;
177  }
178 
186  $this->_paperSize = $pValue;
187  return $this;
188  }
189 
195  public function getOrientation() {
196  return $this->_orientation;
197  }
198 
206  $this->_orientation = $pValue;
207  return $this;
208  }
209 
216  public function save($pFilename = null) {
217  // garbage collect
218  $this->_phpExcel->garbageCollect();
219 
220  $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
222 
223  // Open file
224  $fileHandle = fopen($pFilename, 'w');
225  if ($fileHandle === false) {
226  throw new Exception("Could not open file $pFilename for writing.");
227  }
228 
229  // Set PDF
230  $this->_isPdf = true;
231 
232  // Build CSS
233  $this->buildCSS(true);
234 
235  // Generate HTML
236  $html = '';
237  //$html .= $this->generateHTMLHeader(false);
238  $html .= $this->generateSheetData();
239  //$html .= $this->generateHTMLFooter();
240 
241  // Default PDF paper size
242  $paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
243 
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  }
254 
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  }
263 
264 
265  if (isset(self::$_paperSizes[$printPaperSize])) {
266  $paperSize = self::$_paperSizes[$printPaperSize];
267  }
268 
269 
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);
278 
279  $pdf->setPrintHeader(false);
280  $pdf->setPrintFooter(false);
281 
282  $pdf->AddPage();
283 
284  // Set the appropriate font
285  $pdf->SetFont($this->_font);
286  $pdf->writeHTML($html);
287 
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());
294 
295  // Write to file
296  fwrite($fileHandle, $pdf->output($pFilename, 'S'));
297 
298  // Close file
299  fclose($fileHandle);
300 
301  PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
302  }
303 
309  public function getTempDir() {
310  return $this->_tempDir;
311  }
312 
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 }