Hello Devs,
In this tutorial, we are going to learn how to generate pdf with graph in laravel using snappy.
Follow this step by step guide given below:
Step 1 :
Install wkhtmltopdf and wkhtmltoimage
For x64 System
composer require h4cc/wkhtmltopdf-amd64 0.12.x
composer require h4cc/wkhtmltoimage-amd64 0.12.x
For x86 System
composer require h4cc/wkhtmltopdf-i386 0.12.x
composer require h4cc/wkhtmltoimage-i386 0.12.x
For Windows
composer require wemersonjanuario/wkhtmltopdf-windows "0.12.2.3"
Step 2 :
Install Laravel App
composer create-project --prefer-dist laravel/laravel blog
Step 3 :
Install Laravel Snappy
composer require barryvdh/laravel-snappy
config/app.php
'providers' => [
Barryvdh\Snappy\ServiceProvider::class,
],
'aliases' => [
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
]
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
For Windows config/snappy.php
return [
'pdf' => array(
'enabled' => true,
'binary' => base_path('vendor\wemersonjanuario\wkhtmltopdf-windows\bin\64bit\wkhtmltopdf'),
'timeout' => false,
'options' => array(),
'env' => array(),
),
'image' => array(
'enabled' => true,
'binary' => 'vendor\wemersonjanuario\wkhtmltopdf-windows\bin\64bit\wkhtmltoimage',
'timeout' => false,
'options' => array(),
'env' => array(),
),
];
For Other System config/snappy.php
return [
'pdf' => array(
'enabled' => true,
'binary' => base_path('vendor\wemersonjanuario\wkhtmltopdf-windows\bin\64bit\wkhtmltopdf'),
'timeout' => false,
'options' => array(),
'env' => array(),
),
'image' => array(
'enabled' => true,
'binary' => 'vendor\wemersonjanuario\wkhtmltopdf-windows\bin\64bit\wkhtmltoimage',
'timeout' => false,
'options' => array(),
'env' => array(),
),
];
copy the wkhtmltopdf-amd64 and wkhtmltopdf-amd64 files to user local bin folder of your system.
cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/
cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/
chmod +x /usr/local/bin/wkhtmltoimage-amd64
chmod +x /usr/local/bin/wkhtmltopdf-amd64
change the binary path of the config snappy file and run the following code:
return [
'pdf' => array(
'enabled' => true,
'binary' => base_path('/usr/local/bin/wkhtmltopdf-amd64'),
'timeout' => false,
'options' => array(),
'env' => array(),
),
'image' => array(
'enabled' => true,
'binary' => '/usr/local/bin/wkhtmltoimage-amd64',
'timeout' => false,
'options' => array(),
'env' => array(),
),
];
Step 5 :
Add Route route/web.php
Route::get('graphs', 'PdfController@graphs');
Route::get('graphs-pdf', 'PdfController@graphPdf');
Step 6 :
Create Controller app/http/controller/PdfController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PdfController extends Controller
{
public function graphs()
{
return view('graph');
}
// function to generate PDF
public function graphPdf()
{
$pdf = \PDF::loadView('graph');
$pdf->setOption('enable-javascript', true);
$pdf->setOption('javascript-delay', 5000);
$pdf->setOption('enable-smart-shrinking', true);
$pdf->setOption('no-stop-slow-scripts', true);
return $pdf->download('graph.pdf');
}
}
Step 7 :
Create Blade File /resources/views/graph.blade.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Generate PDF with Graph Using Snappy in Laravel - Rathorji.in<title/>
<style>
.pie-chart {
width: 600px;
height: 400px;
margin: 0 auto;
}
</style>
{{-- make sure you are using http, and not https --}}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
function init() {
google.load("visualization", "1.1", {
packages: ["corechart"],
callback: 'drawChart'
});
}
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Tea');
data.addColumn('number', 'Populartiy');
data.addRows([
['Bourn Tea', 40],
['Lemon Tea', 22],
['Green Tea', 26],
['Black Tea', 35], // Below limit.
['Special Tea', 35] // Below limit.
]);
var options = {
title: 'Popularity of Types of Tea',
sliceVisibilityThreshold: .2
};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body onload="init()">
<h2 style="text-align: center; ">Generate PDF with Graph Using Snappy in Laravel - Rathorji.in</h2>
<div id="chart_div" class="pie-chart"></div>
</body>
</html>
Run this command:
php artisan serve
Open this URL:
http://localhost:8000/graphs
//Download PDF
http://localhost:8000/graphs-pdf
I hope this example helps you.