php - DOMPDF 背景显示不正确的大小

标签 php dompdf

我正在尝试为我患有白血病的 3 岁儿子整理一份联系表。我想用它让人们可以寄给他鼓励的字条,我们可以打印出来卡在他的房间里。我已经使用 DOMpdf 使表单正常工作并发布数据。我的问题是,我尝试用作“固定”的背景图像拒绝缩放到 100%,并且无论我在图形软件中将其制作成多大,它都无法正常工作。对于我所缺少的,任何帮助表示赞赏。下面的代码。例如,参见: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>

最佳答案

一些注意事项,如果您使用的是 dompdf 0.6.0 beta 3 或来自 github 的最新版本,则这些注意事项适用:

  1. dompdf 尚不支持 background-size 属性
  2. 您正在为正文添加边距,因此正文的内容(包括任何背景图像)将在应用边距后开始显示
  3. dompdf 应用 1.2cm 的默认页边距
  4. 在更高版本的 dompdf 中默认的 DPI 是 96。如果您将其降回 72 以使其符合 PDF 标准,则可能更容易控制尺寸和位置。如果您使用任何其他 DPI,dompdf 将在渲染期间执行翻译。

dompdf 在背景图像的应用方面确实与现代浏览器不同。这可能是开发团队将来希望看到的东西。但是,由于您的目标是 dompdf,因此您必须考虑它的怪癖。

这是一份重写的文档,似乎可以满足您的要求。它针对 github 上可用的最新代码考虑到它包含的许多改进,我建议您使用它。如果您需要针对不同的版本,请告诉我,我可以调整 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>

关于php - DOMPDF 背景显示不正确的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16206267/

相关文章:

codeigniter - 在dompdf中添加带有页码的页眉和页脚

php - DOMPDF渲染动态表缓慢的解决方案

css - dompdf 内部分页 : avoid on table not working

PHP 按姓氏和中间名排序

javascript - 在不同服务器上使用 PHP 脚本从数据库中检索数据

php - Mac os x 10.9 上的 MYSQL #1045 错误(使用我的密码)

php - 使用 PHP 爆炸来自 mysql 的数据

php - 无法通过一条删除语句从多个 MySQL 表中删除数据

php - Dompdf 错误 : "The Row #4 could not be found" when converting PHP file to PDF

javascript - 使用 hyphenator.js 在 dompdf 中进行连字符