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 :(

Shafiul
  • 2,832
  • 9
  • 37
  • 55

13 Answers13

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.

vstm
  • 12,407
  • 1
  • 51
  • 47
  • Thanks for the answer, @vstm . 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() – Shafiul Oct 05 '11 at 18:53
  • 3
    It has worked for me, after intall CURL in a debian server: `apt-get install php5-curl && /etc/init.d/apache2 restart` – caligari May 14 '12 at 05:32
  • 1
    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. – tylerpenney May 22 '12 at 18:22
  • png transparency + protected folder. why it uses CURL for local images it's beyond my understanding... – Bobby Tables Mar 03 '15 at 12:59
9

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.

luissquall
  • 1,740
  • 19
  • 14
  • 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. – squarecandy Dec 03 '19 at 06:19
8

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

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.

Michael
  • 63
  • 4
  • 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)` – scrowler Jan 14 '14 at 23:51
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
batigolix
  • 1,674
  • 18
  • 20
3

Try to add path by current working dir.

$img = getcwd().'/web/bundles/_bundlename_/img/logo.png';
2
$pdf->Image($base_url.'/'.$node->field_loc_images[0]['filepath'] ,30, 40, 75, 113, 'JPG', '', '', true, 300, '');
Kijewski
  • 25,517
  • 12
  • 101
  • 143
shashank
  • 21
  • 1
  • 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. – Fluffeh Sep 27 '12 at 11:03
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...

jay ralph
  • 11
  • 7
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.

flabel
  • 31
  • 3
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);
}
arrowd
  • 33,231
  • 8
  • 79
  • 110
shashank
  • 21
  • 1
  • 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? – Yaroslav Oct 05 '12 at 06:43
0

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

Dieter Gribnitz
  • 5,062
  • 2
  • 41
  • 38
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. 
Frits
  • 7,341
  • 10
  • 42
  • 60
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.

Viktor Joras
  • 721
  • 9
  • 16