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

SQL: sneak peek

show databases; use mysql; show tables; select * from component; describe component; create database sql_intro; show databases; use sql_intro; create table emp_details (Name varchar(25), Age int, gender char(1), doj date, city varchar(15), salary float); describe emp_details; insert into emp_details  values("Jimmy",35,"M","2005-05-30","Chicago",70000), ("Shane",30,"M","1999-06-25","Seattle",55000), ("Marry",28,"F","2009-03-10","Boston",62000), ("Dwayne",37,"M", "2011-07-12","Austin", 57000), ("Sara",32,"F","2017-10-27","New York",72000), ("Ammy",35,"F","2014-12-20","Seattle",80000); select * from emp_details; select distinct city from emp_details; select count(name) as count_name from emp_details; select avg(salary) from emp_details; select name, age...