Mpdf: Using <base href>, CSS is first loaded from remote URL even if it is local.

Created on 17 Sep 2016  路  5Comments  路  Source: mpdf/mpdf

I am using mPDF version 6.1.2 downloaded via composer.

In mpdf.php, line 14201, there is:

$contents = @file_get_contents($path);
if ($contents) {
    return $contents;
}
if ($this->basepathIsLocal) {
    ...

This means that using the same CSS for the site and for the PDF, the URL of the CSS being /css/main.css, an attempt is made to load http://mysite/css/main.css.

This results in a hairpinned network call, which (in addition to not being necessary) is not working in my current web server configuration. The call fails after an unsuccessful DNS query. With a bit of fiddling with CSS paths, I was able to make it work again, but with an appreciable delay due to the DNS query which is still made, and still fails.

Even if the site was reachable through hairpinning, I believe that directly accessing the disk would yield better performances.

The modification below works for me if base path is local; if it is not, the code ought to behave exactly as before.

Since the necessary variable is already there, I wrapped the short-circuit in a check:

+++ Only attempt straight content getting if base path is not local
+++ if (!$this->basepathIsLocal) {
$contents = @file_get_contents($path);
if ($contents) {
return $contents;
}
+++ }
if ($this->basepathIsLocal) {
...

bufix research needed

Most helpful comment

Super old reply, but I didn't want to keep this handing without an answer.

I had the same problem, after crawling through the entire codebase I found out that this works in 5.6 (yeah, I'm using the old one still)

$mpdf->SetBasePath('file://path/to/working/folder');

then you can use <img src="my/image.jpg">

and it will look for the file in
file:///path/to/working/folder/my/image.jpg

which is an internal call to (no http calls)

/path/to/working/folder/my/image.jpg

All 5 comments

Super old reply, but I didn't want to keep this handing without an answer.

I had the same problem, after crawling through the entire codebase I found out that this works in 5.6 (yeah, I'm using the old one still)

$mpdf->SetBasePath('file://path/to/working/folder');

then you can use <img src="my/image.jpg">

and it will look for the file in
file:///path/to/working/folder/my/image.jpg

which is an internal call to (no http calls)

/path/to/working/folder/my/image.jpg

Sorry, not working with images start with '/'

@quan-na no, it does not work with images that start with "/"

I haven't tested it, but I think it works the same as hyperlink URL's

in URL's let's say this is the base path:
file://path/to/working/folder/

This "my/image.jpg", becomes "file://path/to/working/folder/my/image.jpg"
This "/my/image.jpg", becomes "file://my/image.jpg"
This "../my/image.jpg", becomes "file://path/to/working/my/image.jpg"

in case /my/image.jpg -> file://path/my/image.jpg ;(
and by /my/image.jpg, I mean /var/ww/html/my/image.jpg ;(((

Try this
$mpdf->SetBasePath('file:///var/www/html');

Was this page helpful?
0 / 5 - 0 ratings

Related issues

azbatuk picture azbatuk  路  4Comments

finwe picture finwe  路  4Comments

jamesbarsby picture jamesbarsby  路  4Comments

maofree picture maofree  路  3Comments

blacksheep-dev picture blacksheep-dev  路  4Comments