Laravel 8 ? produce a PDF with an example graph We'll show you how to make a pdf with a graph in the Laravel 8 app in this tutorial.
The term "graph" refers to a diagram that depicts a relationship between two or more things. Making a sequence of bars on graphing paper is an example of a graph. A graph is a diagram that depicts the relationships between two or more objects. Pie charts are one type of graphs.
This tutorial will walk you through the process of creating a pdf with graph in a Laravel application, step by step.
Table of Content
- Install wkhtmltopdf Software
- Install Laravel 8 App
- Install mikehaertl/phpwkhtmltopdf
- Add Routes
- Create Controller by Command
- Create Blade View
- Run Development Server
- Test This App
Step 1 ? Install wkhtmltopdf Software
To begin, run the following command on your web server to install the wkhtmltopdf package. This programme will be installed on your webservers using the commands below:
For Ubuntu:
sudo apt install wkhtmltopdf
For Windows:
You must click on the below link and download the exe file. Then do the following
https://wkhtmltopdf.org/
Step 2 ? Install Laravel 8 App
To install the latest version of the Laravel application, use the following command. So, open your terminal OR command prompt and type in the following:
composer create-project --prefer-dist laravel/laravel blog
Step 3 ? Install mikehaertl/phpwkhtmltopdf
Install the mikehaertl/phpwkhtmltopdf package in this step. So, open your command prompt once more and type in the following command:
composer require mikehaertl/phpwkhtmltopdf
Step 4 ? Add Routes
Open the web.php file in this phase and add the following routes to it:
routes/web.php
use App\Http\Controllers\GraphPdfController;
Route::get('graph', [GraphPdfController::class, 'index']);
Route::get('download', [GraphPdfController::class, 'dwn'])->name('download');
The very first route will display a graph on a web page, while the second will allow you to download a pdf file.
Step 5 ? Create Controller by Command
Open your command prompt and type the following command to build a controller called GraphPDFcontroller:
php artisan make:controller GraphPdfController
This command will generate GraphPdfController.php as a controller.
After that, navigate to app/Http/Controllers and open GraphPdfController.php. Then, in your controller file, update the following methods:
<?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'));
}
}
You can update two methods in this controller. The index() method loads the web page and displays the data graph. The downlaod() method, on the other hand, will download the pdf file.
Step 6 ? Create View File
Go to the resources/views/ folder and create a new blade view file called graph.blade.php.
Then, add the following code to your graph.blade.php file:
<!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>codesolutionstuff.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
Use the php artisan serve command to start your server locally in this step:
php artisan serve
Step 8 ? Test This App
Open your browser and type the following url into it:
http://127.0.0.1:8000/graph
I hope you will like the content and it will help you to learn Laravel 8 Generate PDF with Graph Tutorial
If you like this content, do share.