Add CSS to DomPDF

Add CSS to DomPDF

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

Stefan Bogdanescu

Founder & Senior Architect

Integrating External CSS with DomPDF in Laravel

Exporting HTML reports to PDF is a common requirement in web applications, but bridging the gap between modern browser rendering and the isolated environment of DomPDF often presents specific challenges. The core issue you are facing stems from how DomPDF handles resources: it does not automatically inherit stylesheets linked via <link> tags in parent templates or external assets loaded at a higher level of your application hierarchy. When you render a partial like user_table.html.twig inside a larger layout that imports Bootstrap or custom CSS, DomPDF sees only the raw HTML string passed to it, effectively discarding the context of the surrounding page's resource links.

To resolve this, you must explicitly inject the stylesheet definitions into the DOM before rendering. The most robust approach is to load the external CSS file directly within the Twig template used for rendering, rather than relying on browser-level linking. This ensures that DomPDF has access to the styles during its internal parsing phase.

You can achieve this by using Twig's embed or include functionality alongside the css filter if your project uses a specific loader, but the most direct method involves loading the raw file content and injecting it into the HTML stream. Since you are already retrieving the view with $this->renderView, you have control over the context passed to that template.

Here is a practical implementation strategy using the embed directive or by manipulating the Options object to load extra CSS:

$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
// Add the external CSS path directly via Options
$pdfOptions->set('isRemoteEnabled', true); // Required for loading external files if they are on different domains or paths
$pdfOptions->set('tempDir', sys_get_temp_dir());

$dompdf = new Dompdf($pdfOptions);

// Load your Twig view. 
// Ensure the template includes the CSS link explicitly, OR embed the raw content.
$html = $this->renderView('dashboard/user_table.html.twig', [
    'users' => $users
]);

// If the external CSS file is known (e.g., /public/css/bootstrap.css),
// you can manually inject it into the HTML string before loading DomPDF.
$cssContent = file_get_contents(public_path('css/bootstrap.min.css')); 
$html = str_replace('<head>', '<head>' . $cssContent, $html);

$dompdf->loadHtml($html);
$dompdf->setPaper('A3', 'landscape');
$dompdf->render();

However, modifying the HTML string directly is fragile and can break if the template structure changes. A cleaner architecture involves passing the full URL of your external stylesheets to the view and letting Twig handle the inclusion within the head section of the specific partial you are rendering for PDF export.

If you cannot modify the user_table.html.twig file itself, you must ensure that the CSS is available via a relative path that DomPDF can resolve. DomPDF requires isRemoteEnabled to be set to true if your CSS files reside on a different host or subdomain, though for same-origin assets, it attempts to fetch them automatically once the <link> tag is present in the loaded HTML.

The limitation here is that DomPDF does not traverse the application's layout inheritance system (e.g., extends base.html.twig where the CSS is imported). It only processes the HTML string provided to loadHtml(). Therefore, the stylesheet must be physically present in the HTML string passed to DomPDF.

If you are using Bootstrap, ensure you are loading the version that matches your project's requirements. Older versions of DomPDF might struggle with complex CSS3 features found in newer Bootstrap iterations. In such cases, consider compiling a specific stylesheet for PDF export that targets only the necessary classes (like .table, .text-center) and injecting that minimal file. This reduces the risk of rendering errors caused by unsupported CSS properties while maintaining visual consistency.

Finally, remember to set $pdfOptions->set('isRemoteEnabled', true) if your CSS files are hosted on a different domain or CDN. Without this flag, DomPDF will silently ignore external stylesheets due to security restrictions regarding remote resource loading. Always test the rendered PDF in a real-world scenario to verify that font fallbacks and layout shifts do not occur when the browser's cached styles are absent.

Our network