DomPDF not showing image or using css file

I am generating a PDF using DomPDF. It works great except for two small things…

  1. DomPDF is not showing the only image I add. And that’s a rather essential image seeing how this is an invoice and that image is our logo…
  2. It’s not using the CSS file I created specifically for the PDF. I’m using really simple, CSS2 compliant codes for this (a table row color here, a font color there, nothing crazy since its an invoice).

For both the image and the CSS file I’m not using relative paths but absolute paths.

For the CSS part of this problem, the CSS itself isnt the cause. When I put the CSS in the header it works perfectly

What have I tried:

  • I tried URLs to display the image and access the css file. This had no effect.
  • I tried relative server paths, no effect.
  • I tried absolute server paths, no effect.

Note: <?= esc(FCPATH) ?> is a codeigniter code that gets me the absolute path to my public folder (which contains the css and images folder)

Here is my function that creates the PDF and streams it to the browser to be displayed:

public function htmlToPDF($view, $filename, $data = null){
        $dompdf = new \Dompdf\Dompdf();
        $dompdf->loadHtml(view($view, $data));
        $dompdf->setPaper('A4', 'portrait');
        $dompdf->render();
        $dompdf->stream('DomPDFOut.pdf', array("Attachment" => false));
    }

Here is the HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="<?= esc(FCPATH) ?>css/factuur.css">
</head>
<body>
    <p><?= esc(FCPATH) ?></p>
    <table width="100%" id="header">
        <tr>
            <td width="20%"><img src="<?= esc(FCPATH) ?>images/medical-crossteam-logo-small.png" height="100" /></td>
            <td>
                <strong>*****</strong><br />
                ****<br />
               *****<br /><br />
                
                <strong>Contactpersoon: </strong>****<br />
                *****
            </td>
            <td>
                <strong>KVK:</strong> ******<br />
                <strong>IBAN:</strong> *********<br />
                <strong>Ter name van:</strong> *******<br /><br />
                
                <strong>E-mail:</strong>*********@*******ssteam.nl
            </td>
        </tr>
    </table>
    <h1>Factuur</h1>
    <table width="100%" id="factuurregels">
        <tr>
            <?php $factuur_datum = new Time($factuur['factuur_datum']);?>
            <td><strong>Factuurdatum:</strong></td>
            <td><?= esc($factuur_datum->format('d-m-Y')) ?></td>
            <?php $dtVerval = new Time($factuur['factuur_verval']);?>
            <td><strong>Vervaldatum:</strong></td>
            <td><?= esc($dtVerval->format('d-m-Y')) ?></td>
        </tr>
        <tr>
            <td><strong>Factuurnummer:</strong></td>
            <td colspan="3"><?= esc($factuur['id']) ?></td>
        </tr>
        <tr>
            <td colspan="4">&nbsp;</td>
        </tr>
        <tr>
            <td><strong>Organisatie:</strong></td>
            <td><?= esc($factuur['organisatie_naam']) ?></td>
            <td><strong>Contactpersoon:</strong></td>
            <td><?= esc($factuur['organisatie_contactpersoon']) ?></td>
        </tr>
    </table>
    <table>
        <tr class="tab-row-header-regels">
            <th>Aantal</th>
            <th>Omschrijving</th>
            <th>Prijs p/s</th>
            <th>Uren</th>
            <th>Prijs</th>
        </tr>
        <?php $totaal = 0; ?>
        <?php foreach($factuurregels as $factuurregel): ?>
        <tr>
            <td><?= esc($factuurregel['aantal']) ?></td>
            <td><?= esc($factuurregel['beschrijving']) ?></td>
            <td><?= esc(number_to_currency($factuurregel['bedrag'], 'EUR', null, 2)) ?></td>
            <td><?= esc($factuurregel['uren']) ?></td>
            <?php $subtotaal =  $factuurregel['bedrag'] * $factuurregel['aantal'] * $factuurregel['uren']?>
            <td><?= esc(number_to_currency($subtotaal, 'EUR', null, $fraction = 2)) ?></td>
            <?php $totaal = $totaal + $subtotaal; ?>
        </tr>
        <?php endforeach; ?>
        <tr>
            <th colspan="4">Totaal:</th><td><?= esc(number_to_currency($totaal, 'EUR', null, $fraction = 2)) ?></td>
        </tr>
    </table>
    <p id="ovv">
        Ik verzoek u dit bedrag voor de vervaldatum van deze factuur over te maken op ons IBAN.<br />
        Dit kan onder vermelding van de omschrijving van: <strong><?= esc($factuur['evenement_naam']) ?></strong>
    </p>
    <p>
        <strong>BTW</strong>: ******sTeam is vrijgesteld van het rekenen van BTW.
    </p>
</body>
</html>

Okay, found a solution. For anyone in the future who is struggling with the same problem, here’s how I sloved it.

I replaced:
$dompdf = new \Dompdf\Dompdf();
with:
$dompdf = new \Dompdf\Dompdf(array(‘enable_remote’ => true));

What this does is make it possible to render remote files in the PDF. So now I just replace the absolute path to the image and css files with the URLs for those files. And it works.

The above worked, compared to using $dompdf = new Dompdf([‘chroot’ => DIR]) which produced no effect.

As noted, I also had to go away from local paths to URLs

Sponsor our Newsletter | Privacy Policy | Terms of Service