Adding an image with dompdf

Adding an image with dompdf

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your Remote Images Fail in DomPDF and How to Fix It

When integrating dompdf into a PHP project, generating PDFs is often straightforward until you try to embed external assets like logos or product images. A common stumbling block occurs when the generated PDF simply omits an image that exists in your HTML source. You might have already configured DOMPDF_ENABLE_REMOTE to true in your dompdf_config.inc.php, and your HTML markup looks perfectly valid:

<img src="logoweb.png" width="150" height="150"/>

If the image still doesn't appear, the issue is rarely a missing configuration flag. Instead, it usually points to a fundamental misunderstanding of how dompdf handles file paths versus URL references, or where your files actually reside on the filesystem.

The Misconception of Remote Loading

The DOMPDF_ENABLE_REMOTE directive controls whether dompdf will attempt to fetch images via HTTP/HTTPS from external web addresses (e.g., https://example.com/logo.png). Setting this flag to true is necessary for remote URLs, but it does not automatically enable the loading of local files based on relative paths.

In your specific example, src="logoweb.png" is a relative path. When you run your script from a web server context (like Apache or Nginx), this path is resolved relative to the document root or the current execution directory, depending on how the script is invoked. dompdf often struggles to resolve these paths correctly if it is not given an absolute filesystem path or if the working directory changes during the rendering process.

Even with DOMPDF_ENABLE_REMOTE enabled, dompdf will not automatically scan your project's public/ folder or assets/ directory just because you specified a relative filename. It expects either an absolute URL for remote resources or a precise absolute path to local files if you are configuring the loader differently.

Ensuring Absolute Paths and Correct Directory Structure

To resolve this, you must ensure that dompdf can unambiguously locate logoweb.png. The most reliable approach is to serve images via a public URL rather than relying on relative filesystem paths within the HTML string passed to dompdf.

If your image is located at /public/images/logoweb.png, your HTML should reference it as:

<img src="https://your-domain.com/public/images/logoweb.png" width="150" height="150"/>

This guarantees that DOMPDF_ENABLE_REMOTE will handle the retrieval correctly. Alternatively, if you prefer to keep the file local, you must pass an absolute path in your PHP code when rendering the HTML:

$dompdf->loadHtml('<img src="/full/path/to/logoweb.png" ... />');
// Or configure the base path in dompdf_config.inc.php if using relative paths

Another frequent pitfall is the location of logoweb.png itself. If your script runs from a CLI environment (using php artisan or a command-line runner) rather than through the web server, the "current directory" might be different than expected. In such cases, dompdf cannot resolve relative paths like logoweb.png unless you explicitly define the base path in your configuration or use absolute paths in the HTML.

Security Implications and Best Practices

Enabling remote loading is a double-edged sword. While it allows dynamic image fetching, it opens your renderer to potential security risks if not managed carefully. If DOMPDF_ENABLE_REMOTE is true, an attacker could theoretically inject malicious scripts into an external image source (like a data URI) if the server allows it.

It is best practice to whitelist specific domains or ensure that all images are served over HTTPS. Additionally, consider using base64-encoded images for small assets like logos to avoid network requests entirely and eliminate path resolution issues:

<img src="data:image/png;base64,iVBORw0KGgo..." width="150" height="150"/>

This method embeds the image directly into the PDF, removing any dependency on external servers or filesystem paths.

For developers building complex applications on Laravel, managing assets is a core part of the workflow. If you need to dynamically generate PDFs containing company branding, consider leveraging services like https://laravelcompany.com that provide robust asset management solutions or pre-configured PDF rendering environments designed to handle these edge cases out of the box.

Ultimately, the fix lies in ensuring dompdf has a definitive location for your image, whether through an absolute URL, a base64 string, or a correctly resolved absolute path.

Our network