8.3: Local fallback for external stylesheets is inverted in CssLoader (basepathIsLocal check negated) — CSS silently dropped when remote fetch fails

Open Beginner friendly
#2,244 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 5 days

@hemreduru is already working on this.

Since Nov 22, 2026.

  • #2251 by @hemreduru — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
backend

Research direction

Start in src/Css/CssLoader.php at the fallback around line 51 and compare its basepathIsLocal condition with the earlier behavior described in the issue. Check for existing tests covering local fallback when an external stylesheet fetch fails. Done means the fallback maps the URL to a local file only when the base path is local, and the stylesheet loads after the remote fetch fails.

Written by the indexing model from the issue text.

Description

Guidelines
Description of the bug

Since 8.3.0, external stylesheets referenced by a same-host absolute URL are no longer read from the local filesystem when the remote fetch fails. The stylesheet is silently dropped (unless debug is on), and the PDF renders with no CSS. This worked in 8.2.7.

The cause appears to be an inverted condition introduced when CssManager was refactored and stylesheet loading moved to Css\CssLoader (commit eeae96b3, "Refactor CssManager").

8.2.7 — src/CssManager.php

$CSSextblock = $this->assetFetcher->fetchDataFromPath($path);

if (!$CSSextblock) {
    $path = $this->normalizePath($path);
    $CSSextblock = $this->assetFetcher->fetchDataFromPath($path);
}

// ...

private function normalizePath($path)
{
    if ($this->mpdf->basepathIsLocal) {
        // maps https://host/path to $_SERVER['DOCUMENT_ROOT'] . /path
    }
    return $path;
}

The fallback is converted to a local path only when basepathIsLocal is true.

8.3.x — src/Css/CssLoader.php (still present on development, line 51)

$data = $this->assetFetcher->fetchDataFromPath($path);
if (!$data) {
    $path = !$this->mpdf->basepathIsLocal ? Path::normalizeLocalFilePath($path) : $path;
    $data = $this->assetFetcher->fetchDataFromPath($path);
}

The condition is negated. When the base path is local (the exact case the fallback exists for), the path is left unchanged, so the second attempt repeats the same failed remote request. When the base path is not local, a remote URL is mapped onto DOCUMENT_ROOT, which doesn't make sense.

When it shows up

The first (remote) fetch has to fail for this to be visible, so many setups never notice. It fails, for example, when:

  • PHP has no CA bundle configured (the default on Windows PHP builds): cURL error: "SSL certificate OpenSSL verify result: unable to get local issuer certificate (20)"
  • the server cannot reach its own public hostname (hairpin NAT, split DNS, containers)
  • the site uses a self-signed or internal certificate
  • the site sits behind auth or a WAF that blocks the server's own requests

In these cases 8.2.7 recovered by reading the file from disk; 8.3.x renders without the stylesheet.

Suggested fix

- $path = !$this->mpdf->basepathIsLocal ? Path::normalizeLocalFilePath($path) : $path;
+ $path = $this->mpdf->basepathIsLocal ? Path::normalizeLocalFilePath($path) : $path;

Related: #234 (prefer local disk over a remote fetch for local stylesheets).

Found via InvoicePlane 1.7.2, which bundles mPDF 8.3.1. Its PDF templates link their stylesheets with absolute base_url() URLs, and every generated invoice lost its styling after the upgrade.

mPDF version

8.3.1 (same line still present on the development branch). Last working version: 8.2.7

PHP Version and environment (server type, cli provider etc., enclosing libraries and their respective versions)

PHP 8.3.36 NTS x64 (FastCGI), IIS 10 on Windows Server 2022, no curl.cainfo / openssl.cafile set (Windows default). Enclosing app: InvoicePlane 1.7.2 (CodeIgniter 3). Expected on any OS where the remote fetch of a same-host stylesheet fails.

Reproducible PHP+CSS+HTML snippet suffering by the error

Run as a web request (needs $_SERVER['DOCUMENT_ROOT'] / HTTP_HOST, so basepathIsLocal is true), with style.css present in the document root and the remote fetch failing, e.g. HTTPS with no CA bundle configured:

<?php
require __DIR__ . '/vendor/autoload.php';

// style.css in the document root contains:
// body { font-family: sans-serif; color: #c00; }
// table { border-collapse: collapse; } td { border: 1px solid #000; padding: 10px; }

$url = 'https://' . $_SERVER['HTTP_HOST'] . '/style.css';

$mpdf = new \Mpdf\Mpdf(['tempDir' => sys_get_temp_dir()]);
$mpdf->debug = true; // without this the stylesheet is dropped silently

$mpdf->WriteHTML(
    '<html><head><link rel="stylesheet" href="' . $url . '"></head>'
  . '<body><p>Styled?</p><table><tr><td>A</td><td>B</td></tr></table></body></html>'
);
$mpdf->Output();

Expected (as in 8.2.7): the remote fetch fails, the fallback reads DOCUMENT_ROOT/style.css, and the PDF is red sans-serif with bordered cells.

Actual (8.3.1): the remote fetch fails, the fallback repeats the same remote URL, and you get the MpdfException above (debug on) or an unstyled serif PDF (debug off).

Configuring curl.cainfo makes the remote fetch succeed and hides the problem, but doesn't fix the inverted fallback.

Dominant language
PHP
Stars
4.7k
Forks
1.1k
Avg merge
6d 11h
Merged PRs (30d)
4

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mpdf/mpdf

All issues in mpdf/mpdf

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.