gpt4 book ai didi

php - mPDF:为整页使用背景图像

转载 作者:行者123 更新时间:2023-12-03 11:22:37 27 4
gpt4 key购买 nike

我需要使用 mPDF 生成发票,它们都有页眉、页脚和必须覆盖整个页面的相同背景图像。

我在网上看到了其他解决方案,但没有一个对我有用。

这是几次尝试,都导致失败(图像不显示、50* 错误或使用图像时图像覆盖部分内容):

1- 使用外部 CSS 文件并将其作为 CSS 内容注入(inject)

$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- 尝试使用 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 - 尝试使用图像(背景图像分为顶部信笺和底部图像)而不是唯一的背景图像,并将边距限制设置为 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是否支持 矢量图像 背景?我可能会将背景图像用作 SVG 文件。

最佳答案

使用 @page CSS 选择器而不是 body设置背景。然后添加属性 background-image-resize: 6;以确保图像覆盖每页的整个宽度和高度。

在styles.css 中:

@page {
background: url('/full/path/to/image/background_300dpi.png') no-repeat 0 0;
background-image-resize: 6;
}

关于php - mPDF:为整页使用背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50275998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com