Dompdf Remote Image is Not Displaying in PDF
When generating PDFs with Dompdf, local images are handled automatically by the library. However, remote URLs often fail to render if your server environment restricts direct file access. The core issue usually stems from PHP's allow_url_fopen setting being disabled. This restriction prevents Dompdf from fetching external resources directly via HTTP, resulting in an "Image not found" error when trying to load assets like http://localhost/dompdf/image.jpg.
Since you cannot enable allow_url_fopen due to security policies or lack of server control, relying on direct remote loading is impossible. To resolve this, you must act as a proxy: fetch the image content within your PHP application and serve it to Dompdf via a local path before generation begins. This approach bypasses the need for allow_url_fopen entirely while adhering to standard security practices.
The most reliable method involves creating an intermediary script that retrieves the remote image and serves it with the correct headers. Let's call this file image.php. When Dompdf requests http://localhost/dompdf/image.php, your server must return the raw binary image data, not an HTML page.
Here is a robust implementation pattern for that intermediary script:
<?php
// image.php - Proxy to fetch remote images securely
$url = $_GET['file']; // e.g., ?file=http://example.com/logo.png
if (!$url) {
http_response_code(400);
exit('Missing file parameter');
}
$ch = curl_init();
// Configure curl strictly to avoid following redirects unintentionally or leaking headers
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // Prevent redirect loops
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Enforce SSL verification for HTTPS
// Fetch the content
$content = curl_exec($ch);
$curl_error = curl_error($ch);
if ($curl_error) {
http_response_code(502);
exit('Curl error: ' . $curl_error);
}
// Determine image type from Content-Type header to set correct disposition
$headers = get_headers($url, 1);
$content_type = 'image/jpeg'; // Default fallback
foreach ($headers as $header) {
if (preg_match('/^Content-Type:\s*(.+)$/i', $header, $matches)) {
$content_type = trim($matches[1]);
}
}
// Disable output buffering to ensure raw bytes are sent immediately
ob_end_clean();
// Serve the image content directly
header("Content-Type: $content_type");
echo $content;
Once this script is in place, you must update your Dompdf configuration to point to this local endpoint instead of the remote URL. In your PDF generation code, locate the DOMPDF_DEFAULT_PAGE_FORMAT, DOMPDF_TEMP_DIR, and DOMPDF_ENABLE_REMOTE settings. Ensure DOMPDF_ENABLE_REMOTE remains true, but change the image source in your HTML template from the external URL to the local proxy:
<img src="http://localhost/dompdf/image.php?file=http://example.com/logo.png" />
By pointing Dompdf to image.php, the library treats it as a local resource. The error message "Image not found" regarding http://localhost/dompdf/image.jpg will disappear because the path is now valid and accessible via HTTP within your domain.
This technique effectively decouples the rendering engine from external network restrictions. You retain full control over what images are fetched, allowing you to implement additional validation or caching logic in image.php if needed. Just remember that this adds a slight overhead: every PDF generation triggers an extra HTTP request to fetch the image data before rendering. For high-frequency, small-batch PDFs, this is usually negligible, but for massive reports with hundreds of images, consider caching the fetched content or processing them as local files during a pre-generation step.
