Reputation: 519
Hi I am using DOMPDF to generate PDF file, I would like to know how to get page number.
I have try the following as mention in FAQ page of DOMPDF. and not successful. NOTE: I also have turn on inline PHP as well as it mention in FAQ
following is my code
<?php
require_once("dompdf/dompdf_config.inc.php");
ob_start();
//be sure this file exists, and works outside of web context etc.)
$dompdf = new DOMPDF();
$html=" <script type='text/php'>";
if ( isset($pdf) ) {
$font = Font_Metrics::get_font("yourfont", "normal");
$size = 9;
$y = $pdf->get_height() - 24;
$x = $pdf->get_width() - 15 - Font_Metrics::get_text_width("1/1", $font, $size);
$pdf->page_text($x, $y, "{PAGE_NUM}/{PAGE_COUNT}", $font, $size);
}
$html.=" </script>";
$html .="
aaaaa<br/>aaaaaa<br/>aaaaaa<br/>aaaaa<br/>aaaaaa<br/>aaaaaa<br/>aaaa<br/>aaaaa<br/>aaaaaa<br/>aaaaa<br/>
aaaaa<br/>aaaaaaa<br/>aaaaaa<br/>
aaaaa<br/>aaaaaa<br/>
aaaaaaa<br/>aaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaa<br/>aaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaaa<br/>aaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
</body></html>";
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
Can someone point me out this issue please? where did i make it wrong
Upvotes: 7
Views: 31475
Reputation: 10572
Code that worked for me. You can customize where is should be located depending on the size of your PDF:
$pdf->load_html($html);
$pdf->render();
$dompdf = $pdf->getDomPDF();
$color = array(0, 0, 0);
$font = null;
$size = 10;
$canvas = $dompdf->get_canvas();
$canvas->page_text(0, 0, "{PAGE_NUM} of {PAGE_COUNT}", $font, $size, $color);
Remember that color array doesn't contain RGB values. Appropriate values you can take from row RYB here as a value in range (0,1), i.e. array(0.46,0.46,0.46)
.
Upvotes: 0
Reputation: 1935
Where you have '1/1' being used to get $x, this won't work when you have more than 10 pages.
A possible solution does mean editing "cpdf_adapter.cls.php", so the _add_page_text() function used the following just after the str_replace()... about line 790:
if ($x < 0) $x = (($this->_width + $x) - $this->get_text_width($text, $font, $size));
if ($y < 0) $y = (($this->_height + $y) - $this->get_font_height($font, $size));
The idea is that you can pass in negative x/y coords, and they will work from the right/bottom edges.
Then in the HTML thats sent to DOMPDF, I used:
<script type="text/php">
$pdf->page_text(-30, -32, "{PAGE_NUM} of {PAGE_COUNT}", Font_Metrics::get_font("serif"), 10, array(0,0,0));
</script>
Or if you want to specify the x/y in px:
<script type="text/php">
$pdf->page_text(((-30 * 72) / DOMPDF_DPI), ((-32 * 72) / DOMPDF_DPI), "{PAGE_NUM} of {PAGE_COUNT}", Font_Metrics::get_font("serif"), 10, array(0,0,0));
</script>
Upvotes: 0
Reputation: 13914
You are running your inline script as part of the PHP page instead of passing it to dompdf. Your code could be written as follows (truncated to the relevant section):
...
$html="
<html>
<body>
<script type='text/php'>
if ( isset($pdf) ) {
$font = Font_Metrics::get_font('helvetica', 'normal');
$size = 9;
$y = $pdf->get_height() - 24;
$x = $pdf->get_width() - 15 - Font_Metrics::get_text_width('1/1', $font, $size);
$pdf->page_text($x, $y, '{PAGE_NUM}/{PAGE_COUNT}', $font, $size);
}
</script>
";
...
Note that inline script must currently appear inside the BODY element. Otherwise it will be ignored during document processing.
There are other ways to achieve what you want as well.
Upvotes: 11