1

I am using the standard DOMPDF code to render existing web pages (e.g.1):

$dompdf = new DOMPDF();
$dompdf->set_base_path($artpath);
$dompdf->load_html_file($artpath);
$dompdf->render();
$dompdf->stream($pdfpath);

where $artpath' is the path to the HTML code and $pdfpath is the name of the PDF.

However, the web page contains both relative links (which are correctly followed) and absolute links (e.g. /gifs/bullet.gif) which are not found. This is probably because the DOMPDF code is being executed at http://www.epress.ac.uk/src/xtra/makeapdf.php, www.epress.ac.uk being a virtual domain on my server, which also hosts the virtual domain jasss.soc.surrey.ac.uk (that is, both domains are on the same server). It would seem that DOMPDF is using the document root of www.epress.ac.uk, when it should be using the document root of jasss.surrey.ac.uk.

Is there some way around this? I have tried resetting $_SERVER['DOCUMENT_ROOT'] to the document root of jasss.soc.surrey.ac.uk before calling new DOMPDF(), but this doesn't seem to solve the problem. I get errors such as:

file_get_contents(/styles/jasssarticle.css) [function.file-get-contents]: failed to open stream: No such file or directory

Unable to load css file /styles/jasssarticle.css

The web page is valid HTML according to the www validator.w3.org

Thanks for your advice!

Nigel
  • 585
  • 1
  • 5
  • 20
  • What is the value of the `$artpath` variable? When you call `$dompdf->load_html_file()` with a relative path dompdf sets the root path to the file system root, not the website document root. – BrianS Mar 29 '13 at 17:42
  • `$artpath` is `/Volumes/Documents/VirtualSites/jasss/16/2/1.html'. The webroot of jasss.soc.surrey.ac.uk is `/Volumes/Documents/VirtualSites/jasss/`. Hope that helps... Thanks. – Nigel Mar 29 '13 at 23:37
  • Also forgot to ask, what version of dompdf? – BrianS Mar 31 '13 at 00:16
  • The latest - downloaded a couple of days ago - but I can't be more specific because I can't find it referenced anywhere in the code I downloaded! – Nigel Apr 01 '13 at 20:22

1 Answers1

4

You're loading a file via the file system. That means all references to external files that don't include a domain part in the path are rendered relative to the file system. You can reference files in three ways:

  • Full URL (including domain), e.g. http://example.com/image.png. These are always read from the URL specified.
  • Absolute path, e.g. /file/path/image.png. This is read relative to the root of the file system, not the root of the web site, or the user's home directory (in the case of shared hosting).
  • Relative path (no leading slash), e.g. file/path/image.png. This is read relative to the HTML file. So in your case the file would be read from /Volumes/Documents/VirtualSites/jasss/16/2/file/path/image.png.

Calling $dompdf->set_base_path() only affects the relative path.

You'll have to modify the absolute file references to include the path to the website root, e.g. /Volumes/Documents/VirtualSites/jasss/styles/jasssarticle.css, or load the file via the website, e.g. http://jasss.soc.surrey.ac.uk/16/2/1.html.

BrianS
  • 13,284
  • 15
  • 62
  • 125
  • 2
    Thanks for this. Indeed, using the URL seems to be the way forward, i.e `$dompdf->load_html_file("http://jasss.soc.surrey.ac.uk/16/2/2.html");`. However, the results are rather disappointing: the resulting PDF file stops short after about 7 pages and the images are all over the place. The same occurs with other similar pages (e.g. `.../16/2/1.html`) If you have dompdf running, you could see what you get with this URL (it is publicly available). I displayed the contents of `$_dompdf_warnings` but this did not reveal anything much wrong (just a couple of invalid CSS properties). – Nigel Apr 01 '13 at 20:29
  • The document does seem to be giving dompdf some trouble. I suspect a lot of the problems are because dompdf is confused about the document structure. I'll take a closer look when I get a chance. – BrianS Apr 01 '13 at 22:29
  • 1
    "I'll take a closer look when I get a chance." Have you had a chance? Thanks. – Nigel Apr 22 '13 at 10:00
  • 1
    I always believed absolute file paths were a bit hackish. Thanks for your answer. `$dompdf->set_base_path()` and relative links (no leading slash) work great! – Wireblue Oct 08 '14 at 06:22