我需要用mPDF生成发票,它们都有一个页眉,一个页脚,以及必须覆盖整个页面的相同背景图像 .
我在网上看过其他解决方案,但没有一个对我有用 .
这些是多次尝试,所有这些都导致失败(图像未显示,50 *错误或图像覆盖部分内容使用图像时):
1- Using external CSS file and injecting it as CSS content
$pdf = new mPDF('es', 'A4', 0, '', 5, 5, 0, 0, 0, 0, 'P');
$pdf->useSubstitutions=false;
$pdf->setAutoTopMargin = 'stretch';
$pdf->SetDisplayMode('fullpage');
$css = file_get_contents('./styles.css');
$cabecera = file_get_contents('./header.html');
$cuerpo = file_get_contents('./body.html');
$pie = file_get_contents('./footer.html');
$pdf->SetHTMLHeader($cabecera);
$pdf->SetHTMLFooter($pie);
$pdf->WriteHTML($css, 1);
$pdf->WriteHTML($cuerpo, 2);
$pdf->Output();
和styles.css文件:
body {
background-image: url('background_300dpi.png') ;
}
这会引发超时错误 . 如果我使用低分辨率图像背景,它可以工作,但它打破了我的布局 .
2- Trying to set the background image with SetDefaultBodyCSS
$pdf = new mPDF('es', 'A4', 0, '', 5, 5, 0, 0, 0, 0, 'P');
$pdf->useSubstitutions=false;
$pdf->setAutoTopMargin = 'stretch';
$pdf->SetDisplayMode('fullpage');
$cabecera = file_get_contents('./cabecera.html');
$cuerpo = file_get_contents('./cuerpo.html');
$pie = file_get_contents('./pie.html');
$pdf->SetHTMLHeader($cabecera);
$pdf->SetHTMLFooter($pie);
$pdf->SetDefaultBodyCSS('background', "url('background_300dpi.png')");
$pdf->SetDefaultBodyCSS('background-image-resize', 6);
$pdf->WriteHTML($cuerpo, 2);
$pdf->Output();
超时错误也是如此 . 如果我使用低分辨率图像,它确实有效 . 但这不是一个很好的打印解决方案,因为信头中有小写字母包含在背景图像中 .
3 - Trying to use images (the background image is split into a top letterhead and a bottom image) instead of an only background image, and setting margin constraints to false
$pdf = new mPDF('es', 'A4', 0, '', 5, 5, 0, 0, 0, 0, 'P');
$pdf->useSubstitutions=false;
$pdf->setAutoTopMargin = 'stretch';
$pdf->SetDisplayMode('fullpage');
$cabecera = file_get_contents('./cabecera.html');
$cuerpo = file_get_contents('./cuerpo.html');
$pie = file_get_contents('./pie.html');
$pdf->SetHTMLHeader($cabecera);
$pdf->SetHTMLFooter($pie);
$pdf->WriteHTML($cuerpo, 2);
$pdf->Image('miramar/background_top.png', 0, 0, 210, 28.5, 'png', '', true, false);
$pdf->Image('miramar/background_bottom.png', 0, 259, 210, 38, 'png', '', true, false, false, false);
$pdf->Output();
在这种情况下没有超时,但页脚图像使得页脚中包含的某些内容消失了 .
任何人都知道如何实现它?
另一个问题:mPDF支持 vector images 的背景吗?我可能会将背景图像用作SVG文件 .
3 回答
使用
@page
CSS选择器而不是body
来设置背景 . 然后添加属性background-image-resize: 6;
以确保图像覆盖每页的整个宽度和高度 .在styles.css中:
我知道了!正如我后来猜到的那样,我可以使用背景图像作为矢量图像(SVG),我可以这样做 .
这是代码:
有了 SVG ,它就像一个魅力,它很快!
您在示例文件中查看:example54_new_mPDF_v5-1_features_gradients_and_images.php