2017-07-28 80 views
1

我有一個excel文件,我想用php excel將文件轉換爲pdf。使用下面的代碼:將excel文件轉換爲pdf,使用phpexcel返回空文件

namespace frontend\controllers; 
use PHPExcel; 
use PHPExcel_IOFactory; 
use PHPExcel_Settings; 
use PHPExcel_Style_Alignment; 
use yii\web\Controller; 

class ExportsController extends Controller 
{ 
    public function actionIndex() 
    { 
     $inputFile = \Yii::getAlias('@frontend') . '/controllers/matrix.xls'; 
     $inputFileType = PHPExcel_IOFactory::identify($inputFile); 
     $objReader = PHPExcel_IOFactory::createReader($inputFileType); 
     $objPHPExcel = $objReader->load($inputFile); 
     $objPHPExcel->setActiveSheetIndex(0); 

     $rendererLibrary = 'DomPDF'; 
     $rendererLibraryPath = \Yii::getAlias('@frontend') . '/components/Classes/PHPExcel/Writer/PDF/' . $rendererLibrary; 
     $rendererLibraryPath .= '.php'; 

     if (!PHPExcel_Settings::setPdfRenderer(
      $rendererLibrary, 
      $rendererLibraryPath 
     ) 
     ) { 
      die(
       'NOTICE: Please set the $rendererName and $rendererLibraryPath values' . 
       '<br />' . 
       'at the top of this script as appropriate for your directory structure' 
      ); 
     } 
     header('Content-Type: application/pdf'); 
     header('Content-Disposition: attachment;filename="01simple.pdf"'); 
     header('Cache-Control: max-age=0'); 
     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); 
     $objWriter->save('php://output'); 
    } 
} 

當我評論的最後兩行代碼否則它會返回空的PDF文件,其拋出以下錯誤:net::ERR_INVALID_RESPONSE

你能幫我找到代碼的錯誤嗎?

回答

0

添加該代碼在最後

header('Content-Type: application/pdf'); 
    header('Content-Disposition: attachment;filename="01simple.pdf"'); 
    header('Cache-Control: max-age=0'); 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); 
    ob_end_clean(); 
    $objWriter->save('php://output'); 
+0

仍然是一樣的錯誤 –

+0

當我把$ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'PDF');在標題之前,它沒有給net :: ERR_INVALID_RESPONSE,但顯示另一個錯誤無法加載PDF渲染庫 –

+0

使用required_once加載PDF庫 –

0

如果你的xls文件比圍繞3000條記錄以上,DOMPDF會產生內存錯誤。

+0

你建議用什麼來代替DomPDF –

相關問題