Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMPDF Background Displaying Incorrect Size

Tags:

php

dompdf

I am trying to put together a little contact form for my 3 year old son who has leukemia. I want to use it so people can send him notes of encouragement, and we can print them out and hang them in his room. I've got the form working and posting the data using DOMpdf. My issue is, the background image I am trying to use as the "stationary", refuses to scale out to 100% and regardless of what size I make it in my graphic software, it is not working. Any help is appreciated on what I am missing. Code below. For example, see: http://bebrave.me/#contact

<head>
<style>
  body { background-image: url(http://www.bebrave.me/test.jpg); background-position: top left; background-repeat: no-repeat;
  background-size: 100%;
  margin-top:300px;
  width:612px;
  height:792px;
  }
</style>

</head>

<body>

<table width="500px"  border="0" align="center" cellpadding="0" cellspacing="0" id="Table">
<tr>
<td align="left">Dear Joshua,<br />
<?php echo $post->Message; ?></td>
</tr>
<tr>
<td align="right">Be Brave!<br />
-<?php echo $post->Name; ?></td></tr>
</table>

</body>

like image 972
Jeremy Chambers Avatar asked Apr 25 '13 04:04

Jeremy Chambers


People also ask

Why does dompdf ignore default size and use letter?

It does look like dompdf ignores the default size and uses letter. The default is probably being superseded during initialization. I've posted a bug report, which you can follow if you want to know when the issue is addressed.

Should PDF have watermark image at background of document?

As client's requirement pdf should have watermark image at background of document. I have tried many ways in coding and also differnt css style but its not helpful. Is anybody has any idea about it? please Help me out. Show activity on this post. Which version of DOMPDF?

Does dompdf support alpha transparency?

Show activity on this post. dompdf v0.5.1 does not support alpha transparency. The most recent release ( v0.6.0) does, as you can see here on the dompdf demo site. Thanks for contributing an answer to Stack Overflow!

Is dompdf a good library to use?

In fact after a quick look through the dompdf.cls.php file you can see that at line 911 the supported labels are shown which are 'author', 'keywords' and 'description'. But keeping DOMPDF as the creator is a good way of supporting such a fantastic and free library.


Video Answer


2 Answers

A few notes, these apply if you're using dompdf 0.6.0 beta 3 or the latest from github:

  1. the background-size property is not yet supported in dompdf
  2. you're adding a margin to the body, so the content of the body (including any background image) will be start after the margin is applied
  3. dompdf applies a default page margin of 1.2cm
  4. the default DPI in later versions of dompdf is 96. It's probably a bit easier to get a handle on the dimensions and placement if you drop that back to 72 so that it matches the PDF standard. If you use any other DPI dompdf will perform translation during the render.

dompdf does appear to diverge from modern browsers in regards to the application of the background image. This is probably something the dev team will want to look at in the future. However, since you're targeting dompdf you'll have to take into account its quirks.

Here's a re-written document that appears to do what you want. It's targeting the latest code available on github which I recommend you use considering the many improvements it includes. If you need to target a different release let me know and I can tweak the HTML.

<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Denk+One' rel='stylesheet' type='text/css'>
<style>
  @page { margin: 0in; }
  body {
    font-family: Denk One, sans-serif;
    background-image: url(http://www.bebrave.me/test.jpg);
    background-position: top left;
    background-repeat: no-repeat;
    background-size: 100%;
    padding: 300px 100px 10px 100px;
    width:100%;
    height:100%;
  }
  p {
    width: 400px;
    margin: auto;
  }
  .signature {
    margin-top: 4em;
    text-align: right;
  }
</style>

</head>

<body>

<p>
  Dear Joshua,<br />
  You may not understand all that's happening. Sometimes, you may not even care.
  Maybe you just want to do what you have to do to get better so you can
  go back to being a three-year-old ... growing, learning, playing, loving.
  It may be hard to understand, but you are a hero to your family and friends. You
  are a hero to them because you show strength in the face of something so 
  scary. Stay strong, be happy, and and get better.
</p>

<p class="signature">
  Be Brave!<br />
  -BrianS
</p>

</body>
</html>
like image 118
BrianS Avatar answered Oct 03 '22 19:10

BrianS


The resolution of your image should be 96 pixels per inch (PPI).

For example:

Format A4: Resolution 96ppi and 210mm x 297mm or 297mm x 210mm (horizontal or vertical, respectively)

like image 25
user3481636 Avatar answered Oct 03 '22 19:10

user3481636