10-Jun-2023
.
Admin
Hello Guys,
Now let's see an example of how to generate a pdf from html view file in Laravel 10 example. This is a short guide on Laravel 10 pdf from html view file. Here you will learn to generate pdf from html view file Laravel 10. We will use generate a pdf from html view file in Laravel 10. Let's get started with generating a pdf from html view file in Laravel 10.
Here I will give you a few steps and instructions to generate a pdf from html view file in Laravel 10.
Step 1: Download Laravel
Let us begin the tutorial by installing a new Laravel application. if you have already created the project, then skip the following step.
composer create-project laravel/laravel example-app
Step 2: Install Package
In this step, we will do Installation. We will first open our terminal or command prompt and run the following command:
composer require barryvdh/laravel-dompdf --ignore-platform-reqs
Now we will open our file named config/app.php. Then we will add aliased and service provider.
config/app.php
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
],
Step 3: Add Route
In this step, we will Add Route, We will create a route so that we can generate a view,
app/Http/routers.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ItemController;
/*
|--------------------------------------------------------------------------
| 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('itemPdfView',[ItemController::class,'itemPdfView']);
Route::post('itemPdfView',array('as'=>'itemPdfView','uses'=>'ItemController@itemPdfView'));
Step 4: Add Controller
In this step, we will create a Controller. For this, we are going to use the app/Http/Controllers/ItemController.php path to add a new controller as ItemController.
php artisan make:controller ItemController
app/Http/Controllers/ItemController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use PDF;
class ItemController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function itemPdfView(Request $request)
{
$items = DB::table("items")->get();
view()->share('items',$items);
if($request->has('download')){
$pdf = PDF::loadView('itemPdfView');
return $pdf->download('itemPdfView.pdf');
}
return view('itemPdfView');
}
}
Step 5: Add Blade File
In this step, we will create a View file. We will create a view and pdf file by using the view file named "pdfview.blade.php".
resources/view/itemPdfView.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How To Generate Pdf From Html View File And Download Using Laravel 10 Example - NiceSnippets.com</title>
</head>
<style type="text/css">
table{
width: 100%;
border-collapse: collapse;
}
table td, table th{
border:1px solid black;
text-align: center;
}
table tr, table td{
padding: 5px;
}
</style>
<body>
<div class="container">
<br/>
<h4>How To Generate Pdf From Html View File And Download Using Laravel 10 Example - NiceSnippets.com</h4>
<a href="{{ route('itemPdfView',['download'=>'pdf']) }}">Download PDF</a>
<table>
<tr>
<th>No</th>
<th>Name</th>
<th>Price</th>
</tr>
@foreach ($items as $key => $item)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->price }}</td>
</tr>
@endforeach
</table>
</div>
</body>
</html>
Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the Laravel app:
php artisan serve
Now, you have to open the web browser, type the given URL and view the app output:
http://localhost:8000/itemPdfView
Output :
It will help you...
#Laravel 10