Skip to content

Coding several pages then converting them into PDF file using laravel-snappy library.

Notifications You must be signed in to change notification settings

dumaaas/html-to-pdf-laravel

Repository files navigation

City Curator Report

HTML to PDF convertor for cc reports.

Laravel-Snappy installation

  1. Require this package in your composer.json and update composer.
$ composer require barryvdh/laravel-snappy
  1. After updating composer, add the ServiceProvider to the providers array in config/app.php
Barryvdh\Snappy\ServiceProvider::class,
  1. Use the Facade for shorter code. Add this to your facades:
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
  1. Finally you can publish the config file:
$ php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
  1. 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"
  1. 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'),
  1. 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
  1. Add css files to public/css and js files to public/js
css files => [app.css, fonts.css]
js files => [chart.js, doughnot.js]
  1. 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' => [],
],
  1. 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);
}

About

Coding several pages then converting them into PDF file using laravel-snappy library.

Topics

Resources

Stars

Watchers

Forks