How to add page number for every page in laravel dompdf?
Asked Answered
C

6

10

I get from here : https://github.com/barryvdh/laravel-dompdf

My controller is like this :

public function listdata()
{
    $pdf=PDF::loadView('print_tests.test_pdf');
    $pdf->setPaper('L', 'landscape');
    return $pdf->stream('test_pdf.pdf');
}

My view is like this :

<table>
    <tr>
        <th>header1</th>
        <th>header2</th>
        <th>header3</th>
    </tr>
    <tr>
        <td>content-row-1</td>
        <td>content-row-1</td>
        <td>content-row-1</td>
    </tr>
    <tr>
        <td>content-row-2</td>
        <td>content-row-2</td>
        <td>content-row-2</td>
    </tr>
</table>

I want every page there is a page number

Is there any people who can help me?

Crumpler answered 21/12, 2016 at 22:15 Comment(1)
Have you seen this? The accepted answer can be found here. #19984110Nikethamide
B
6

Follow below steps to achieve it:
- Enable DOMPDF_ENABLE_PHP from /config/dompdf.php
- Publish vendor file via php artisan vendor:publish command
- Pass $pdf object from controller:
- Add below code inside the view file:

<script type="text/php">
    if ( isset($pdf) ) {
        $font = Font_Metrics::get_font("helvetica", "bold");
        $pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
    }
</script> 

You can get more idea from Page count and page number

Burglar answered 24/12, 2016 at 10:41 Comment(2)
Note that for version >= 0.7, you should be using Dennis Ameling's solution.Veroniqueverras
here are a solution github.com/barryvdh/laravel-dompdf/issues/452Enchilada
Y
10

Try this one.

<style>
   .pagenum:before {
        content: counter(page);
    }
</style>
<body>

    <span class="pagenum"></span>

    <table>
        <tr>
          <th>header1</th>
          <th>header2</th>
          <th>header3</th>
     </tr>
     <tr>
         <td>content-row-1</td>
          <td>content-row-1</td>
         <td>content-row-1</td>
     </tr>
     <tr>
        <td>content-row-2</td>
        <td>content-row-2</td>
        <td>content-row-2</td>
     </tr>
    </table>
</body>
Yaupon answered 18/7, 2019 at 18:17 Comment(0)
N
8

you can do like that for example..

in your controller :

$pdf = app('dompdf.wrapper');
$pdf->getDomPDF()->set_option("enable_php", true);
$data = ['title' => 'Testing Page Number In Body'];
$pdf->loadView('welcomeView', $data);

in your welcomeView.blade.php

<html>
 <body>
   <h1> {{ $title }} </h1>

   <script type="text/php">
    if (isset($pdf)) {
        $x = 250;
        $y = 10;
        $text = "Page {PAGE_NUM} of {PAGE_COUNT}";
        $font = null;
        $size = 14;
        $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>

</body>
</html>
Necrotomy answered 15/3, 2019 at 6:42 Comment(1)
May I know what is font?Misinform
B
6

Follow below steps to achieve it:
- Enable DOMPDF_ENABLE_PHP from /config/dompdf.php
- Publish vendor file via php artisan vendor:publish command
- Pass $pdf object from controller:
- Add below code inside the view file:

<script type="text/php">
    if ( isset($pdf) ) {
        $font = Font_Metrics::get_font("helvetica", "bold");
        $pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
    }
</script> 

You can get more idea from Page count and page number

Burglar answered 24/12, 2016 at 10:41 Comment(2)
Note that for version >= 0.7, you should be using Dennis Ameling's solution.Veroniqueverras
here are a solution github.com/barryvdh/laravel-dompdf/issues/452Enchilada
J
0
$pdf = PDF::loadView('print_tests.test_pdf');

$pdf->render();

$dompdf = $pdf->getDomPDF();
$font = $dompdf->getFontMetrics()->get_font("helvetica", "bold");
$dompdf->get_canvas()->page_text(34, 18, "{PAGE_NUM} / {PAGE_COUNT}", $font, 10, array(0, 0, 0));

return $pdf->stream('test_pdf.pdf');

The page_text() acts on each generated page, and it is expected that pages have already been generated within the document when it is called. The starting $pdf->render() call is intended to trigger that action, in particular when large tables (spreading across multiple pages) are included in the document.

Judoka answered 13/10, 2023 at 20:5 Comment(0)
B
0

Without using php in template, based on documentation :

    try {
        $pdf = PDF::loadview('my_view');
        $pdf->render();
        $canvas = $pdf->getDomPDF()->getCanvas();
        $canvas->page_script(function ($pageNumber, $pageCount, $canvas, $fontMetrics) {
            // Text to display
            $text = "$pageNumber/$pageCount";
            // Margins from the bottom right
            $marginRight = 5;
            $marginBottom = 5;
            // Font and font size
            $fontSize = 12;
            $font = $fontMetrics->getFont('Helvetica');
            // Set position
            $textWidth = $fontMetrics->getTextWidth($text, $font, $fontSize);
            $xPosition = $canvas->get_width() - $textWidth - $marginRight;
            $yPosition = $canvas->get_height() - $marginBottom;
            // Draw text
            $canvas->text($xPosition, $yPosition, $text, $font, $fontSize);
        });
        $pdf->save('test.pdf');
    } catch (\Exception $e) {
        \Log::error('Error - rendering PDF');
    }
Bignoniaceous answered 13/4 at 15:31 Comment(0)
C
0

Controller Part
//You can Generate the PDF with A4 landscape orientation
//admin.reports.transactions_pdf is your blade file

$pdf = PDF::loadView('admin.reports.transactions_pdf', $data) - > setPaper('a4', 'landscape') - > set_option("enable_php", true);
//Download the generated PDF
return $pdf - > download("{$reportTitle}.pdf");

at the blade part add this code

< script type = "text/php" >
  enter code here
if (isset($pdf)) {
  $x = 360;
  $y = 540; // Vertical position (adjust based on your page size, typically 820 enter code herefor A4)
  $text = "Page {PAGE_NUM} of {PAGE_COUNT}";
  $font = null;
  $size = 12;
  $color = array(0, 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>
Cleave answered 25/6 at 21:40 Comment(1)
its now clear ?Cleave

© 2022 - 2024 — McMap. All rights reserved.