2

I am rendering some html pages in dompdf. But It doesn't seem to be able to include my images or external css files. If I simply print the page to the screen everything works fine. But dompdf can't find them.

I found a function called: setBasePath where the documentation states "Sets the base path used for external stylesheets and images." Sound like what I want but what is this base path relative too? I tried entering relative to root but it still doesn't find anything.

$dompdf = new Dompdf();
$dompdf->loadHtml($template);
$dompdf->setBasePath(realpath('./'));
$dompdf->render();
$dompdf->stream('note');
danielson317
  • 3,121
  • 3
  • 28
  • 43

1 Answers1

1

The base path is the path used when a resource is references using a relative path (e.g. inc/site.css). For resources referenced using absolute paths or full URIs the base path would not be used.

As with any other running script Dompdf doesn't have knowledge of any root path other than the file system root. Thus, any absolute path is read from the root of the file system. Relative paths are determined based on the current working directory (CWD) of the executing PHP script.

In your example you set the base path relative to ./ which would be the same as the base path already used by dompdf when loading a document using $dompdf->loadHtml(). To give a more complete answer it would help to have more information on your file layout and how you reference those files in the HTML.

Try this discussion for more information on how resource references are interpreted.

Community
  • 1
  • 1
BrianS
  • 13,284
  • 15
  • 62
  • 125
  • I understand what the base path is supposed to do. What is the path relative too? If ./ is the same path used by dompdf what is the path used by dompdf? Is it the directory dompdf is located in? Is it the directory of the php file that calls dompdf? Is it the apache root? Is it the server root? – danielson317 Aug 10 '16 at 16:08
  • They're relative to the CWD of the executing PHP script, answer updated. – BrianS Aug 10 '16 at 17:42