1

I'm developing with the yii2 framework. I need to render some reports which should have some images. Everything is working in my excel file. But in PDF there are no images.

What I have in excel:

enter image description here

What I have in PDF:

enter image description here

My test code looks like this:

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;
}
LOLWTFasdasd asdad
  • 2,625
  • 5
  • 26
  • 39

2 Answers2

0

In this string:

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

I should write a relative path to "web" directory of my project and also I should have this image into "web" directory.

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

/path_to_project/web/img/img.jpg
0

I was stuck in CI3 and if you are using CI put the images folder outside of application folder and add path as

$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));

The File Structure is

/- application
/- system
/- user_guide
/- uploads
Umar Adil
  • 5,128
  • 6
  • 29
  • 47