Home » Php » php – CSS not working with DOMPDF

php – CSS not working with DOMPDF

Posted by: admin July 12, 2020 Leave a comment


I am using DOMPDF (v 0.5.2) to convert an html page to a pdf file.

The pdf file appears after the PHP script has run (as expected), but no styles are applied to any of the content. As far as I can find, the float property does not work with DOMPDF, so I have to do some work to get around that, but not even the font styles are being applied.

I have tried all three methods of including styles:
attaching a style sheet,

<link href="styles.css" rel="stylesheet" type="text/css">

writing styles in the header,


and inline styles.

<div class="..." style="...">

The php file to create the pdf looks like this…


<link href="flhaha.css" rel="stylesheet" type="text/css">


... content (divs, etc) 
$dompdf = new DOMPDF(); 

There is an image within the content that loads fine and is displayed properly when the pdf opens, but still no styles.

Thanks in advance!

Edit #1: The tags above were meant to be “style”, not “styles”. Have fixed the typo.

How to&Answers:

dompdf v0.5.x does not support floats. v0.6.0 does, though it is still an experimental feature that has to be enabled in the configuration. After testing your document I can say that dompdf doesn’t handle it very well. If you want to stick with dompdf you might consider tables since your document is somewhat tabular in nature.


Internal stylesheet can be created by including the styles in between <style> element not <styles> element.

below stmt is wrong:


It should be :

<style> ... </style>

and inline styles should given as shown below:

<div class="className" style="color:red;text-align: left;">

and external style sheet should be included as shown below:

<link rel="stylesheet" type="text/css" media="screen" href="styles.css" />

If external style sheet is not working then check the source file path.


Simple solution is, Put your css in separate (.php) or any other server scripting file and include it in your pdf file.
You have pdf.blade.php file for pdf

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    {{-- <link rel="icon" href="/favicon.ico"> --}}

    <title>Starter Template for Bootstrap</title>
<style type="text/css">

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          <a class="navbar-brand" href="#">Project name</a>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
        </div><!--/.nav-collapse -->

    <div class="container">

      <div class="starter-template">
        <h1>Bootstrap starter template</h1>
        <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>

    </div><!-- /.container -->

Suppose you have css file style.css, So next step is to copy your css file content and paste it in new style.blade.php
And you almost done!!

Now include it in your pdf file


thats the simple trick worked for me.