Hello, laravel web developers! In this article, we'll see how to add a page number to a PDF file in laravel 11. In laravel 11 generate a PDF file with page numbers using the dompdf composer package.
If you want to add page numbers to a PDF file using Laravel's DomPDF library, you can easily do it using the DomPDF canvas method. I'll show you how to use getDomPDF()
, get_canvas()
, and page_text()
to insert page numbers in the header and footer of the PDF document.
Laravel 11 Add Page Number to PDF File
In this step, we'll install the laravel 11 application using the following command.
composer create-project --prefer-dist laravel/laravel laravel-11-example
Then, we'll install laravel-dompdf composer package using the following command.
composer require barryvdh/laravel-dompdf
Next, we'll define the routes into the web.php file.
routes/web.php
Route::get('generate-pdf','PDFController@generatePDF');
Then, we'll create a controller and generate the PDF file.
app/Http/Controllers/PDFController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PDFController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function generatePDF()
{
$data = ['title' => 'Welcome to Techsolutionstuff'];
$pdf = PDF::loadView('test-pdf', $data);
$pdf->output();
$domPdf = $pdf->getDomPDF();
$canvas = $domPdf->get_canvas();
$canvas->page_text(10, 10, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, [0, 0, 0]);
/* Set Page Number to Footer */
/* $canvas->page_text(10, $canvas->get_height() - 20, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, [0, 0, 0]); */
return $pdf->download('test.pdf');
}
}
Now, we'll create a blade file for the PDF file and add the image to the PDF file.
resources/views/test-pdf.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Add Page Number in PDF file in Laravel 11 - Techsolutionstuff</title>
<style>
.page-break {
page-break-after: always;
}
</style>
</head>
<body>
<h1>Welcome to - {{ $title }}</h1>
<p class="page-break">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p class="page-break">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>
Also, you can add page numbers using the script.
Follow the below steps to achieve it:
ENABLE_PHP
from /config/dompdf.php by default it is false. So, change to true.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PDFController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function generatePDF()
{
$data = ['title' => 'Welcome to Techsolutionstuff'];
$pdf = PDF::loadView('test-pdf', $data);
return $pdf->download('test.pdf');
}
}
resources/views/test-pdf.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Add Page Number in PDF file in Laravel 11 - Techsolutionstuff</title>
<style>
.page-break {
page-break-after: always;
}
</style>
</head>
<body>
<h1>Welcome to - {{ $title }}</h1>
<p class="page-break">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p class="page-break">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
<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>
</html>
Note: page_text() first two parameters are the x and y position. So, you can adjust it accordingly.
You might also like: