I'm using a multi-site setup where the (public) files directory for reach website is located at ../sites/example.com/files.

If I try to create a PDF of a node (using TCPDF) I get an error message:

TCPDF ERROR: [Image] Unable to get image: file:///<drupal base>/files/<path>/<filename>

for all images including the site logo. TCPDF seems to be looking in /files for images rather than the defined files directory.

A workaround for the logo is to place a dummy file in the /files directory, but any nodes containing images produce an error message when trying to produce a PDF.

Comments

jcnventura’s picture

The module currently tries to replace the base_url (i.e. http://www.example.com/base_path) with the realpath of (your document root + base_path). Which works fine in single-site installs :)

I've never tested a multi-site install, but I can't understand why the above seems to be dropping the 'sites/example.com' from the path. I'll try to setup a multi-install test to fix this.

ceme’s picture

Priority: Minor » Major

Hi!
I've got the same problem on a single site installation because of my Apache configuration:

DOCUMENT_ROOT = /www/
base_path() = /test/ (eg. http://localhost/test)

But my Drupal install is in /www/test/web/*

So, when you concat DOCUMENT_ROOT and base_path(), it gives me /www/test/ that is not correct (missing the "web" directory in my case).

The problem seems to come from print/print_pdf/print_pdf.pages.inc, lines 123-125:

$pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?([^>]*?>)!is";
$replacement = '$1file://'. realpath($_SERVER["DOCUMENT_ROOT"] . base_path()) .'/$2';
$html = preg_replace($pattern, $replacement, $html);

Here is my config in httpd.conf:

DocumentRoot "/www/"

Alias /test "/www/test/web/"

<Directory "/www/test/web">
    Options Indexes
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

Thanks in advance for your help!
Cedric

ceme’s picture

In my case I resolved the above problem by changing the line 124 in print/print_pdf/print_pdf.pages.inc

from:

$replacement = '$1file://'. realpath($_SERVER["DOCUMENT_ROOT"] . base_path()) .'/$2';

to:

$replacement = '$1file://'. dirname($_SERVER['SCRIPT_FILENAME']).'/' .'/$2';

I hope it can help...
Cedric

jcnventura’s picture

Status: Active » Needs work

There's an extra '/' in the line above. Other than that, it seems to be a better solution indeed.

ceme’s picture

Hehe, you're right! I've added this slash because I thought dirname() was trailing the final slash. But I haven't seen yours ;-)

Thanks to add this code in your next release...

Cedric

jcnventura’s picture

Status: Needs work » Needs review

Hi,

I've committed the patch in #3.

jmbarlow, can I ask you to try the latest dev in your multi-site?

João

jmbarlow’s picture

Here's my contrib:

In this example, the path to the logo file is: http://example.com/sites/example.com/files/logo.png
It's filename is /home/account/public_html/sites/example.com/files/logo.png

Using the original $replacement definition, the value passed (and error) are:
TCPDF ERROR: [Image] Unable to get image: file:///home/account/public_html/./files/logo.png

Using the new $replacement [without the extra slash], the value passed (and error) are:
TCPDF ERROR: [Image] Unable to get image: file:///home/account/public_html/./files/logo.png

Using the same method as from Line 120 above for Private Filesystems, I get:

TCPDF ERROR: [Image] Unable to get image: file:///home/account/public_html/sites/example.com/files/./files/logo.png

which is pretty close. I can fiddle things with .htaccess files and similar to make it work, but someone else might get caught out.

preg_replace() is turning $2 in the $replacement string into "./files/logo.png". I'm dreadful at writing regular expression, but if you can find a way to spit out the filename and higher level directories without the ./files on the front, then you can concatenate that string with the realpath() of the files directory and we're good to go. I think this might work regardless of Private or Public storage as well.

James

jcnventura’s picture

Status: Needs review » Active

Unfortunately, the solution to the private files (file_directory_path()) is not feasible for the general case as images can be included from modules/themes, etc which are not located in the files directory.

I'll need to create a multi-site test to figure this out..

The problem seems indeed to be in $2, as that should be sites/example.com/files/logo.png and not './files/logo.png'..

João

jmbarlow’s picture

I can arrange a test environment with a few sites in it if you need one.

jcnventura’s picture

Status: Active » Postponed (maintainer needs more info)

Hi,

I think this may have been caused by my removal of a call to init_theme() a few weeks ago. Can you please add a call to init_theme() at the beginning of the _print_var_generator() function in print.pages.inc?

This is caused by themes that rely on path_to_theme() instead of drupal_get_path("theme", __theme_name__).

I've added the init_theme call back to CVS, but the next dev version may take some hours to generate.

João

jmbarlow’s picture

I don't see a print_var_generator() function in the print.pages.inc in 6.x-1.11

jcnventura’s picture

It's around line 152.

jcnventura’s picture

Anyway, try the 6.x-1.12 version which already has the call to init_theme added.

jmbarlow’s picture

Version: 6.x-1.11 » 6.x-1.12

No joy on 6.x-1.12, unfortunately.

The error message from TCPDF is:

TCPDF ERROR: [Image] Unable to get image: file:///home/account/public_html/./files/logo.png

where the actual multisite path is

file:///home/account/public_html/sites/example.com/files/logo.png

jcnventura’s picture

Status: Postponed (maintainer needs more info) » Active
kirie’s picture

I'm having truble with TCPDF for a multisite as well. The error displayed is like this:

TCPDF ERROR: [Image] No such file or directory in file:///home/admin/domains/multisite.example.com/public_html/sites/subsite.com/files/fields/field_image/news/2010/image.jpg?1287688840

What's strange is that the image is actually present in the that location. The permissions on the file is:
-rw-rw-r-- 1 apache apache - does that have anything to do with it?

As a side note, the actual files/ directory is symlinked from another place in the directory tree.

pedropablo’s picture

Hello all. After dealing with this strange bug, I have found that the problem resides in the function _print_pdf_file_access_images()

When this function try to get the complete file:// url for the file, it does not remove the query string, thus resulting in the local file not being available to tcpdf (as tcpdf does not understands about query strings).

I don't know what's wrong with the regular expression defined in variable $pattern
$pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?([^>]*?>)!is";
too much for my limited experience with regexp :-))
However, substituting that $pattern expression with this simpler one (used in a previous version of the module)
$pattern = "!(<img\s[^>]*?src\s*?=.*?)%3F\d+([^>]*?>)!is";
Works for me.

BTW, most error messages posted in this thread have three (3) slashes after file: file:///. These are added by the $replacement variable in _print_pdf_file_access_images() function. Although it shouldn't be much of a problem: I have modified the numbre of slashes, and it is only reflected when there are other error, as changing this does not results in errors once the $pattern variable is fixed (very strange, indeed)

powery’s picture

I have the same issue:

TCPDF ERROR: [Image] Unable to get image: /home/users/rad/blabla.cz/web/sites/all/libraries/tcpdf/images/file:///home/users/rad/blabla.cz/web/sites/default/files/logo.png

The latest dev version has the same problem.

pinoguin’s picture

Even from a non-multi-site I'm still getting the same error

iamjon’s picture

I'm also having the problem with a multisite/multilang install.

jcnventura’s picture

I'm planning to add an option to not use local file access, which may or may not fix the problem.

The major problem with developing a fix for this is the extra time in setting up a multi-site test.

iamjon’s picture

jcnventura
if I have a multisite test running, how can I help?

jcnventura’s picture

Status: Active » Postponed (maintainer needs more info)

@iamjon: look at the lines surrounding the line with:
$replacement = '$1file://'. dirname($_SERVER['SCRIPT_FILENAME']).'/' .'/$2';

Maybe you can PM to me the value of the $html, $pattern and $replacement going into those lines.. Also, I'll need to know the real path in the server where one those files can be found.. They should be similar to value of $replacement.

jcnventura’s picture

Status: Postponed (maintainer needs more info) » Fixed

The latest version of the module (6.x-1.14) is by default configured to access images using the HTTP access. (see #1158678: Print module does not preprocess ImageCache Formats)

It can be configured back to the old setting, if required..

Status: Fixed » Closed (fixed)

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