0

I am using MPDF in LARAVEL to create PDFs using an HTML template.

I just want to add the header at the start of the PDF and the Footer at only the End of the PDF.

$mpdf = new \Mpdf\Mpdf( [ 'mode' => 'en-GB', 'format' => 'A4', 'default_font_size' => "", 'default_font' => "", 'margin_left' => $margin_left, 'margin_right' => $margin_right, 'margin_top' => $margin_top, 'margin_bottom' => $margin_bottom,'margin_footer' => $margin_footer, 'setAutoBottomMargin' => 'stretch','setAutoTopMargin' => 'stretch', 'orientation'=>'P' ] );
$mpdf->SetDocTemplate('public/'.$template, true);

$mpdf->WriteHTML($proposal_template_html);

$mpdf->Output($name.'.pdf', 'I');

In proposal_template_html I am adding LARAVEL BLADE HTML Template with mostly Tables.

<htmlpageheader name="myHeader1"> 
Header HTML Code
</htmlpageheader>

My Table Content for PDF of 2 page

<htmlpagefooter name="myCustomFooter" >
Footer HTML Code
</htmlpagefooter>

<style>
  @page { footer: html_myCustomFooter; }
  @page { header: html_myCustomHeader; }
</style>

ISSUE

Need Header only at BEGINNING of Template

Need Footer only at END of Template

But it has been on EVERY Page which is the issue, I just want it only 1s.

always-a-learner
  • 3,671
  • 10
  • 41
  • 81
  • you can use css @page, see here https://mpdf.github.io/paging/using-page.html#example-using-headers-and-footers – Gzai Kun Oct 19 '22 at 09:05
  • @GzaiKun see i have use that already, but that will make header and footer for every page, and i want it only 1s, header at start at PDF and Footer at Bottom of PDF – always-a-learner Oct 19 '22 at 11:32

1 Answers1

0

I don't know if you tried it according to my suggestion or not, but it should be like this :

public function pdf_01()
{
        $mpdf = new \Mpdf\Mpdf();
        
        $html = '
        <html>
        <head>
        <style>
            @page {
                size: auto;
                header: _blank;
                footer: _blank;
            }
            @page page_first {
                header: html_Header;
                footer: _blank;
            }
            @page page_last {
                header: _blank;
                footer: html_Footer;
            }
            @page noheader {
                header: _blank;
                footer: _blank;
            }
            div.page_first {
                page-break-before: right;
                page: page_first;
            }
            div.page_last {
                page-break-before: right;
                page: page_last;
            }
            div.noheader {
                page-break-before: right;
                page: noheader;
            }
        </style>
        </head>
        
        <body>
        
            <htmlpageheader name="Header" style="display:none">
                <div>Header</div>
            </htmlpageheader>
        
            <htmlpagefooter name="Footer" style="display:none">
                <div>Footer</div>
            </htmlpagefooter>
        
            <div class="page_first">Page 1</div>
        
            <div class="noheader">Page 2</div>
            
            <div class="noheader">Page 3</div>
            
            <div class="noheader">Page 4</div>
        
            <div class="page_last">Page 5</div>
        
        </body>
        </html>';
        
        $mpdf->WriteHTML($html);
        
        $mpdf->Output();
}

OR

I'm assuming you're displaying all your data directly into the table, you can use like this :

(note : mpdf can't detect the last attribute it can only detect the first attribute (:first), so we trick it )

    public function pdf_02()
    {
        $mpdf = new \Mpdf\Mpdf();
        
        $data = [
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
            ['Data A', 'Data B', 'Data C', 'Data D', 'Data E'],
        ];
        
        $collection = collect( $data );
        
        // you can change data per page to chunck
        $per_page = 15;
        
        $chunks = $collection->chunk( $per_page );
        
        $table = '';
        
        foreach ($chunks as $key1 => $value1) {
            
            if ( $key1 == 0 ) 
            {
                $class = 'page_first';
            } else if ( $key1 == count( $chunks ) - 1 ) 
            {
                $class = 'page_last';
            } else 
            {
                $class = 'noheader';
            }
            
            $table .= '
            <div class="' . $class . '">
                <table border="1" cellspacing="0" cellpadding="10" width="100%">
                    <thead>
                        <tr>
                            <th>Title 1</th>
                            <th>Title 2</th>
                            <th>Title 3</th>
                            <th>Title 4</th>
                            <th>Title 4</th>
                        </tr>
                    </thead>
                    <tbody>';
                
                foreach ($value1 as $key2 => $value2) {
                    $table .= '
                    <tr>
                        <td>' . $value2[0] . '</td>
                        <td>' . $value2[1] . '</td>
                        <td>' . $value2[2] . '</td>
                        <td>' . $value2[3] . '</td>
                        <td>' . $value2[4] . '</td>
                    </tr>
                    ';
                }
                
            $table .= '
                    </tbody>
                </table>
            </div>
            ';
        }
        
        $html = '
        <html>
        <head>
        <style>
            @page {
                size: auto;
                header: _blank;
                footer: _blank;
            }
            @page page_first {
                header: html_Header;
                footer: _blank;
            }
            @page page_last {
                header: _blank;
                footer: html_Footer;
            }
            @page noheader {
                header: _blank;
                footer: _blank;
            }
            div.page_first {
                page-break-before: right;
                page: page_first;
            }
            div.page_last {
                page-break-before: right;
                page: page_last;
            }
            div.noheader {
                page-break-before: right;
                page: noheader;
            }
        </style>
        </head>
        
        <body>
        
            <htmlpageheader name="Header" style="display:none">
                <div>Header</div>
            </htmlpageheader>
        
            <htmlpagefooter name="Footer" style="display:none">
                <div>Footer</div>
            </htmlpagefooter>
        
            ' . $table . '
        
        </body>
        </html>';
        
        $mpdf->WriteHTML($html);
        
        $mpdf->Output();
    }
Gzai Kun
  • 148
  • 1
  • 12