Laravel 5:每页中的DOMPDF(0.8.3)页眉和页脚

时间:2019-08-26 05:10:16

标签: php laravel dompdf export-to-pdf

我们正在使用dompdf版本0.8.3。我们正在报表中使用它,我们希望通过在每页底部的顶部添加一些页眉和页码来改进报表。

当前,我们有页码,我们想要一些标题。

控制器

<!-- My other function/data -->
$pdf = PDF::loadView('purchase-order.export.export-pdf', $data)->setPaper('a4');
$pdf->getDomPDF()->set_option("enable_php", true);
return $pdf->stream('purchase-order-'.$purchase_order->number.'.pdf');

PDF

@extends('layouts.formpdf')

@section('content')

<div id="page-wrap">
    <script type="text/php">
        if (isset($pdf)) {
            $x = 550;
            $y = 800;
            $text = "{PAGE_NUM}";
            $font = null;
            $size = 12;
            $color = array(255,0,0);
            $word_space = 0.0;  //  default
            $char_space = 0.0;  //  default
            $angle = 0.0;   //  default
            $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
        }
    </script>
.......
</div>

问题:我们如何在每个页面中添加一些标题?

2 个答案:

答案 0 :(得分:0)

我所做的是,我添加了另一个page_text,而该page_text用于我的标题。

PDF

<script type="text/php">
    if (isset($pdf)) {
        $x = 550;
        $y = 800;
        $text = "{PAGE_NUM} of {PAGE_COUNT}";
        $font = null;
        $size = 12;
        $color = array(255,0,0);
        $word_space = 0.0;  //  default
        $char_space = 0.0;  //  default
        $angle = 0.0;   //  default
        // header
        $pdf->page_text(250,10,'HEADER: GGG',$font,$size,$color,$word_space,$char_space,$angle);
        // footer
        $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
    }
</script>

答案 1 :(得分:0)

这是我的控制者:

    $pdf = \PDF::loadView('shipment.shipment.invoice', compact('invoice', 'invoice_taxes'));
    $output = $pdf->output();
    $pdf->save(public_path('\pdf\Invoices').'\Invoice'.$invoice->id.'.pdf');
    return $pdf->stream(public_path('\pdf\Invoices').'\Invoice'.$invoice->id.'.pdf', compact($pdf));

用于标题(在每个页面上重复)。在CSS中添加

<style>
    @page { margin-top: 120px; margin-bottom: 120px}
    header { position: fixed; left: 0px; top: -90px; right: 0px; height: 150px; text-align: center; }
    #footer { position: fixed; left: 0px; bottom: -145px; right: 0px; height: 150px; }
</style>

将此添加到您的体内(用于页码和标题)

<body>
    <script type="text/php">
        if ( isset($pdf) ) {
            $y = $pdf->get_height() - 20; 
            $x = $pdf->get_width() - 15 - 50;
            $pdf->page_text($x, $y, "Page No: {PAGE_NUM} of {PAGE_COUNT}", '', 8, array(0,0,0));
        }
    </script> 
    <header style="width: 100%; margin-top: 0px; margin-bottom: 10px;">
        <img src="{{ public_path('img/invoice_header.png') }}" alt="header" style="width: 100%;">
    </header>
    <footer style="width: 100%;" id="footer">
        <img src="{{ public_path('img/invoice_footer.png') }}" alt="footer" style="width: 100%;">
    </footer>
    <div>
         Rest of the content from here
    </div>
</body>