tcpdf is trying to write in /sites/all/libraries/tcpdf/cache

Fatal error: Uncaught exception 'ImagickException' with message 'Permission denied to: .../tcpdf/cache/mska_58d47ffff0132735a2ad9d61fc2294a4' in .../tcpdf/tcpdf.php:8083 Stack trace: #0 .../tcpdf/tcpdf.php(8083): Imagick->writeimage('...') #1 .../tcpdf/tcpdf.php(7545): TCPDF->ImagePngAlpha('/home/clients/g...', 51.8784916667, 17.9292126667, 22, 22, 6.35, 6.35, 'PNG', '', 'B', false, 300, '', '58d47ffff013273...') #2 [internal function]: TCPDF->Image('...', 51.8784916667, 17.9292126667, 6.35, 6.35, '', '', 'B', false, 300, '', false, false, Array, false, false, true) #3 /lib in .../tcpdf/tcpdf.php on line 8083

if it doesn't has permissions it returns this error.
this can't happen, permission to write on libraries and modules dir is a security risk. move cache out to /sites/default/files/tmp by default and provide a variable to change the directory.



hunziker’s picture

Your right, we need to change this.

I have never give the system the option to right to those directories and it works. I depends on the environment (Image Processing Library etc.).

sachbearbeiter’s picture


patcon’s picture


Came across this and just wanted to share where the cache path and all that is set :)

castawaybcn’s picture

Just in case somebody can't get it to work (thanks to patcon for pointing out the solution):
1. Create a folder named tmp in your_drupal_site/sites/default/files
2. Make it writable by the server
3. Open the file your_drupal_site/sites/all/libraries/tcpdf/config/tcpdf_config.php and change this:

 * cache directory for temporary files (full path)
define ('K_PATH_CACHE', K_PATH_MAIN.'cache/');

 * cache directory for temporary files (url path)
define ('K_PATH_URL_CACHE', K_PATH_URL.'cache/');

to this:
 * cache directory for temporary files (full path)
define ('K_PATH_CACHE', K_PATH_MAIN.'../../../default/files/tmp/');

 * cache directory for temporary files (url path)
define ('K_PATH_URL_CACHE', K_PATH_URL.'../../../default/files/tmp/');

Of course you can use any name other than tmp for your folder.

jlab’s picture

I got this error after upgrading from Drupal 7.10 to Drupal 7.14

I copied the origional tcpdf and fpdi libraries I used on the Drupal 7.10 platform and got the above error...

When I install a new tcpdf library downloaded from the site the error goes away but none of the text is displayed in pdf only the images...

I have two sites on this platform both using views_pdf so the above solution will only solve one sites issues...

What would the best versions of tcpdf and fpdi to use in conjunction with this module?

ownage’s picture

Version:6.x-1.0» 7.x-1.0-rc1

Upgrading 7.12 to 7.14 (important Security Update) just caused my Views PDF View with images to render a similar error as well:

Warning: fclose() expects parameter 1 to be resource, boolean given in TCPDF->Image() (line 7525 of /var/www/html/sites/all/libraries/tcpdf/tcpdf.php).
Warning: fwrite() expects parameter 1 to be resource, boolean given in TCPDF->Image() (line 7524 of /var/www/html/sites/all/libraries/tcpdf/tcpdf.php).
Warning: fopen(/var/www/html/sites/all/libraries/tcpdf/cache/img_344d0d621153b4d6e332f4042ef3a22a): failed to open stream: Permission denied in TCPDF->Image() (line 7523 of /var/www/html/sites/all/libraries/tcpdf/tcpdf.php).

^ I get this for every node in the view

Line 7523 starts on $fp line:

					if ($imgdata !== FALSE) {
						// copy image to cache
						$file = K_PATH_CACHE.'img_'.md5($imgdata);
						$fp = fopen($file, 'w');
						fwrite($fp, $imgdata);
						$cached_file = true;
						$imsize = @getimagesize($file);
						if ($imsize === FALSE) {
							$cached_file = false;


On a side note, I would appreciate some help on this as this is on a client's production website who will be needing this view in the next couple weeks. I'll experiment with it but have never had to fix a library related issue in the past.

ownage’s picture

I was able to temporarily fix my specific issue by changing $diskcache to true on line 1213:

protected $diskcache = true;

I still get the errors because the "/var/www/html/sites/all/libraries/tcpdf/cache" directory isn't writable for this script. Gotta contact my other webmaster to see how he set up the privileges. If I can suppress the errors, I will let you know here.

patcon’s picture

Hi ownage,

Just a heads up, but that's a totally different error from the original, and so probably belongs in another new issue. I'd open a new one instead of posting here :)

glenn_jones’s picture

Same issue here

TCPDF ERROR: [Image] Unable to get image: /web/libraries/tcpdf//sites/default/files/styles/exhibitor_logo_140/adaptive-image/public/E-Boost%20logo.png

Can't we just change the directory?


agnese.stelce’s picture

Same issue here. I think it's time to change cache directory. Print, email and and PDF versions module already do that - TCPDF allows to set custom cache directory with K_TCPDF_EXTERNAL_CONFIG setting. I will try to create a patch for that.

Simon Georges’s picture

Version:7.x-1.0-rc1» 7.x-1.x-dev

Thanks for trying to create a patch!

agnese.stelce’s picture

Hello, Simon Georges!

I have created a patch for custom cache directory against 7.x - 1.x. I took idea from Printer, email and PDF versions (see here http://drupalcode.org/project/print.git/blob/refs/heads/7.x-1.x:/print_p...) Print module has separate setting for that, my patch at the moment just change the TCPDF cache directory so maybe that should be improved.

Simon Georges’s picture

Status:Active» Needs review

Hey, thanks!

Can somebody else review it, so we can confirm it works on several server configurations?

jibize’s picture

The patch from #12 worked for me.


mareks’s picture

I can confirm that patch #12 applied to 7.x-1.x-dev (2013-Mar-09) solved the issue for me.

I had all sort of error messages (see below) before that and no images.

Warning: getimagesize(http://dev.local/sites/default/files/property/fields/field_property_layout_plan/test-property-1_0.jpg) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in TCPDF_IMAGES::_parsejpeg() (line 163 of /var/www/dev.local/html/sites/all/libraries/tcpdf/include/tcpdf_images.php).

Warning: imagecreatefromjpeg(http://dev.local/sites/default/files/property/fields/field_property_layout_plan/test-property-1_0.jpg) [function.imagecreatefromjpeg]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in TCPDF->Image() (line 7017 of /var/www/dev.local/html/sites/all/libraries/tcpdf/tcpdf.php).

Warning: imagejpeg(): supplied argument is not a valid Image resource in TCPDF_IMAGES::_toJPEG() (line 148 of /var/www/dev.local/html/sites/all/libraries/tcpdf/include/tcpdf_images.php).

Warning: imagedestroy(): supplied argument is not a valid Image resource in TCPDF_IMAGES::_toJPEG() (line 149 of /var/www/dev.local/html/sites/all/libraries/tcpdf/include/tcpdf_images.php).

Notice: getimagesize() [function.getimagesize]: Read error! in TCPDF_IMAGES::_parsejpeg() (line 163 of /var/www/dev.local/html/sites/all/libraries/tcpdf/include/tcpdf_images.php).
Simon Georges’s picture

Status:Needs review» Fixed

Ok, committed, thanks for the patch and the reviews!

killua99’s picture

Status:Fixed» Reviewed & tested by the community

Exist a patch to handle this issues and others. Please see #2032189: Multiple issue fixing in a single patch and review others issues patch.

This is RTBC, and ready to apply.

Simon Georges’s picture

Status:Reviewed & tested by the community» Fixed

This patch has already been committed and pushed (just minutes before your comment, I suppose it's just damn bad luck on the timing on my part).

killua99’s picture

Ummmm yeah I'll check again the full patch and handle the others fixings.

killua99’s picture

Priority:Critical» Normal

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.