This post is also available in:
Indonesia (Indonesian)
This example is focused on laravel 8 pdf file from view. You can understand the laravel 8 concept of generating pdf files. I explain simply about laravel 8 pdf dompdf. This article will give you a simple example on how to generate pdf in laravel 8.
Here, Create basic example of laravel 8 create pdf of view.
PDF is one of the basic requirements when you are working with erp level projects or e commerce websites. we may need to create pdf files for reports or invoices etc. So, here I will give a very simple example to create a pdf file with laravel.
You just need to follow below steps to create pdf file and also can download. So let’s do the steps below.
Step 1: Install Laravel 8
I will explain step by step from scratch so, we need to get new Laravel 8 application using below command, So open your terminal OR command prompt and run below command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install dompdf Package
we will first install the barryvdh/laravel-dompdf composer package by following the composer command in your laravel 8 application.
composer require barryvdh/laravel-dompdf
After successfully installing the package, open the config/app.php file and add the service provider and alias.
config/app.php
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 3: Add Route
In this step we need to create a route for the list of items. so go to your “routes/web.php” file and add the following routes.
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: Add Controller
Here, we need to create a new PDFController controller which will manage the generatePDF route method. So let’s put the code below.
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 temanngoding.com',
'date' => date('m/d/Y')
];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('itsolutionstuff.pdf');
}
}
Step 5: Create View File
In the last step, let’s create myPDF.blade.php(resources/views/myPDF.blade.php) for the layout pdf file and enter the following code:
resources/views/myPDF.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Hi</title>
</head>
<body>
<h1>{{ $title }}</h1>
<p>{{ $date }}</p>
<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>
</body>
</html>