Generate PDF with Graph Using Snappy in Laravel

Admin   Laravel   182  2021-03-16 03:30:06

Hi Guys,

In this blog, I will show you how to generate pdf with graph in laravel using snappy.laravel Snappy is better than other packages. Because its support javascript. We need to install wkhtmltopdf and wkhtmltoimage before installing laravel snappy package.

Laravel Snappy is a library that uses Wkhtmltopdf to convert HTML to PDF. generate pdf with graph in laravel using snappy package.

Here I will give you full example for laravel generate pdf with graph using snappy.So let's follow bellow step by step.

Step 1 : Install wkhtmltopdf and wkhtmltoimage

In this step You can install wkhtmltopdf convert html to pdf. So Let's install them.

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

In this step, You can install laravel fresh app. So open terminal and put the bellow command.

 

composer create-project --prefer-dist laravel/laravel blog

Step 3 : Install Laravel Snappy

In this step, You can install snappy package. So open terminal and put the bellow command.

 

composer require barryvdh/laravel-snappy

Run succefully above command then after open config/app.php and put the bellow code.

config/app.php

 

'providers' => [

Barryvdh\Snappy\ServiceProvider::class,

],

'aliases' => [

'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,

'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,

]

Now publish snappy config file using bellow command so lets open terminal and run bellow command:

 

php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"

Run Successfully above command thenafter open snappy config file add configuration.

For Windows

The pdf and image’s binary will load from wemersonjanuario\wkhtmltopdf-windows package. change in snappy file.

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

The pdf and image’s binary will load from wemersonjanuario\wkhtmltopdf-windows package. change in snappy file.

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(),

),

];

If error 126 Occurs: If you see the error 126 (not for Windows users), then you can fix the error in this way:

You need to copy the wkhtmltopdf-amd64 and wkhtmltopdf-amd64 files to user local bin folder of your system. Let’s do this:

 

cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/

cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/

Now give the permission for this folder. Run bellow command:

 

chmod +x /usr/local/bin/wkhtmltoimage-amd64

chmod +x /usr/local/bin/wkhtmltopdf-amd64

Now change the binary path of the config snappy file put the bellow 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

now, we need to add route for pdf controller in laravel application. so open your "routes/web.php" file and add following route.

route/web.php

 

Route::get('graphs', '[email protected]');

Route::get('graphs-pdf', '[email protected]');

Step 6 : Create Controler

Here this step now we should create new controller as PdfController. So run bellow command and create new controller.

app/http/controller/PdfController.php