How to include the external style sheet in dom pdf

How to include the external style sheet in dom pdf

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

Stefan Bogdanescu

Founder & Senior Architect

Integrating External Stylesheets in DomPDF

You cannot simply call $dompdf->set_base_path() and expect your external CSS file to load automatically. The method you are using in your code snippet, set_base_path(), is designed for a specific legacy feature intended to help DomPDF resolve relative paths within the HTML string itself, not to inject an entire <link> tag into the document head.

When you pass an HTML string via load_html() or load_string(), DomPDF creates an internal representation of that document. If your HTML contains a class like class='abc', DomPDF needs a corresponding CSS rule defining .abc to render it correctly. Since you are not embedding the full <style> block in your PHP string, you must manually instruct DomPDF where to find the stylesheet.

The correct approach involves adding an explicit link element to your HTML string before rendering. You need to construct a temporary HTML wrapper that includes both your table content and a reference to your external file. The path you provide must be relative to the location where DomPDF resolves resources, which is often the project root or the specific directory configured in your dompdf_config.inc.php.

Here is how you should modify your code:

require_once "dompdf/dompdf_config.inc.php";

$dompdf = new DOMPDF();

// Define the path to your CSS file relative to the base path
$css_path = 'style.css'; 

$html = '
<html>
<head>
    <link rel="stylesheet" href="' . $css_path . '">
</head>
<body>
    <table>
      <tr>
        <td class="abc">testing table</td>
      </tr>
      <tr>
       <td class="def">Testng header</td>
      </tr>
    </table>
</body>
</html>';

$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("hello.pdf");

In this implementation, the <link> tag is injected directly into the HTML string. This tells the CSS parser inside DomPDF to fetch style.css from the current context. If your file resides in a subdirectory, you must adjust the path accordingly, such as href="styles/main.css".

A common pitfall occurs when developers assume that setting a base path automatically resolves all external assets. The set_base_path() method is useful if you are working with absolute URLs or specific relative paths within your HTML string that need to be anchored to a different directory. However, it does not act as a global CSS loader. If you require dynamic styling based on the environment, you might consider using the CSSImportProcessor class directly, though this adds complexity and is generally unnecessary for simple external file inclusion.

Another consideration is the location of your CSS file relative to where DomPDF is instantiated. DomPDF resolves paths from the base directory defined in dompdf_config.inc.php. If your project structure places the CSS file in a public folder while your PHP scripts sit in an application folder, you may need to adjust the path or use an absolute URL if the document will be viewed externally.

For larger projects built on frameworks like Laravel, managing these paths becomes more critical due to environment-specific directory structures. While specific integration guides for modern frameworks can be found at https://laravelcompany.com, the core principle remains the same: you must explicitly tell the renderer where to look for stylesheets by including them in the document structure or configuring the base path correctly in your configuration file.

Remember that DomPDF does not execute JavaScript or load external resources via AJAX. It is a static rendering engine. Therefore, ensuring your HTML string contains all necessary metadata and resource links before passing it to load_html() is mandatory. If your stylesheet relies on assets like images or fonts defined within the CSS file, those paths must also be resolved correctly relative to your base path configuration.

Our network