9
votes

I get my images in my pdf document on my localhost but on the production site i get the error TCPDF ERROR: [Image] Unable to get image i am using an html img tag to get the images and the src is the directory path to this image not a url, but i found out that TCPDF is adding the path i give it with the path to my www folder like:

path to picture i give to tcpdf: home/inc_dir/img/pic.jpg
tcpdf looks for it here: home/www/home/inc_dir/pic.jpg

can someone please help me find out tcpdf is concatenating the directories?

4
Try with a relative path (as it obviously is one): ../inc_dir/pic.jpg. - hakre
@Sado Ogie how was this fixed? - edocabhi

4 Answers

12
votes

You can also change only the image path instead of main path use:

define('K_PATH_IMAGES', '/path/to/images/');
require_once('tcpdf.php');

This won't break fonts/ and other tcpdf paths.

7
votes

TCPDF is using $_SERVER['DOCUMENT_ROOT'] as a root directory of all your images, and builds their absolute paths in relation to it. You can change it either in $_SERVER or with this PHP constant: K_PATH_MAIN:

define('K_PATH_MAIN', '/path/to/my-images/');
require_once 'tcpdf.php';
3
votes

I use image data instead of paths. It can be passed to TCPDF using an @ in the image's src-attribute, like so:

<img src="@<?php echo base64_encode('/path/to/image.png')?>" />

An img-tag in HTML takes a BASE64 encoded string, unlike the Image() function, which takes unencoded data.

I don't know if this is even documented, I found this by reading the code (tcpdf.php, line 18824 pp):

if ($imgsrc[0] === '@') {
    // data stream
    $imgsrc = '@'.base64_decode(substr($imgsrc, 1));
    $type = '';
}
0
votes

I have got the same problem. But it is now resolved. I have change the code of TCPDF.php from

Old Code

if ($tag['attribute']['src'][0] == '/') {
   $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
}
$tag['attribute']['src'] = urldecode($tag['attribute']['src']);
$tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);

New Code

if ($tag['attribute']['src'][0] == '/') {
   $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
   $tag['attribute']['src'] = urldecode($tag['attribute']['src']);
   $tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);
}

Please try this.