/** * Save PHPExcel to file * * @param string $pFileName * @throws Exception */ public function save($pFilename = null) { // garbage collect $this->_phpExcel->garbageCollect(); $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType(); PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE); // Open file $fileHandle = fopen($pFilename, 'w'); if ($fileHandle === false) { throw new Exception("Could not open file $pFilename for writing."); } // Set PDF $this->_isPdf = true; // Build CSS $this->buildCSS(true); if(!$this->_printParamsSetted) { // Default PDF paper size $paperSize = 'A4'; // Check for paper size and page orientation if (is_null($this->getSheetIndex())) { $orientation = ($this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; $printPaperSize = $this->_phpExcel->getSheet(0)->getPageSetup()->getPaperSize(); $printMargins = $this->_phpExcel->getSheet(0)->getPageMargins(); } else { $orientation = ($this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; $printPaperSize = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize(); $printMargins = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageMargins(); } $this->setOrientation($orientation); // Override Page Orientation if (!is_null($this->getOrientation())) { $orientation = ($this->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT) ? PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT : $this->getOrientation(); } $orientation = strtoupper($orientation); // Override Paper Size if (!is_null($this->getPaperSize())) { $printPaperSize = $this->getPaperSize(); } if (isset(self::$_paperSizes[$printPaperSize])) { $paperSize = self::$_paperSizes[$printPaperSize]; } if(is_string($paperSize)){ $paperSize = strtoupper($paperSize.(substr($orientation,0,1) == 'L' ? '-L' : '')); } if($printMargins) { //$printMargins是英寸制,要转换成毫米 $tmargin = 25.4 * $printMargins->getTop(); $bmargin = 25.4 * $printMargins->getBottom(); $lmargin = 25.4 * $printMargins->getLeft(); $rmargin = 25.4 * $printMargins->getRight(); $hmargin = 25.4 * $printMargins->getHeader(); $fmargin = 25.4 * $printMargins->getFooter(); } } else { $paperSize = $this->_printPaperSize; $orientation = $this->_printOrientation; $tmargin = $this->_printTmargin; $bmargin = $this->_printBmargin; $lmargin = $this->_printLmargin; $rmargin = $this->_printRmargin; $hmargin = $this->_printHmargin; $fmargin = $this->_printFmargin; } error_reporting(E_ALL ^ E_NOTICE); //当前的版本很多notice警告,屏蔽掉 // Create PDF $pdf = new mpdf(); $pdf->useAdobeCJK = true; // Default setting in config.php $pdf->SetAutoFont(AUTOFONT_ALL); // AUTOFONT_CJK | AUTOFONT_THAIVIET | AUTOFONT_RTL | AUTOFONT_INDIC // AUTOFONT_ALL // Log::write("\ndefault pdf:\nT:".$pdf->tMargin."\nB:".$pdf->bMargin."\nL:".$pdf->DeflMargin."\nR:".$pdf->DefrMargin."\nH:".$pdf->margin_header."\nF:".$pdf->margin_footer."\n\n"); $pdf->_setPageSize($paperSize, $orientation); $pdf->DefOrientation = $orientation; if(!empty($this->_printFooterO)) { $pdf->SetHTMLFooter($this->_printFooterO); } if(!empty($this->_printHeaderO)) { $pdf->SetHTMLHeader($this->_printHeaderO, 'O'); } if(!empty($this->_printFooterE)) { $pdf->SetHTMLFooter($this->_printFooterE, 'E'); } if(!empty($this->_printHeaderE)) { $pdf->SetHTMLHeader($this->_printHeaderE, 'E'); } if(!empty($tmargin)) { $pdf->tMargin = $tmargin; } if(!empty($bmargin)) { $pdf->bMargin = $bmargin; } if(!empty($lmargin)) { $pdf->DeflMargin = $lmargin; } if(!empty($rmargin)) { $pdf->DefrMargin = $rmargin; } if(!empty($hmargin)) { $pdf->margin_header = $hmargin; } if(!empty($fmargin)) { $pdf->margin_footer = $fmargin; } $pdf->mirrorMargins = $this->_mirrorMargins; if(!empty($this->_script)) { $pdf->SetJS($this->_script); } // Document info $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle()); $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator()); $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject()); $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords()); $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator()); $pdf->WriteHTML( $this->generateHTMLHeader(false) . $this->generateSheetData() . $this->generateHTMLFooter() ); // Write to file fwrite($fileHandle, $pdf->Output('','S')); // Close file fclose($fileHandle); PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType); }