TCPDF ERROR: [Image] Unable to get image
Asked Answered
M

13

25

I'm using TCPDF with Drupal's print module to generate PDF of articles, & ending up with following error message when I click the link to generate PDF:

TCPDF ERROR: [Image] Unable to get image: http://localhost/pathToDrupal/themes/bartik/logo.png

The Image exists in the location specified. I tried applying:

allow_url_fopen = On;
allow_url_include = On;

in php.ini but that could not resolve the problem.

Please care to help :(

Menarche answered 5/10, 2011 at 17:57 Comment(0)
N
29

Apparently the "fopen"-wrappers are not used by TCPDF.

If you supply the URL of an image, TCPDF tries to download it with cURL into the "cache"-directory where your TCPDF-installation is stored (you have to check what the K_PATH_CACHE-constant contains to be sure).

So I guess you have to have write permissions in this directory that the magic works. Also you need cURL enabled.

Ninos answered 5/10, 2011 at 18:33 Comment(4)
Thanks for the answer, @Ninos . I've another problem now: TCPDF prints incorrectly. I'm trying to print something with Unicode & complex-script (Bengali language) .. can you guess What's wrong? I've managed to install an unicode .ttf font successfully with TCPDF & set it using SetFont()Menarche
It has worked for me, after intall CURL in a debian server: apt-get install php5-curl && /etc/init.d/apache2 restartDumont
I also ran into this issue, but it was because the folder was password protected and curl couldn't access the folder directly. Removed protection and it worked.Ratib
png transparency + protected folder. why it uses CURL for local images it's beyond my understanding...Bacteriolysis
P
10

We had problems with the way connections were handled in our linux "example.com" server. So this lead us to try on the server:

curl -v http://www.example.com/image.jpg

Whenever TCPDF tried to download an image with curl, the image was not found, nevertheless, we could view the image by just opening it directly in the browser (e.g. http://www.example.com/image.jpg).

We solved the problem by setting the example.com VirtualHost to accept 127.0.0.1 connections and adding the line "127.0.0.1 example.com" to /etc/hosts.

Pindus answered 9/12, 2011 at 23:1 Comment(1)
This was working for us for several years. Recent server updates meant we had to add our actual url and the domain of the site using curl too: 123.456.789.123 example.com - not sure which update caused this, but I'm grateful it's working again now.Pipestone
A
8

Just use the image path as "images/your_image.png" instead of "http://yourdomain.com/images/your_image.png" in the PDF html.

Aeolis answered 19/11, 2014 at 11:46 Comment(0)
D
4

I've found that TCPDF would throw the Unable to get Image: error when the src was an absolute link. Just changing to a relative link would successfully create the PDF.

Decaliter answered 19/2, 2013 at 16:45 Comment(1)
on this note, mine wouldn't work with https://, and because I am using some heavy mod_rewriting I couldn't use a relative URL. So using the // protocol worked. For anyone using CakePHP having this problem and using the BASE_URL constant, you can use this instead: str_replace(array('http:', 'https:'), '', BASE_URL)Revolver
S
3

I had this problem on a staging server where the docroot of the site was protected by an .htaccess file (to prevent search engine indexing and other confusions)

tcpdf uses curl to fetch the image and it gives this error if it cannot access the file.

To solve it, I added a rule to .htaccess to allow requests from the webserver

Allow from 123.45.6.7.8
Skunk answered 15/10, 2013 at 12:25 Comment(0)
U
3

Try to add path by current working dir.

$img = getcwd().'/web/bundles/_bundlename_/img/logo.png';
Ungodly answered 12/11, 2015 at 17:50 Comment(0)
D
2
$pdf->Image($base_url.'/'.$node->field_loc_images[0]['filepath'] ,30, 40, 75, 113, 'JPG', '', '', true, 300, '');
Diverse answered 25/8, 2012 at 12:11 Comment(1)
When answering with code, it is best to provide at least a little explanation alongside the code that solves the problem so that the OP (and other visitors) can understand better why this will work.Atiana
D
1

In your font unicode problems you need to put this syntax code:

// set font
$fontname = $pdf->addTTFfont('../your path here/sampletruetype.ttf', 'TrueTypeUnicode', '', 32);

$pdf->SetFont($fontname, '', <font size value here>);

put it before you add the page...

Dylandylana answered 24/5, 2012 at 5:23 Comment(0)
C
1

In drupal be sure to include the tcpdf library in your function and not at the top of your module file or you will get this error.

Consumable answered 8/5, 2015 at 10:46 Comment(0)
D
0

try this also

foreach($node->field_loc_images as $key=> $s)
{
    $pdf->Image($base_url.'/'.$s['filepath'], $x, $y, $w, $h, 'JPG', '', '', false, 300, '', false, false, 0, $fitbox, false, false);
}
Diverse answered 25/8, 2012 at 12:14 Comment(1)
Care to comment your code, why you think this is gonna work? The question has an already accepted answer, what does yours adds to the problem?Eraser
A
0

To expand on the error. It also seems to fail with base64 embedded images. This is a big problem for me right now.

Azine answered 13/8, 2013 at 14:7 Comment(0)
K
0

After I upgraded PHP 5.5 to 5.6, I lost hours because of an image error,

I found the solution here from @caligari (a comment on the accepted answer) and it fixed the problem for me:

install CURL like this:

apt-get install php5-curl && /etc/init.d/apache2 restart. 
Kvass answered 26/7, 2017 at 14:31 Comment(0)
P
0

In my case I tried to

curl -v http://www.example.com/image.jpg

and the "curl: (60) server certificate verification failed." was shown. So it was simply a certificate issue.

Penthea answered 18/8, 2019 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.