8

For some reason, the background image doesn't show in dompdf no matter what I try. I've put the direct url as well. None of them worked. Can someone with dompdf experience tell me what I need to do?

It's important to note that other images appear just fine. It's only the bakground images that are causing issues. Here is one of the background images:

body{
 font-size:15px;
 background:url(bg.jpg) repeat-x bottom left;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Refiking
  • 641
  • 3
  • 10
  • 18

5 Answers5

6

Using DOMODF 0.5.1? It's probably having trouble parsing the background shorthand. You could try breaking up the generic background property into the specific properties:

background-image: url(bg.jpg);
background-repeat: repeat-x;
background-position: bottom left;

I also found that sometimes I had to supply a full URL (http://example.com/bg.jpg).

The handling of this property is a little buggy in that version of DOMPDF. You might consider upgrading to the 0.6.0 code base (currently at beta 1). It has a number of improvements over the previous release.

BrianS
  • 13,284
  • 15
  • 62
  • 125
  • Following up (since this has gotten some activity). The background shorthand should be parsed correctly in the newer releases. There is a stable release of the 0.6.x branch ([v0.6.1](https://github.com/dompdf/dompdf/releases/tag/v0.6.1)) and the follow-up ([v0.7.0](https://github.com/dompdf/dompdf/releases/tag/v0.7.0-beta)) is in beta. – BrianS May 08 '15 at 17:42
2

Well, I had the same issue, in my local XAMMP (windows) it worked as it should, but when moving to a live server background images didn't show up.

After looking for a solution without success, finally solved it.

This wasn't working:

body{ font-size:15px; background-image:url(<?php echo getcwd(); ?>/bg.jpg); background-repeat:repeat;}

This worked like a charm:

body{ font-size:15px; background-image:url(./bg.jpg); background-repeat:repeat; }

I hope this can help.

Cristian
  • 21
  • 1
1

for some odd reason I had the same problem dompdf ver 0.6.1

  1. in the img tags the full local path was needed
  2. as a background image the relative path worked only, but background-image set in the style tag didn't work

hope that helps

stefan
  • 2,685
  • 2
  • 24
  • 31
1

It looks like DOMPDF supports background images, so I have to assume that the filepath is wrong.

If you add <img src="bg.jpg" alt="" /> does it work?

I've found in the last project I used DOMPDF I needed to point the image file to it on the filesystem (not a relative path to webroot), and I had to use a root path that ended up looking like this

<img src="<?php echo DOCROOT; ?>assets/images/bg.jpg" alt="" />

Where DOCROOT became /users/me/public_html/. To test it as plain HTML before I sent to DOMPDF, I did a str_replace() to change the DOCROOT to / (relative to my path).

alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    direct img tags work fine. it's only the background images that don't work. the file path is correct because when I view the html, it works. when I take that same exact html and render it through dompdf, it doesn't work – Refiking Jul 26 '10 at 00:11
  • @Refiking That is what I was saying, I've had many times where I view the HTML and it is fine, but I need to change the path to the image once it needs to be rendered with DOMPDF to get it to work. – alex Jul 26 '10 at 00:29
  • If that's the case, then why can I see the other images just fine, but can't see the background image if they have the same path? – Refiking Jul 26 '10 at 00:48
0

In your config file, set DOMPDF_ENABLE_REMOTE to true.

Then in your page or style sheet, use the full path (i.e., "http://yoursite.com/images/imagename.png") in the src tag or the url() in the style sheet.

Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
Jason
  • 11