Question

I'm trying to create a PDF document from a HTML document. I am using DOMPDF to do this as every other one I found was either too complicated or required me to install something to the web server itself. I just need a very simple HTML to PDF and force download script of some kind. This is my code so far.

It is working almost completely. The ONLY thing that isn't working is the images... For some reason... And it works when I echo the $html variable and load it into a page on it's own.

PHP

include('dompdf/dompdf_config.inc.php');

$html = $html_header . formatFormResultDownload($userid) . $html_footer;

$dompdf = new DOMPDF();
$dompdf->set_paper("a4");
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('User Gallery (' . date('Y-m-d H:i:s') . ').pdf');

HTML

<!DOCTYPE html>
<html>
<head>
    <title>****</title>
</head>
<body>
    <table border="1" cellpadding="10" width="100%">
        <tr>
            <td width="50%"><img src=
            "http://****.net/client/****/images/logo-large.gif"></td>

            <td width="50%"><span><strong>Email</strong>:
            ****@****.net</span><br>
            <span><strong>Company Name</strong>:****</span><br>
            <span><strong>Company Email</strong>:****</span></td>
        </tr>
    </table>

    <table border="1" cellpadding="10" width="100%">
        <tr>
            <td align="center" width="25%"><span><img src=
            "http://****.net/client/****/index.php?option=com_datsogallery&amp;view=sbox&amp;catid=7&amp;id=70635"
            style="width:200px" width="100%"></span><br>
            <br>
            <span><strong>IS Reference</strong>: 70635</span><br>
            <span><strong>Author</strong>: Rob Speakman</span><br></td>

            <td align="center" width="25%"><span><img src=
            "http://****.net/client/****/index.php?option=com_datsogallery&amp;view=sbox&amp;catid=7&amp;id=71173"
            style="width:200px" width="100%"></span><br>
            <br>
            <span><strong>IS Reference</strong>: 71173</span><br>
            <span><strong>Author</strong>: James Hearne</span><br></td>

            <td align="center" width="25%"><span><img src=
            "http://****.net/client/****/index.php?option=com_datsogallery&amp;view=sbox&amp;catid=7&amp;id=71177"
            style="width:200px" width="100%"></span><br>
            <br>
            <span><strong>IS Reference</strong>: 71177</span><br>
            <span><strong>Author</strong>: James Hearne</span><br></td>

            <td align="center" width="25%"><span><img src=
            "http://****.net/client/****/index.php?option=com_datsogallery&amp;view=sbox&amp;catid=7&amp;id=71171"
            style="width:200px" width="100%"></span><br>
            <br>
            <span><strong>IS Reference</strong>: 71171</span><br>
            <span><strong>Author</strong>: Makiko</span><br></td>
        </tr>
    </table>
</body>
</html>

The PDF

enter image description here

Thank you

Was it helpful?

Solution

dompdf considers image paths with a full URL (i.e. one with a domain part such as http://example.com/image.png) to be "remote." There are a few settings that are important when handling remote images:

  1. DOMPDF_ENABLE_REMOTE should be set to true
  2. The account running your script (e.g. your web server) needs read/write access to the folder specified by DOMPDF_TEMP_DIR
  3. dompdf fetches remote images using file_get_contents() which will only work if allow_url_fopen is enabled (some hosts disable this)
  4. dompdf will probably need to use the GD PHP extension for some image processing

It's also important to note that dompdf 0.5.x has problems with dynamically-generated images because it relies heavily on extension for image type detection. You can try to trick it by appending a filename to the end of the URL, but this doesn't always work. The dynamic image problem was addressed in version 0.6.x (currently in beta, but still recommended).

Finally, if you're using dompdf 0.6.x check out the setup file (dompdf/www/setup.php) which will let you know if there are any problems with your installation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top