dompdf/dompdf · error · Dompdf\Exception

File '$file' not found.

Error message

File '$file' not found.

What it means

The URI passed the protocol whitelist and all rules, but the actual read failed: Helpers::getFileContent() returned null. That means the file does not exist or cannot be read — wrong path, missing file, HTTP 404/5xx, DNS failure, SSL problem, allow_url_fopen disabled for remote URLs, or OS-level permission denial. This is the generic 'content could not be loaded' endpoint of loadHtmlFile().

Source

Thrown at src/Dompdf.php:376

        }

        if ($protocol === "file://") {
            $ext = strtolower(pathinfo($uri, PATHINFO_EXTENSION));
            if (!in_array($ext, $this->allowedLocalFileExtensions)) {
                throw new Exception("Permission denied on $file: The file extension is forbidden.");
            }
        }

        foreach ($allowed_protocols[$protocol]["rules"] as $rule) {
            [$result, $message] = $rule($uri);
            if (!$result) {
                throw new Exception("Error loading $file: $message");
            }
        }

        [$contents, $http_response_header] = Helpers::getFileContent($uri, $this->options->getHttpContext());
        if ($contents === null) {
            throw new Exception("File '$file' not found.");
        }

        // See http://the-stickman.com/web-development/php/getting-http-response-headers-when-using-file_get_contents/
        if (isset($http_response_header)) {
            foreach ($http_response_header as $_header) {
                if (preg_match("@Content-Type:\s*[\w/]+;\s*?charset=([^\s]+)@i", $_header, $matches)) {
                    $encoding = strtoupper($matches[1]);
                    break;
                }
            }
        }

        $this->restorePhpConfig();

        $this->loadHtml($contents, $encoding);
    }

    /**

View on GitHub (pinned to b14267808b)

Solutions

  1. Verify the source loads from the same environment first: curl the URL or is_readable/file_exists the path as the web-server user.
  2. For remote documents, enable allow_url_fopen or fetch with cURL (handling auth/TLS/redirects) and pass the body to loadHtml().
  3. Fix permissions on local files (read access for the PHP process user).
  4. If the document is generated asynchronously, ensure it exists (or wait/retry) before rendering.

Example fix

// before
$dompdf->loadHtmlFile('https://internal.example/report'); // may 404 or need auth

// after: fetch explicitly with your own HTTP client, then load the string
$resp = $http->request('GET', 'https://internal.example/report');
if ($resp->getStatusCode() !== 200) {
    throw new RuntimeException('Report source unavailable: HTTP ' . $resp->getStatusCode());
}
$dompdf->loadHtml((string) $resp->getBody());
Defensive patterns

Strategy: validation

Validate before calling

if (parse_url($file, PHP_URL_SCHEME) !== null) {
    // remote: check reachability with your HTTP client first
    $status = headRequest($file);
    if ($status !== 200) {
        throw new RuntimeException("HTML source returned HTTP $status: $file");
    }
} elseif (!is_file($file) || !is_readable($file)) {
    throw new RuntimeException("HTML source missing or unreadable: $file");
}
$dompdf->loadHtmlFile($file);

Try / catch

try {
    $dompdf->loadHtmlFile($file);
} catch (\Dompdf\Exception $e) {
    if (strpos($e->getMessage(), 'not found') !== false) {
        // distinguish local-missing vs remote failure for the user; do not blind-retry
    }
    throw $e;
}

Prevention

When it happens

Trigger: $dompdf->loadHtmlFile('https://example.com/missing.html') (404); a local path that does not exist; allow_url_fopen = Off in php.ini while loading http(s) URLs; a self-signed certificate failing TLS verification; the web server user lacking read permission on the file.

Common situations: Passing remote URLs that redirect or require auth; environments disabling allow_url_fopen for security; typos in paths; race where the file is generated asynchronously and not yet present when rendering starts.

Related errors


AI-assisted analysis of dompdf/dompdf@b14267808b (2026-08-21). Data as JSON: /api/errors/18881a38c8a29d18. Report an issue: GitHub.