HTML to PDF convertor for cc reports.
- Require this package in your composer.json and update composer.
$ composer require barryvdh/laravel-snappy
- After updating composer, add the ServiceProvider to the providers array in config/app.php
Barryvdh\Snappy\ServiceProvider::class,
- Use the Facade for shorter code. Add this to your facades:
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
- Finally you can publish the config file:
$ php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
- Require wkhtmltopdf package in your composer.json and update composer.
LINUX
$ composer require h4cc/wkhtmltopdf-amd64 0.12.x
WINDOWS
composer require wemersonjanuario/wkhtmltopdf-windows "0.12.2.3"
- The main change to this config file (config/snappy.php) will be the path to the binaries.
LINUX
'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),
WINDOWS
'binary' => base_path('vendor/wemersonjanuario/wkhtmltopdf-windows/bin/64bit/wkhtmltopdf.exe'),
- Create reports directory in your resources/views and add this files there
'footer.blade.php' -> footer for all report pages
'header.blade.php', -> header for all report pages
'report.blade.php', -> all report pages
- Add css files to public/css and js files to public/js
css files => [app.css, fonts.css]
js files => [chart.js, doughnot.js]
- Set this options in config/snappy.php
'pdf' => [
enabled' => true,
'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),
'timeout' => false,
'options' => [
'enable-javascript' => true,
'javascript-delay' => 1000,
'enable-smart-shrinking' => true,
'no-stop-slow-scripts' => true,
'margin-top' => 15,
'margin-bottom' => 12,
'margin-left' => 0,
'margin-right' => 0,
],
'env' => [],
],
- Finally, add this code to your report controller
public function report()
{
$viewName = 'reports.report';
$header = View::make('reports.header');
// for some reason $footer is not working (need to check this)
// $footer = View::make('reports.footer');
$pdf = PDF::loadView($viewName)->setOrientation('landscape');
$pdf->setOption('enable-local-file-access', true);
$pdf->setOption('header-html', $header);
// $pdf->setOption('footer-html', $footer);
// download pdf file
return $pdf->download('report.pdf');
//stream pdf file
// return $pdf->download('report.pdf');
//open pdf as view
// return view($viewName);
}