2015-10-12 58 views
1

我用yii2框架開發。我需要渲染一些應該有一些圖像的報告。一切都很好。但在PDF中沒有圖像。PHP excell渲染PDF無圖像

我在Excel中什麼:

enter image description here

我有PDF:

enter image description here

我的測試代碼是這樣的:

public function run($format = self::EXCEL) { 
    $this->format = $format; 
    if ($this->format == self::PDF) { 
     $rendererName = \PHPExcel_Settings::PDF_RENDERER_MPDF; 
     $rendererLibraryPath = Yii::getAlias('@vendor/mpdf/mpdf/'); 

     if (!\PHPExcel_Settings::setPdfRenderer($rendererName, $rendererLibraryPath)) { 
      throw new BadRequestHttpException('Export pdf fail'); 
     } 
     $this->headerContentType .= 'pdf'; 
     $this->headerFilename .= date('d_m_Y') . '.pdf'; 
    } elseif ($this->format == self::EXCEL) { 
     $this->headerContentType .= 'vnd.ms-excel'; 
     $this->headerFilename .= date('d_m_Y') . '.xls'; 
    } else { 
     throw new Exception('Unknown format for export'); 
    } 

    $this->objPHPExcel->setActiveSheetIndex(0); 
    $activeSheet = $this->objPHPExcel->getActiveSheet(); 
    $activeSheet->setTitle('Sample' . date('d_m_Y')); 

     $objDrawing = new PHPExcel_Worksheet_Drawing(); 
     $objDrawing->setWorksheet($activeSheet); 
     $activeSheet->getColumnDimension('B')->setWidth(50); 
     $activeSheet->getRowDimension(2)->setRowHeight(80); 
     $activeSheet->setCellValue('A2','img -> '); 
     $activeSheet->setCellValue('B2',' '); 
     $objDrawing->setCoordinates('B'.2); 
     $objDrawing->setOffsetX(10)->setOffsetY(10); 

     $objDrawing->setName('Sample_image'); 
     $objDrawing->setDescription('Sample_image'); 
     $objDrawing->setPath('/home/vladimir/projects/temp/img.jpg'); 
     $objDrawing->setWidth(50)->setHeight(50); 

    header($this->headerContentType); 
    header($this->headerFilename); 
    header('Cache-Control: max-age=0'); 
    $objWriter = \PHPExcel_IOFactory::createWriter($this->objPHPExcel, $this->format); 
    $objWriter->save('php://output'); 
    exit; 
} 
+0

是否圖像在HTML中呈現?如果是這樣,那麼問題是在PDF渲染引擎 –

+0

請幫助。可能是我必須寫一些細節?是明確的問題? –

+0

嘗試使用mpdf擴展名(https://github.com/kartik-v/yii2-mpdf)。它會爲你節省很多時間。 –

回答

0

在這字符串:

$objDrawing->setPath('/home/vladimir/projects/temp/img.jpg'); 

我應該寫一個相對路徑到我的項目的「web」目錄,我也應該有這個圖像到「web」目錄。

$objDrawing->setPath('img/img.jpg'); 

/path_to_project/web/img/img.jpg 
0

我被卡在CI3,如果你正在使用CI把圖像應用程序文件夾以外的文件夾並添加路徑

$objDrawing = new PHPExcel_Worksheet_Drawing(); 
$objDrawing->setName('Logo'); 
$objDrawing->setDescription('Logo'); 
$objDrawing->setPath('uploads/organizations/1.jpg'); 
$objDrawing->setHeight(36); 
$objDrawing->setWorksheet($this->excel_reader_writer->getSheetByName($download_section)); 

文件結構是

/- application 
/- system 
/- user_guide 
/- uploads