Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

We are using the dompdf version 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

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

like image 238
Angel Avatar asked Nov 06 '22 14:11

Angel


1 Answers

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

<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>
like image 149
Jay Momaya Avatar answered Nov 15 '22 05:11

Jay Momaya