Friday, April 21, 2017

Generate PDFs in Laravel 5.x with Snappy & Wkhtmltopdf

At first we need to install Wkhtmltopdf on our server. To do so we need to add following lines under "require" section on composer.json file as follows:

For 32 bit server:



"require": {
    "h4cc/wkhtmltopdf-i386": "0.12.x",
    "h4cc/wkhtmltoimage-i386": "0.12.x"
}




And for 64 bit server:


"require": {
    "h4cc/wkhtmltopdf-amd64": "0.12.x",
    "h4cc/wkhtmltoimage-amd64": "0.12.x"
}


Now time to install wkhtmltopdf. Open command prompt and navigate to your project location and execute below command to install packages into your project:

composer update h4cc/wkhtmltopdf-i386 --lock
composer update h4cc/wkhtmltoimage-i386 --lock
Or you can download wkhtmltopdf from here. Or you can download also from here.

For windows you do not do anything above. Just download installer for windows version, install it in your machine, done. 

After installation done, need to install "Laravel-Snappy", to do so add following to "composer.json" as follows:


"require": {
    "barryvdh/laravel-snappy": "0.3.x"
}

Open command prompt and navigate to your project location and execute below command to install packages into your project:

composer update barryvdh/laravel-snappy --lock

Next step is to add the line "Barryvdh\Snappy\ServiceProvider::class" to file "config/app.php" under the section "providers" as follows:


'providers' => [Barryvdh\Snappy\ServiceProvider::class]

Its time to add an alias, do the following in "config/app.php" under "aliases" section:


'aliases' => [
    'SnappyPDF' => Barryvdh\Snappy\Facades\SnappyPdf::class
]

Now copy "vendor/barryvdh/laravel-snappy/config/snappy.php" file to "/config" folder and modify as follows:

Location of binary is your wkhtmltopdf installation location. You must careful with the path.

Binary path will be as follows:

For Linux Server with project location: "binary" => ...
base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64')

For Linux Server with fixed location: "binary" => ...
"/usr/local/bin/wkhtmltopdf-amd64"

And for Windows with installation location: "binary" => ...
'"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf\bin\wkhtmltopdf.exe"'

<?php
return array(
    'pdf' => array(
        'enabled' => true,
        'binary'  => 'C:\PROGRA~1\wkhtmltopdf\bin\wkhtmltopdf.exe',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    ),
    'image' => array(
        'enabled' => true,
        'binary'  => 'C:\PROGRA~1\wkhtmltopdf\bin\wkhtmltoimage.exe',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    )

);

Execute following command:
php artisan vendor:publish

Below is a code snippet to stream pdf to browser:

<?php
namespace App\Http\Controllers;

use SnappyPDF;
use Illuminate\Routing\Controller as BaseController;

class HomeController extends BaseController
{
    public function pdf()
    {
        set_time_limit(60 * 5);
        $d1 = array();
        $d2 = array();

        $pdf = SnappyPDF::loadView('home.pdf', compact('d1', 'd2'));

        //Below line code will save pdf to "public" folder
        //$pdf->save('Output.pdf'); die();

        //Below line code to download pdf
        //return $pdf->download('Output.pdf');

        //Below line code will stream PDF to browser
        return $pdf->stream('Output.pdf');
    }

}

And thats it, you have integrated Wkhtmltopdf in your Laravel project using Snappy.

No comments:

Post a Comment