首页 文章

mPDF:使用背景图像获取整页

提问于
浏览
1

我需要用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 回答

  • 2

    使用 @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;
    }
    
  • 1

    我知道了!正如我后来猜到的那样,我可以使用背景图像作为矢量图像(SVG),我可以这样做 .

    这是代码:

    $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_image.svg')");
    $pdf->SetDefaultBodyCSS('background-image-resize', 6);
    
    $pdf->WriteHTML($cuerpo, 2);
    
    $pdf->Output();
    

    有了 SVG ,它就像一个魅力,它很快!

  • 0

    您在示例文件中查看:example54_new_mPDF_v5-1_features_gradients_and_images.php

    <?php
    
    
    $path = (getenv('MPDF_ROOT')) ? getenv('MPDF_ROOT') : __DIR__;
    require_once $path . '/vendor/autoload.php';
    
    $mpdf = new \Mpdf\Mpdf();
    
    //==============================================================
    $html = '
    <style>
    table {
        background: url(\'assets/bayeux1.jpg\') repeat scroll left top;
    }
    .table1 tr.thisrow1 {
        background-image-resolution: 300dpi;
    
    }
    .table1 tr.thisrow1 td {
        height: 28mm;
    }
    .table1 tr.thisrow2 {
        background-image: none;
        background: -moz-linear-gradient(left, #c7Fdde 20%, #FF0000 );
        background: -webkit-gradient(linear, left bottom, left top, color-stop(0.29, rgb(90,83,12)), color-stop(0.65, rgb(117,117,39)), color-stop(0.83, rgb(153,153,67)));
    }
    </style>
    
    <table class="table1">
    <tbody>
        <tr><td>Row 1</td><td>This is data</td><td>This is data</td></tr>
        <tr class="thisrow1">
            <td>This row has</td>
            <td>a background-image</td>
            <td>of the bayeux tapestry</td>
        </tr>
        <tr><td><p>Row 3</p></td><td><p>This is long data</p></td><td>This is data</td></tr>
        <tr class="thisrow2"><td>This row has</td><td>a gradient set</td><td>which spans all 3 cells</td></tr>
        <tr><td>Row 5</td><td>Also data</td><td>Also data</td></tr>
    </tbody>
    </table>
    
    
    ';
    
    $mpdf->WriteHTML($html);
    
    $mpdf->Output(); exit;
    

相关问题