使用@page选择器将背景图像设置为指定页面


set background image to a specified page with @page selector

我想用mpdf库从html创建一个pdf文件。我想将背景图像设置为渲染pdf的第2页(不是所有页面)。我使用以下代码:

$mpdf=new mPDF(''); 
$html = '
    <body>
    <style>
      @page {
       background: url("../mpdf60/bg1.jpg") 50% 0 no-repeat;
     }
    </style>
    <div style="margin-bottom:50px">&nbsp;</div>
    <div align="center" style=" margin-bottom:350px"><img src="../mpdf60/pdffirst1.jpg" height="100" width="190" alt=""></div>
    <pagebreak />
    <div>
    </div>
    </body>';

在这段代码中,背景图像设置在渲染的pdf的所有页面上(使用@page选择器)。

如何仅为一页(2'nd页)设置背景图像?谢谢

根据文档,mPDF支持命名的@page选择器,因此您可以这样做:

<style>
  @page second {
    background: url("../mpdf60/bg1.jpg") 50% 0 no-repeat;
  }
</style>

然后:

div.second {
  page: second;
}

然后您的第二个页面应该在具有second类的div中。请看第章链接中给出的示例。