PHPOffice / PHPExcel

ARCHIVED
Other
11.46k stars 4.19k forks source link

pdf example, add image #547

Open Faryshta opened 9 years ago

Faryshta commented 9 years ago

the pdf example doesn't include how to add an image to the generated file.

On the example the library used is mPDF which has a different approach on how to include images.

While phpexcel uses the file:// protocol to load an image and check if it exists, mPDF uses http:// by default to load an image.

On Excel 5 the way to include an image is

$dirPath = '/var/www/';
$logo = new PHPExcel_Worksheet_Drawing();
$logo->setName('Logo')
    ->setDescription('Logo')
    ->setPath($dirPath . '/logo.png)

Yet that will generate an image on html like

<img src="http://example.org//var/www//logo.png" alt="Logo">

Which obviously won't load.

The solution i found is

$logo = new PHPExcel_Worksheet_Drawing();
$logo->setName('Logo')
    ->setDescription('Logo')
    ->setPath('logo.png', false)

and the false part is vital because without that phpExcel will check if the file exists and won't find it sending an exception.

I think its important to add that to the example because i couldn't find anything related to this issue.

alvaro-canepa commented 8 years ago

Thanks @Faryshta This save me a lot of testing hours...

I use Laravel, and the same doc was download in xls or pdf. For xls the image inclusion must have the full path, but for pdf export, the image must have the public path.

So, I use this, and work fine for me.

// $file is an array with data to export XLS or PDF
// $image was defined from one Laravel Model ($model->image)
$image = ($file['ext'] == 'pdf') ? $image : base_path() . $image
$logo = new PHPExcel_Worksheet_Drawing();
$logo->setName('Logo')
     ->setDescription('Logo')
     ->setPath($image, false)

Thanks and sorry my English.