2

I am using Laravel DOMPDF everything is working fine except i want to show page no on every page also which is not showing as of now.

My Controller:

public function generatePDF()
    {
       try
        {
            $id = hexdec($_GET['id']);
            $data = DB::table('table_name')
            ->where('id','=',$id)
            ->get();       
            $pdf = PDF::loadView('pages.myPDF', compact('data'));
            return $pdf->download($caseName.'.pdf');
          }
          catch(\Exception $ex)
          {
              return $ex->getMessage();
          }   
    }

My View File:

<html>
<head>
</head>
<style type="text/css" media="all">
    @page {
    header: page-header;
    footer: page-footer;
}

@page{
    header:page-header;
    margin-top:105t;
}

.w3-Times-New-Roman {
  font-family: "Lobster", serif;
}
</style>

<body>
    <script>
    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>    
        <div style="font-family: Times New Roman; font-size: 12px;word-spacing: 3px; text-align: justify; color:#000;"> <?php
            $array1 = array("<blockquote>\r\n<p>","</p>\r\n</blockquote>");
            $array2 = array("<blockquote>","</blockquote>");
            $array3 = array("<i><u>","</u></i>"); 
            $value = str_replace($array1, $array3, $data[0]->columnName);
            $value = str_replace($array2, $array3, $value);
          ?>
          {!!$value!!}
        </div><br>

</body>

</html>

But this is not generating no on every page in PDF. Please help me how can i achieve that.

Amit
  • 71
  • 7

2 Answers2

0

Follow below steps to achieve it:

  • Publish vendor file via command

    php artisan vendor:publish
    
  • Enable DOMPDF_ENABLE_PHP from /config/dompdf.php

  • Pass $pdf object from controller

Use php artisan vendor:publish to create a config file located at config/dompdf.php which will allow you to define local configurations to change some settings (default paper etc). You can also use your ConfigProvider to set certain keys.

Kaleem Shoukat
  • 811
  • 6
  • 14
0

Try this

$pdf = App::make('dompdf.wrapper');
    $pdf->loadView('welcome', ['data'=>$data]);
    $pdf->output();
    $dom_pdf = $pdf->getDomPDF();

    $canvas = $dom_pdf ->get_canvas();
    $canvas->page_text(520, 10, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, array(0, 0, 0));

    return $pdf->download('safsa.pdf');;

and in view

 <footer>
            <div class="pagenum-container" style="float: right">Page
                <span class="pagenum"></span>
            </div>
        </footer>

and style

footer .pagenum:before {
            content: counter(page);
        }
footer { position: fixed; bottom: -60px; left: 0px; right: 0px; height: 50px; }
John Lobo
  • 14,355
  • 2
  • 10
  • 20