简体   繁体   English

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

[英]Laravel 5: DOMPDF(0.8.3) header and footer in each page

We are using the dompdf version 0.8.3.我们使用的是 dompdf 版本 0.8.3。 We are using it in our reports, we want to improve our report by adding some header and the page number in the bottom part in every page.我们正在我们的报告中使用它,我们希望通过在每个页面的底部添加一些标题和页码来改进我们的报告。

Currently, we have the page number and we want to have a some header.目前,我们有页码,我们想要一个标题。

Controller控制器

<!-- 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 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>

Question: How can we add some header in each page?问题:我们如何在每个页面中添加一些标题?

This is my controller:这是我的控制器:

    $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));

For header (repeat on each page).对于标题(在每一页上重复)。 Add this in CSS在 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>

Add this in your body (for page number and header)将此添加到您的正文中(用于页码和标题)

<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>

What I did is, I added another page_text and that page_text is for my header.我所做的是,我添加了另一个 page_text 并且 page_text 用于我的标题。

PDF 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM