Question

I have problem with create PDF and MySQL using HTML2PDF Librairy. I would like to display an image and does not show. Does anybody know how to do that?

For example: I have code below:

bookmark.php

<?php
ob_start();
?>
<style type="text/css">
<!--
    table.page_header {width: 100%; border: none; background-color: #DDDDFF; border-bottom: solid 1mm #AAAADD; padding: 2mm }
    table.page_footer {width: 100%; border: none; background-color: #DDDDFF; border-top: solid 1mm #AAAADD; padding: 2mm}
    h1 {color: #000033}
    h2 {color: #000055}
    h3 {color: #000077}

    div.niveau
    {
        padding-left: 5mm;
    }
-->
</style>
<page backtop="14mm" backbottom="14mm" backleft="10mm" backright="10mm" style="font-size: 12pt">
    <page_header>
        <table class="page_header">
            <tr>
                <td style="width: 100%; text-align: left">
                    <img src=showimage.php alt="image" />
                </td>
            </tr>
        </table>
    </page_header>
    <page_footer>
        <table class="page_footer">
            <tr>
                <td style="width: 100%; text-align: right">
                    page [[page_cu]]/[[page_nb]]
                </td>
            </tr>
        </table>
    </page_footer>
</page>
<?php
    $content = ob_get_clean();

    require_once(dirname(__FILE__).'/../html2pdf.class.php');
    try
    {
        $html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8', 0);
        $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
        $html2pdf->Output('bookmark.pdf');
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }
?>

showimage.php

<?php
mysql_connect("localhost","user","pass");
mysql_select_db("db");
$rs = mysql_query("select * from gallery");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[imgdata];
header("Content-type: image/jpeg");
print $imagebytes;
?>
Was it helpful?

Solution

This is old but since no correct answer was provided, I'll try to pinch in. Html2pdf is made to load image directly from disk not using http. In the current version, you would have received an error saying it can't load the image...

The usual way to link the image would be :

<img src='/path/to/image.png' alt="image" />

instead of:

<img src=showimage.php alt="image" />

In your case, if you don't want to write the image on disk, you could try embedding it as data-uri (I haven't test if html2pdf support this)

echo '<img src="data:image/jpeg;base64,'.base64_encode($row[imgdata]).'"/>';

OTHER TIPS

I wonder whether you get an error message. Maybe it is an idea to make PHP more verbose on error/warning reporting.

Please see error_reporting@php.net. There is also a default value set in your php.ini.

What might be an issue in your showimage.php:

$row = mysql_fetch_assoc($rs);
$imagebytes = $row[imgdata];

mysql_fetch_assoc returns an associative array. Since imgdata is not even a variable in this case you should either change imgdata to $imgdata and init the variable with the column name of your MySQL table holding your image data/blob. If the column name is imgdata, you might just need to put it in quotation marks, like this:

$imagebytes = $row['imgdata'];

On third thought, I might miss here, that you are using imgdata as a constant (then you should consider making the constant identifier all uppercase for better readability) - if that is the case, make sure the constant holds the correct column name to your MySQL table.

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