Skip to main content

laravel snappy pdf

Install this:

then install this two,

$ composer require h4cc/wkhtmltopdf-i386 0.12.x
$ composer require h4cc/wkhtmltoimage-i386 0.12.x
Then, composer require barryvdh/laravel-snappy
Then,
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
And Now,
copy the wkhtmltopdf.exe and wkhtmltoimage.exe files to the above h4cc folder which
is present inside vendor folder of laravel. and set the path to
snappy.php file-> see the snappy.php file which is given below:


[NOTE: see the code which i've commented out. You can run & check every line of code]

Route::get('/'function () {
    $data = [
        [
            'name'=>'Audi',
            'logo'=>'audi.jpg'
        ],
         [
            'name'=>'Rolex',
            'logo'=>'audi.jpg'
        ],
    ];
    $pdf    = PDF::loadView('pdf.tutorial',['cars'=>$data]);
    // $html = "<h1>Hello World</h1>";
    // $pdf    = PDF::loadHtml($html);
    $pdf->setOrientation('landscape');
    // $pdf->setOption('header-left','[page]');
    // $pdf->setOption('header-right','[date]');//[page] or [date]
    
    
    // $pdf->setOptions([
    //     // 'header-left'=>'[page]',
    //     // 'header-right'=>'[date]',
    //     // 'footer-right'=>'Bitfumes',
    //     // you can set this options inside snappy.php files option
    // ]);

    //$pdf->setOption('footer-html',view('pdf.footer'));

    // return $pdf->download('hello.pdf');
    return $pdf->stream('hello.pdf');
});




Snappy.php file looks like this:


'pdf' => [
        'enabled' => true,
        'binary'  => env('WKHTML_PDF_BINARY'base_path('vendor/h4cc/wkhtmltopdf')),
        'timeout' => false,
        'options' => [
            'header-left'=>'[page]',
            'header-right'=>'[date]',
            'footer-right'=>'Bitfumes',
        ],
        'env'     => [],
    ],
    
    'image' => [
        'enabled' => true,
        'binary'  => env('WKHTML_IMG_BINARY',base_path('vendor/h4cc/wkhtmltoimage')),
        'timeout' => false,
        'options' => [],
        'env'     => [],
    ],


tutorials.blade.php blade is present inside pdf folder
& tutorials.blade.php file looks like this:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Tutorial PDF</title>
    <style>
        th,td{
            border1px solid black;
            border-collapsecollapse;
        }

        th,td{
            padding15px;
            text-alignleft;
        }

        table{
            width100%;
            background-color#f1f1c1;
        }
    </style>
</head>
<body>
    <table class="table table-light">
        <thead>
            <tr>
                <th>S. No</th>
                <th>Name</th>
                <th>Image</th>
            </tr>
        </thead>
        <tbody>
            @foreach($cars as $car)
            <tr>
                <td>1. </td>
                <td>{{$car['name']}}</td>
                <td>
                    <img src="{{public_path('/'.$car['logo'])}}" width="150" alt="">
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</body>
</html>


HOPE THIS WILL HELP SOMEBODY IN THIS WORLD! HAPPY CODING. (PRITAM DATTA)



Comments

Popular posts from this blog

Laravel Vuejs Basic Setup

In this article we will see how to setup vue with laravel. 1) Create a laravel project: laravel new laravue 2) In web.php create a temporary route:  Route :: get ( '/test' ,  function  () {      return   view ( 'welcome' ); }); So, when you will see the welcome page once you hit the  http://laravue.test/test  url. 2) In terminal run:  npm install 3) npm install vue (In package.json file you will see that vue dependency has been added successfully) 4) Now go to resources -> js -> app.js file. Here we will have to load the vue. write the following lines in app.js.  require ( './bootstrap' ); window . Vue  =  require ( 'vue' ); const   app  =  new   Vue ({      el :   '#app' }); (Here, vue js will execute and control anything that will be inside the app id. So, for our example  let's put the app id inside the welcome view). So, inside welcome.blade.php 's body...