Home > PHP > Images not working in dompdf

Images not working in dompdf

What is dompdf?  Well basically its a HTML to PDF converter. It's rendering engine is built in PHP and is style-driven which means it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements.

I won't go into detail of how to use it (unless you want me to or would like to write about it), but recently I  had an issue with images not rendering  in the pdf. At first I thought it was a permissions problem, then I thought it was an image type problem. It turns out it was a file location issue.

instead of the file location being:

<img src="images/myimage.jpg" style="width:200px;height:200px">

It should be:

<img src="/var/www/images/myimage.jpg" style="width:200px;height:200px">

So as you can see it needs to reference the file from the server side file directory and not just from within the web directory.

Wish someone wrote this, when I needed it....

  1. Andrey
    September 1st, 2009 at 02:03 | #1

    Thank you so much for this! I was cracking my head about this issue, thinking it had something to do with the framework I use – symfony. But then I’ve seen your article and – bing – got it!
    The only addition though, in my case I have production and development enviroments on different servers, so I could’t hard-code absolute path. Insead I use php function getcwd() to get absolute path to current directory:

  2. September 1st, 2009 at 22:58 | #2

    No problem.. I was smashing my head against the wall on this one too.

    Glad it helped.

    Thank you for sharing how you fixed your issue.

  3. Kurtis
    November 9th, 2009 at 12:46 | #3

    this is a good one, I had that problem, and now it’s solved, but the include image is way too tiny, even though I put the with and size, I have to zoom at 400% just to see that the image is there, that’s not normal

  4. Unknown
    December 31st, 2009 at 19:01 | #4

    Thanks Guyz. Great help.

  5. Allan
    February 18th, 2010 at 10:20 | #5

    This was a great help to me as well. My web directory is set up differently, and it took a couple tries to get it work. Also, one thing that’s good to remember is DPI. PDF is a print file, and by default in the config file set to 150 DPI. Web DPI is 72. I had to increase all image dimensions by 48% in order to get them to the same dimensions.

  6. Graham
    May 25th, 2010 at 23:13 | #6

    Thanks! Had the same issue, in 0.5 you could put the http:// address in for an image but not the beta!

  7. Emmanuel
    May 26th, 2010 at 07:17 | #7

    I solved the problem by defining the variable base_path:

    $dompdf = new DOMPDF();
    $dompdf->set_base_path(“../path/images/”);

    with

  8. Emmanuel
    May 26th, 2010 at 07:19 | #8

    with –img src=”image.png”–

    the name of image only.

  9. May 26th, 2010 at 15:29 | #9

    Nice work Emmanuel!

  10. May 27th, 2010 at 19:26 | #10

    Thanks Guyz. Great help.

  11. June 16th, 2010 at 11:42 | #11

    If I even attempt to put an img tag in the code, it will not generate the pdf. If I comment out the code with the img tag, it generates the pdf.

    Does anyone know why?

  12. Geo Paul
    August 15th, 2010 at 03:25 | #12

    Actually you can add the tag in html.. and it will work in the pdf generation too.. For this
    1. Either the image should be in the local server and must be referenced like src=”/www/var/..”..
    2. Or it can be an ordinary url like “http://google.com/sdf.jpg”… But for this one to work you should enable the DOMPDF_ENABLE_REMOTE to true.. which will be false by default….
    Change define(“DOMPDF_ENABLE_REMOTE”, false); to define(“DOMPDF_ENABLE_REMOTE”, true);

  13. August 30th, 2010 at 03:02 | #13

    thanks for this hint

    Stephan

  14. Ecco
    January 12th, 2011 at 23:46 | #14

    Thank you thank you thank you thank you :D

  15. Evan
    February 12th, 2011 at 19:58 | #15

    Good job! Saved me a load of time.
    THANKS!

  16. April 18th, 2011 at 05:27 | #16

    @Geo Paul:
    Thanks for the hint.

  17. manish
    May 31st, 2011 at 21:59 | #17

    Gr8 hint.. It saved my brain from just popping out…. I had the images working well in my local system but not on the server… Using getcwd() for the current directory path helped me a lot….

  18. June 21st, 2011 at 09:44 | #18

    Bless You manish =D, i own you a beer

  19. abdullah
    September 7th, 2011 at 23:51 | #19

    I also had this problem and solved it by using absolute image paths instead of relative ones.

    For example, instead of:

    ,

    I changed the html to

  20. Leon
    September 12th, 2011 at 12:40 | #20

    Thank you, this post probably saved me a lot of frustration.

  21. Wayne
    September 26th, 2011 at 04:27 | #21

    Thanks… I was pulling the little hair I left out until I found this page!

    Geo Paul you are a Legend!

  22. Alex
    November 3rd, 2011 at 08:26 | #22

    Thank you guy ! seems I own you a beer too !

  23. Pito
    November 23rd, 2011 at 07:17 | #23

    I am using dompdf to generate the PDF file and swift to email it to my email address. I am able to generate the PDF file with all the information and I get it in my email but I am having issues with images. For some reason I can’t get the images to appear in my PDF file, I get a small red X.

    Here is what I have for my code:

    $val)
    $post->$key = trim(strip_tags($_POST[$key]));

    // Check for blank fields
    if (empty($post->name) OR empty($post->dob) OR empty($post->age) OR empty($post->address) OR empty($post->city) OR empty($post->state) OR empty($post->zip) OR empty($post->email) OR empty($post->phone) OR empty($post->name2) OR empty($post->cell2) OR empty($post->name4) OR empty($post->phone4) OR empty($post->signature))
    $error = true;

    else {

    // Get this directory, to include other files from
    $dir = dirname(__FILE__);

    // Get the contents of the pdf into a variable for later
    ob_start();
    require_once($dir.’/pdf.php’);
    $pdf_html = ob_get_contents();
    ob_end_clean();

    // Load the dompdf files
    require_once($dir.’/dompdf/dompdf_config.inc.php’);

    $dompdf = new DOMPDF(); // Create new instance of dompdf
    $dompdf->load_html($pdf_html); // Load the html
    $dompdf->render(); // Parse the html, convert to PDF
    $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later

    // Get the contents of the HTML email into a variable for later
    ob_start();
    require_once($dir.’/html.php’);
    $html_message = ob_get_contents();
    ob_end_clean();

    // Load the SwiftMailer files
    require_once($dir.’/swift/swift_required.php’);

    $mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer

    $message = Swift_Message::newInstance()
    ->setSubject(‘FODT Registration Form’) // Message subject
    ->setTo(array(‘my email [email protected]‘ => ‘Registration’)) // Array of people to send to
    ->setFrom(array(‘[email protected]’ => ‘FODT’)) // From:
    ->setBody($html_message, ‘text/html’) // Attach that HTML message from earlier
    ->attach(Swift_Attachment::newInstance($pdf_content, ‘DiveRegForm.pdf’, ‘application/pdf’)); // Attach the generated PDF from earlier

    // Send the email, and show user message
    if ($mailer->send($message))
    $success = true;
    else
    $error = true;

    }

    }
    ?>

  24. December 2nd, 2011 at 06:56 | #24

    Hey, thanks for that tip… i was struggeling for 1 hour with this and forgot to check the google… your article was on 1st place … Thank you very very much

  1. No trackbacks yet.
SEO Powered by Platinum SEO from Techblissonline