PHP - Excel 呈现没有图像的 PDF

PHP - Excel renders PDF without image

我正在使用 yii2 框架进行开发。我需要呈现一些应该有一些图像的报告。在我的 excel 文件中一切正常。但是在PDF中没有图像。

我在 excel 中有什么:

我有 PDF 格式的内容:

我的测试代码如下所示:

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;
}

在此字符串中:

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

我应该写一个相对路径到我的项目的 "web" 目录,而且我应该把这张图片放到 "web" 目录中。

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

/path_to_project/web/img/img.jpg

我卡在了 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