Search code examples
pdf-generationdompdfhtml2canvas

passing the image to dompdf


I have the following code in my save.php:

<?php 
require_once("dompdf/dompdf_config.inc.php");

$LgPath=$_POST['image'];
header("Content-Transfer-Encoding: binary");
header("Content-Type: image/png");
//header("Content-Disposition: attachment; filename=image.png");

$html = file_get_contents($LgPath);
//$html = readfile($LgPath); --only shows the image

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>

I take succesfully using HTML2CANVAS the print screen of some element, and then i pass it to the save.php page using $POST. I want that using DOMPDF to put that image in the sample.pdf. Using this code I only have this text in the sample.pdf "‰PNG", and I have don't figure how to fix it. Using readfile i see the image, but that's it... Any suggestions? Thank you.


Solution

  • You're reading your image into a variable and passing it to dompdf. As such dompdf is treating that text that makes up that image as HTML source. Since your image is readable from the PHP script at the location stored in $LgPath you should be able to do something like this:

    <?php
    require_once("dompdf/dompdf_config.inc.php");
    $LgPath=$_POST['image'];
    $html = '<img src="' . $LgPath . '">';
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $dompdf->stream("sample.pdf");
    ?>