Angel
Angel

Reputation: 1009

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?

Upvotes: 5

Views: 8728

Answers (2)

Jay Momaya
Jay Momaya

Reputation: 2049

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>

Upvotes: 3

Angel
Angel

Reputation: 1009

What I did is, I added another page_text and that page_text is for my header.

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>

Upvotes: 0

Related Questions