21-Feb-2022
.
Admin
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.
config/app.php
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 3: Create Route
routes/web.php
<?php
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
app/Http/Controllers/PDFController.php
<?php
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 Nicesnippets.com',
'date' => date('m/d/Y')
];
$pdf = PDF::loadView('myPDF', $data);
$pdf->setPaper('L');
$pdf->output();
$canvas = $pdf->getDomPDF()->getCanvas();
$height = $canvas->get_height();
$width = $canvas->get_width();
$canvas->set_opacity(.2,"Multiply");
$canvas->set_opacity(.2);
$canvas->page_text($width/5, $height/2, 'Nicesnippets.com', 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
<html>
<head>
<style>
/** 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;
}
</style>
</head>
<body>
<!-- Define header and footer blocks before your content -->
<header>
<p>Nicesnippets.com</p>
</header>
<footer>
<div style="margin-top: 8px !important">Copyright © <?php echo date("Y");?> . All rights reserved.</div>
</footer>
<!-- Wrap the content of your PDF inside a main tag -->
<main>
<p>
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>
</main>
</body>
</html>
Preview
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.
http://localhost:8000/generate-pdf
It will help you...
#Laravel 8
#Laravel