How to Set Watermark using DomPDF in Laravel?




How to Set Watermark using DomPDF in Laravel?

Hi Dev,

Today, in this Example I will share with you something new about how to set watermark dompdf in laravel application, we will show an example of watermark in dompdf laravel.

Here, I will give you a full example of how to add dompdf watermark text in pdf using laravel in laravel as below so follow my all steps.

Step 1 : Install Laravel

In the first step, we need to get fresh laravel version application So let's open terminal and run bellow command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel blog

Step 2 : Install dompdf Package

first of all we will install barryvdh/laravel-dompdf composer package by following composer command in your laravel application.

composer require barryvdh/laravel-dompdf

After successfully install package, open config/app.php file and add service provider and alias.


'providers' => [




'aliases' => [


'PDF' => Barryvdh\DomPDF\Facade::class,


Step 3: Create Route



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PDFController;



| Web Routes



| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!



Route::get('generate-pdf', [PDFController::class, 'generatePDF']);

Step 4:Create Controller

In this step,we will create a controller. Use the below command for generate controller



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;

class PDFController extends Controller



* Write Your Code..


* @return string


public function generatePDF()


$data = [

'title' => 'Welcome to',

'date' => date('m/d/Y')


$pdf = PDF::loadView('myPDF', $data);



$canvas = $pdf->getDomPDF()->getCanvas();

$height = $canvas->get_height();

$width = $canvas->get_width();



$canvas->page_text($width/5, $height/2, '', null,

55, array(0,0,0),2,2,-30);

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



Step 5:Create a blade view

In this step, we will create a blade file name myPDF.blade.php bellow following path.

Path : resources/views/myPDF.blade.php




/** Define the margins of your page **/

@page {

margin: 100px 25px;


header {

position: fixed;

top: -60px;

left: 0px;

right: 0px;

height: 60px;

/** Extra personal styles **/

background-color: #1dbb90;

color: white;

text-align: center;

line-height: 35px;


header p{

font-size: 25px;

margin-top: 8px;


footer {

position: fixed;

bottom: -60px;

left: 0px;

right: 0px;

height: 60px;

font-size: 20px !important;

color: white; !important;

/** Extra personal styles **/

background-color: #1dbb90;

text-align: center;

line-height: 35px;





<!-- Define header and footer blocks before your content -->





<div style="margin-top: 8px !important">Copyright © <?php echo date("Y");?> . All rights reserved.</div>


<!-- Wrap the content of your PDF inside a main tag -->



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.






Now, we will use the php artisan serve command.

php artisan serve

Now we are ready to run our example so run bellow command to quick run.


It will help you...

#Laravel 8