images' path in PDF

Topics: Developer Forum, Project Management Forum, User Forum
Jun 25, 2009 at 7:24 AM

Hi again. My script creates excel2003, as well as excel2007 and pdf files. 

some days ago i added images-support to it. the code belov shows how i create and add it:

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Name');
$objDrawing->setDescription('Desc');
$objDrawing->setPath($_SERVER['DOCUMENT_ROOT'].$pathToImageRes);
$objDrawing->setHeight(250);
$objDrawing->setCoordinates('A1');
$objDrawing->setOffsetX(3);
//$objDrawing->setRotation(0);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(45);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

and there is a little trouble with path of image in it.

both excel 2003 and excel 2007 files creates almost successfully. a trouble was with pdf. script says:
"TCPDF ERROR: [Image] No such file or directory in ./www/username/www/htdocs//_russian/analytics/content/val_img/650/jan09_v4.png"
I couldn't understand what was wrong, until I found a starting dot in the image path. I tried to find a place in Shared/PDF/tcpdf.php where this dot is appends. It was unsuccessful. then I decided to add these lines to the `public function Image(lots_of_params)`, just at the beginnig of it:

$twoChars = substr($file, 0, 2);
if ($twoChars == "./")
	$file = substr($file, 1);

and it helped me. $file contains a right path.

 

p.s. can somebody explain the code below? why is it used? in my case, $tag['attribute']['src'] contains "/www/username/www/htdocs//_russian/analytics/content/val_img/650/jan09_v4.png", as you see the first symbol is "/", and according to the code below, $_SERVER['DOCUMENT_ROOT'] that is "/www/username/www/htdocs/" will be added at the beginning of $tag['attribute']['src'] and it will be "/www/username/www/htdocs//www/username/www/htdocs//_russian/analytics/content/val_img/650/jan09_v4.png". Am I right? why is it used? I can't understand anyway.

 

case 'img': {
	if (isset($tag['attribute']['src'])) {
		// replace relative path with real server path
		if ($tag['attribute']['src'][0] == '/') {
			$tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
		}

Developer
Jun 27, 2009 at 1:24 AM

Thanks for the research. I now recall seing that TCPDF ERROR before, but didn't investigate it further. Will examine your example and post back when I know more.

Developer
Jul 11, 2009 at 9:45 AM

Using your example I was indeed able to reconstruct that TCPDF error on Linux. After some thinking, I think the solution is to avoid using an absolute path in the setPath() method. I would only use relative paths because, as far as I can see, absolute path won't work if you instead wanted to generate HTML file instead of PDF.

I think PHPExcel could be improved the way it handles images. For example, if you convert from .xlsx to .html, (plain read/write), I would expect that the images are extracted from the xlsx-file and saved to the disk in a directory next to the saved HTML-file. Just like Excel does when you save as HTML. Currently, this is not what happens in PHPExcel. Instead images are required to be pre-extracted which is not always ideal.

In short, for now, use relative paths for the images. In the future we must try to improve the library so it can handle absolute paths, but that will need some adjustments.

Jul 14, 2009 at 7:28 AM

Thanks for the response! I will try to use your advice in future projects.

Dec 1, 2009 at 7:10 PM
koyama wrote:

I think PHPExcel could be improved the way it handles images. For example, if you convert from .xlsx to .html, (plain read/write), I would expect that the images are extracted from the xlsx-file and saved to the disk in a directory next to the saved HTML-file. Just like Excel does when you save as HTML. Currently, this is not what happens in PHPExcel. Instead images are required to be pre-extracted which is not always ideal.

This is a great idea.  Is there a work item open for this enhancement?

Developer
Jan 22, 2010 at 5:52 AM
MitchMcBride wrote:

This is a great idea.  Is there a work item open for this enhancement?

Currently not. But there are a couple of other related work items involving images. Once they are fixed we can bring this up.