14

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,

<style>...</style>

and inline styles.

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

The php file to create the pdf looks like this...

<?php 
ob_start(); 
?>
<html>
<head>

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

</head>

<body>
... content (divs, etc) 
</body>
</html>
<?php 
require_once("../../scripts/dompdf/dompdf_config.inc.php"); 
$dompdf = new DOMPDF(); 
$dompdf->load_html(ob_get_clean()); 
$dompdf->render(); 
$dompdf->stream('test.pdf');
?>

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.

Michael
  • 169
  • 2
  • 2
  • 9
  • Your sample links aren't working for me. I don't see any reason why the styles would not be applied. Do you get any PHP errors or notices? Can you post some sample HTML+CSS? – BrianS Aug 02 '13 at 16:33
  • @BrianS, the server (bluehost) was down today. Is back up now and the links are working fine. Sorry about that! – Michael Aug 03 '13 at 03:39
  • I realized after a while it was probably your host. I did eventually get access. – BrianS Aug 05 '13 at 19:26

3 Answers3

5

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.

BrianS
  • 13,284
  • 15
  • 62
  • 125
  • I agree that I should go to tables. I've always preferred divs, but it's no big deal to change it. You mention "sticking with dompdf", is there something else out there that I can use to write pdfs via php code? dompdf was the most popular choice it seemed, so that's what I went with. – Michael Aug 03 '13 at 03:41
  • There are a number of PHP-based libraries: mPDF, TCPDF, html2pdf. And if you want something more powerful you can go with a headless browser executable, like PhantomJS. – BrianS Aug 05 '13 at 19:25
  • 1
    I ended up going with mPDF and it worked great! Much better CSS support, and easy to use. One thing to keep in mind (for future people referencing this thread) is that the 'float' style does NOT work in mPDF with tags (only with
    tags). The reason DOMPDF wasn't working is my CSS was written ".page" instead of "div.page" but that still only fixed SOME of the issues. mPDF wins. Thanks for your answers!
    – Michael Aug 06 '13 at 15:10
  • Sorry to see you go (I'm one of the devs). But ... I totally understand. Other libraries work better in some use cases right now. – BrianS Aug 06 '13 at 15:28
  • @Michael this worked for me too. I moved from dompdf to mPDF. – Krish Oct 08 '20 at 08:56
4

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

below stmt is wrong:

<styles>...</styles>

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.

web2008
  • 914
  • 4
  • 11
2

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

<!DOCTYPE html>
<html lang="en">
  <head>
    <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">
@include('pdf.style')
</style>
  </head>
  <body>

    <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>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <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>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <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>

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

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 eg.

<style>
  @include('style')
</style>    

thats the simple trick worked for me.

Introvert
  • 21
  • 2