How to display images in DOM PDF using Php

As you all know DOM PDF is a great library by which you can create PDF’s directly from Php code by using writing minimum lines of code.

Though there are lot of answers in stackoverflow which suggest you to use remote urls most of them wrong.The best solution I found for displaying images is to convert the image into Base64 and display them as base64 when the PDF is generated. The below code will explain about how to do it..

require_once 'dompdf/';
use Dompdf\Dompdf';
use Dompdf\Options';

//Getting image
$imgpath='<img src="data:image/png;base64, '.$dataBase64.'">';


//Setting options
$options=new Options();
$options->set('defaultFont', 'Helvetica');
$options->set('tempDir','C:\xxx\htdocs\'); //Any directory)

$dompdf=new Dompdf($options);



In the above code the 2 important steps are

  • Converting the image into Base64 data
  • Then setting up temporary directory

Even if one of them is not followed then you may not see the desired output

Scroll to Top