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

  1. Install wkhtmltopdf Software
  2. Install Laravel 8 App
  3. Install mikehaertl/phpwkhtmltopdf
  4. Add Routes
  5. Create Controller by Command
  6. Create Blade View
  7. Run Development Server
  8. 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


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:


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:

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->setOptions(['javascript-delay' => 5000]);
        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>
    <meta charset="utf-8">
    <script src=""></script>
        .pie-chart {
            width: 600px;
            height: 400px;
            margin: 0 auto;
            text-align: center;
<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>
<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');
            ['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);


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:

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.

Recommended Posts

View All

Unlocking the Power of Data Visualizing Laravel App Data with Chartello

Discover the potential of data visualization with Chartello, a Laravel app that helps you unlock insights from your data. Try it now and see the resul...

Laravel 9 Add Watermark on Image

In this article, we'll show you how to use the Laravel application to apply a text overlay watermark on an image.

How To Integrate Google Maps in Laravel

A number of ways are available through the Google Maps JavaScript API to build maps for web applications.

Laravel 9 Elasticsearch Integration From Scratch With Example

In this post, we'll talk about how to incorporate Elasticsearch from scratch using a Laravel 9 example.

Laravel 8 Import Export Excel & CSV File Example

Using the Maatwebsite/Laravel-Excel package, you will learn how to easily import and export Excel and CSV files in the Laravel 8 application while com...