Packages

wkhtmltopdf is an open source (LGPLv3) command line tool to render HTML into PDF. It runs entirely “headless” and do not require a display or display service.

Snappy (KnpLabs/snappy) is a PHP wrapper for wkhtmltopdf.

barryvdh/laravel-snappy is a Laravel ServiceProvider for Snappy.

Notes for DOMPDF: DOMPDF only supports limited CSS, therefore it is not ideal for HTML-to-PDF exports that contain advanced CSS style (grid, flex, etc.).

Steps

  1. Download the correct wkhtmltopdf version based on the operating system (for example: Windows - Installer (Vista or later) - 64-bit for Windows 10) to a preferred path (usually in vendor folder).
  2. Install barryvdh/laravel-snappy in your Laravel project:
composer require barryvdh/laravel-snappy
  1. (Optional) Publish laravel-snappy config file:
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
  1. Config path to wkhtmltopdf in .env file (or config in config/snappy.php). If using a Windows system, you may need to use absolute path with correct quotations.
WKHTML_PDF_BINARY='"[path_to_root]\vendor\wkhtmltox\bin\wkhtmltopdf"'
  1. Build a blade file as export template. Better not to use external CSS.
  2. Pass data to template and view/download the exported PDF file.
use Barryvdh\Snappy\Facades\SnappyPdf;

// download
return SnappyPdf::loadView('template', [...data])->download('file.pdf');

// view
return SnappyPdf::loadView('template', [...data])->inline('file.pdf');
  1. View more available functions on: barryvdh/laravel-snappy.