Forum How do I...?

Resolve absolute URLs to images and CSS?

I have an HTML document that I'm generating and converting. Thing is, all the image source attributes and CSS links are absolute (ie leading /). I'm having a real hard time getting this to work. This is on PHP using the supplied wrapper. The only possibly relevant method I've seen is setBaseURL() but that doesn't seem to do anything.

How do I set it so Prince uses the parent of the current directory (or some arbitrary directory) as the base to resolve references like /images/logo.gif?
There is no option for this at the moment; it would require some kind of URL remapping. The base URL option is for resolving relative paths to a specified base URL, which doesn't apply in this case.
Hmm, that's weird. Because if you do this:

<img src="images/logo.gif">

and you happen to be a directory with an images subdirectory that contains a logo.gif, it all just works.

Stylesheets aren't as much of a problem because if I have to (although I'd prefer not to) I can use addStyleSheet() on the prince object.

But what am I meant to do to include images?
If you have an absolute URL path such as "/images/foo.jpg" then what it resolves to depends upon the base URL of the document. If the base URL is a HTTP URL such as "", then it will resolve to "". If the base URL is a local file path, then it will resolve to the absolute path "/images/foo.jpg". If you actually want it to refer to "/var/www-data/images/foo.jpg" you would need to specify that path, or use a relative URL that resolves to that path, or we would need to add some other option to Prince. :)
To give you an example. Let's say the script that generates the PDF file is at:


And the image is at:


And the image tag is:

<img src="/images/logo.gif">

The script seems to execute with a current working directory of /document (well, wahtever directory that resolves too), although with Apache rewrite rules I think that might actually be different.

Part of the problem is I don't know what Pache and Prince are separately doing regarding current working directory.

I've tried setting the base URL to the following:


and it didn't work. It'd be nice if the prince log would print some output about the current working directory and/or if it couldn't load a particular stylesheet or image (listing the exact path it tried and why).

This is what's getting me stuck: a complete lack of information.

If I have to put in a full base URL ( to get this to work I wil but I won't be happy about it because the sitename varies depending on if it's a production or test site.

I don't want to put in an absolute pathname (like /var/www/mysite) because that too varies.

But I'll prefer a URL to the absolute path. Can you please confirm how I get it to resovle by path? Just put in the path or a URL like file://... ?

My preferred choice is just to tell Prince (relatively) where the "root" directory of the Web application is and to resolve all URLs from there.

Can this be done? If so, how?
It depends on whether you are running Prince on a local file or a HTTP URL. For example, Prince cannot turn "/images/logo.gif" into "/var/www/mysite/images/logo.gif" if you are trying to process a local file. But if you point Prince at a HTTP URL like "" then it should resolve any image URLs without any problems at all.

That's slightly disappointing but I can workaround that at least and use the REQUEST_URI, hostname, etc to get to it. Thanks for clearing it up.

I'm surprised setBaseURL("../../") doesn't work though. I think it should.
I think an additional option is needed to resolve this situation, but it will take some thought to make sure it doesn't clash awkwardly with existing options like base URL.
Yeah, I'm having this exact problem too, so add one vote for me. I would expect setBaseURL or similar to specify the site root for Prince as having a local development environment and a remote test environment is a pretty common setup for web development.

It's easy enough to set an environment variable based on $_SERVER['HTTP_HOST'] or REQUEST_URI but is is far preferable to set this once in Prince than for every image and CSS file referenced by the HTML, which are almost always site absolute.
OK I have a solution using setBaseURL. I always set up my local development environment to use virtual hosts, and the following setting seems to work for me. I haven't tried it without a vhost, so not sure if this will work in another situation.

$prince -> setBaseURL( 'http://'.$_SERVER['HTTP_HOST'] );

Hope this helps.
In Prince 7.1 we have added an experimental new command-line option called --fileroot, which allows you to specify a path that will be used to resolve absolute filenames.
Anyone got this to work? I tried it and it's still looking for a pth relative to the source doc

My doc is in C:\XSL
images are in C:\XSL\images
I've set fileroot to C:\XSL
the path in my doc is images\logo.gif

This should, if I understand the setting correctly, resolve to C:\XSL\images\logo.gif.

My command-line is:

"C:\Program Files (x86)\Prince\engine\bin\prince.exe" --fileroot "C:\inetpub\wwwroot" ...

Any thoughts?


The --fileroot option only affects absolute paths, eg. URLs that being with "/". Is this the case in your document?
Hi Mike

Thanks for the clarification.

Changed my path to /images, still no luck though. Go this message

prince: /images/Logo.png: warning: can't open input file: No such file or directory

fileroot is set to C:\inetpub\wwwroot and logo.png resides in that dir

Any other suggestions?


If the URL is "/images/Logo.png" and --fileroot is set to "C:\inetpub\wwwroot" then Prince should end up trying to load "C:\inetpub\wwwroot\images\Logo.png". Can you try running Prince with the --debug flag and check the output log?