PHP excell渲染PDF没有图像

时间:2015-10-12 14:13:01

标签: phpexcel

我用yii2框架开发。我需要渲染一些应该有一些图像的报告。在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;
}

2 个答案:

答案 0 :(得分:0)

在此字符串中:

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

我应该写一个相对路径来" web"我的项目的目录,我也应该把这个图像放到" web" 。目录

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

/path_to_project/web/img/img.jpg

答案 1 :(得分:0)

我被困在CI3中,如果你使用CI,将images文件夹放在应用程序文件夹之外并添加路径为

$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