Laravel 9 Generate PDF with Chart Example Tutorial

Reading Time: 6 minutes

Inside this article we will see the concept i.e Laravel 9 Generate PDF with Chart Example Tutorial. Article contains the classified information about generating PDF file with google chart. In this article we will consider google pie chart in PDF file.

If you are looking for an article which gives you the understanding of adding google charts in PDF inside laravel then this article will help you a lot to understand the things.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve


Assuming laravel already installed inside your system.

Install wkhtmltopdf Sofwate Package

You need to install this software package into your system. This will help a laravel package to work with internal functions. HTML to PDF converter.

For Ubuntu System

Run this command into terminal to install it.

$ sudo apt install wkhtmltopdf

Inside terminal you will see something like this –

For Windows System

If you are a window OS user, then you need to follow these steps to install it.

Install Composer Package – phpwkhtmltopdf

Open project terminal and run this composer command to install this phpwkhtmltopdf package.

$ composer require mikehaertl/phpwkhtmltopdf

It will install a composer package into your laravel application.

Create Controller


We need to create a controller file.

$ php artisan make:controller PDFController

It will create a file PDFController.php inside /app/Http/Controllers folder.

Open PDFController.php and write this complete code into it.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use mikehaertl\wkhtmlto\Pdf;

class PDFController extends Controller
     * Write code on Construct
     * @return \Illuminate\Http\Response
    public function preview()
        return view('chart');

     * Write code on Construct
     * @return \Illuminate\Http\Response
    public function download()
        $render = view('chart')->render();

        $pdf = new Pdf;
        $pdf->setOptions(['javascript-delay' => 5000]);

        return response()->download(public_path('report.pdf'));

report.pdf will be downloaded and saved into /public folder.

Create Blade Layout File

Go to /resources/views folder and create a file with name chart.blade.php

Open chart.blade.php and write this complete code into it.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Laravel 9 Generate PDF with Chart Tutorial</title>
    <script src=""></script>
        .pie-chart {
            width: 600px;
            height: 400px;
            margin: 0 auto;
            text-align: center;
<h2 class="text-center">Laravel 9 Generate PDF with Chart Tutorial</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);

Add Route

Open web.php from /routes folder and add these route into it.

use App\Http\Controllers\PDFController;


Route::get('preview', [PDFController::class, "preview"]);
Route::get('download', [PDFController::class, "download"])->name('download');

Application Testing

Run this command into project terminal to start development server,

php artisan serve


When you click on “Download PDF File”, it will download a PDF file with name report.pdf into /public folder.

When you open report.pdf, it will be –

We hope this article helped you to Laravel 9 Generate PDF with Chart Example Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more