In this tutorial you will learn about the Laravel 8 Generate PDF with Graph Tutorial and its application with practical example.
In this Laravel 8 Generate PDF with Graph tutorial, I’ll show you how to generate pdf with graph in laravel 8 application. In this tutorial you will learn to generate pdf file with graph in laravel. In this article we will create example to Generate PDF with Graph. We will be using wkhtmltopdf Software to generate pdf file in this tutorial.
Laravel 8 Generate PDF with Graph Tutorial
In this Laravel 8 Generate PDF with Graph Tutorial I will guide you step by step how to generate pdf with graph in laravel 8 application.
- Step 1 – Install wkhtmltopdf Software
- Step 2 – Install Laravel 8 App
- Step 3 – Install mikehaertl/phpwkhtmltopdf
- Step 4 – Add Routes
- Step 5 – Create Controller by Command
- Step 6 – Create Blade View
- Step 7 – Run Development Server
- Step 8 – Test This App
Step 1 – Install wkhtmltopdf Software
For Ubuntu:
1 |
sudo apt install wkhtmltopdf |
For Windows:
You have go the following link and download exe. Then install it:
1 |
https://wkhtmltopdf.org/ |
Step 2 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 3 – Install mikehaertl/phpwkhtmltopdf
In this step, we will install mikehaertl/phpwkhtmltopdf Package via the composer dependency manager. Use the following command to install mikehaertl/phpwkhtmltopdf Package.
1 |
composer require mikehaertl/phpwkhtmltopdf |
Step 4 – Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 |
use App\Http\Controllers\GraphPdfController; Route::get('graph', [GraphPdfController::class, 'index']); Route::get('download', [GraphPdfController::class, 'dwn'])->name('download'); |
Step 5 – Create Controller by Command
Now, lets create a controller named GraphPdfController using command given below –
1 |
php artisan make:controller GraphPdfController |
The above command will create a controller file GraphPdfController.php in app/Http/Controllers folder. Now, go to app/Http/Controllers and open GraphPdfController.php. Then put the following code into your controller file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use mikehaertl\wkhtmlto\Pdf; class GraphPdfController extends Controller { /** * Write code on Construct * * @return \Illuminate\Http\Response */ public function index() { return view('graph'); } /** * Write code on Construct * * @return \Illuminate\Http\Response */ public function dwn() { $render = view('chart')->render(); $pdf = new Pdf; $pdf->addPage($render); $pdf->setOptions(['javascript-delay' => 5000]); $pdf->saveAs(public_path('report.pdf')); return response()->download(public_path('report.pdf')); } } |
Step 6 – Create View File
Go to resources/views/ folder and create a new blade view file that name graph.blade.php file. After that, update the following code into your graph.blade.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://www.google.com/jsapi"></script> <style> .pie-chart { width: 600px; height: 400px; margin: 0 auto; } .text-center{ text-align: center; } </style> </head> <body> <h2 class="text-center">Laravel 8 Generate PDF with Chart</h2> <div id="chartDiv" class="pie-chart"></div> <div class="text-center"> <a href="{{ route('download') }}">Download PDF File</a> <h2>w3adda.com.com</h2> </div> <script type="text/javascript"> window.onload = function() { google.load("visualization", "1.1", { packages: ["corechart"], callback: 'drawChart' }); }; function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('string', 'Pizza'); data.addColumn('number', 'Populartiy'); data.addRows([ ['Laravel', 33], ['Codeigniter', 26], ['Symfony', 22], ['CakePHP', 10], ['Slim', 9] ]); var options = { title: 'Popularity of Types of Framework', sliceVisibilityThreshold: .2 }; var chart = new google.visualization.PieChart(document.getElementById('chartDiv')); chart.draw(data, options); } </script> </body> </html> |
Step 7 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/graph |