Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMPDF how to add Page Number

Tags:

php

dompdf

I have checked every answer related to this question,yet it didnt worked.

I want to add Page number in Footer, I have tried below code yet, It didn't worked. Can anyone tell me where I am going wrong?

$dompdf = new Dompdf();
$dompdf->set_option("isPhpEnabled", true);
$html_content = "
<html>
<head>
<style>
@font-face {
  font-family: kindergarten;
  font-weight: normal;
  font-style: normal;
  src: url('fonts/kindergarten.ttf') format('truetype');
}
.test{  font-family: kindergarten;  }
</style>

</head>
<body>
<script type='text/php'>
if ( isset($pdf) ) { 
    $pdf->page_script('

            $font = $fontMetrics->get_font('Arial, Helvetica, sans-serif, 'normal');
            $size = 12;
            $pageText = 'Page 1';
            $y = 15;
            $x = 520;
            $pdf->text($x, $y, $pageText, $font, $size);

    ');
}
</script>
<div>My Content goes here</div>
</body>
</html>
"; 
//echo $html_content; die;
$dompdf->loadHtml($html_content);
$dompdf->render();
like image 720
Bits Please Avatar asked Jan 27 '23 07:01

Bits Please


1 Answers

Use the public method page_text()

// Documentation

 * @param float  $x
 * @param float  $y
 * @param string $text       the text to write
 * @param string $font       the font file to use
 * @param float  $size       the font size, in points
 * @param array  $color
 * @param float  $word_space word spacing adjustment
 * @param float  $char_space char spacing adjustment
 * @param float  $angle      angle to write the text at, measured CW starting from the x-axis

My example (https://prnt.sc/mifmxl):

$html   = 'Text to test...';
$dompdf = new Dompdf();

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

// Parameters
$x          = 505;
$y          = 790;
$text       = "{PAGE_NUM} of {PAGE_COUNT}";     
$font       = $dompdf->getFontMetrics()->get_font('Helvetica', 'normal');   
$size       = 10;    
$color      = array(0,0,0);
$word_space = 0.0;
$char_space = 0.0;
$angle      = 0.0;

$dompdf->getCanvas()->page_text(
  $x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle
);

// stream
$dompdf->stream('pdf_out.pdf', array('Attachment' => false));

Version: DomPDF (v0.8.3)

like image 78
JeFF - JXG Avatar answered Jan 30 '23 04:01

JeFF - JXG