How to get page number on dompdf PDF when using view
Update Regarding changes with version of
dompdf
>=0.7.0
1. Because the dompdf_config.inc.php file has been removed from this release (and is no longer referenced) all dompdf options should be set at run time.
4. The FontMetrics class is now instantiated instead of static. To simplify migration of embedded scripts from earlier versions of dompdf we provide access to the instantiated FontMetrics class via the $fontMetrics variable. Please update your embedded scripts. For example, FontMetrics::get_font('helvetica') would now be $fontMetrics->getFont('helvetica').
~ Thanks to Dennis Ameling's answer for the updated information.
Found my answer by looking over the dompdf_config.inc.php
file. As it turns out, DOMPDF_ENABLE_PHP
is set to false
thus causing the inline php script to be ignored. I simply edited dompdf_config.custom.inc.php
to the following and all is fine and working with the later code in the view
.
In dompdf/dompdf_config.custom.inc.php
<?php
define("DOMPDF_ENABLE_PHP", true);
At Run Time
$dompdf->set_option("isPhpEnabled", true);
Then, in my html file
<body>
<script type="text/php">
if ( isset($pdf) ) {
// OLD
// $font = Font_Metrics::get_font("helvetica", "bold");
// $pdf->page_text(72, 18, "{PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(255,0,0));
// v.0.7.0 and greater
$x = 72;
$y = 18;
$text = "{PAGE_NUM} of {PAGE_COUNT}";
$font = $fontMetrics->get_font("helvetica", "bold");
$size = 6;
$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>
<div
If you go this route, don't forget to restart Apache
How to add page number for every page in laravel dompdf?
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
dompdf page number
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.
How can I get the total number of pages in DOMPDF?
By default, inline PHP is disabled for security reasons, you need to enable it yourself in dompdf_config.custom.inc.php. See here.
For now, total page count is not supported with the CSS you are using, we are planning to make it work in 0.6 final though.
Related Topics
What Is the Maximum Size of an Array in PHP
How to Pass a PHP Variable to Vue Component Instance in Laravel Blade
Disable Browser Cache in PHP or JavaScript in a Flash Application
Session Lost After Page Redirect in PHP
Is MySQLi Extension Enabled in This PHP Configuration
Accessing Main Picture of Wikipedia Page by API
Determine in PHP Script If Connected to Internet
PHP Constructor to Return a Null
PHP File Upload Error Tmp_Name Is Empty
PHP Include Causes White Space at the Top of the Page
How to Calculate Time Difference in PHP
Get the Hierarchy of a Directory with PHP
Backreference Does Not Work in PHP
Warning: Date() Expects Parameter 2 to Be Long, String Given In