How to generate and download pdf using Laravel Snappy

Laravel is a popular PHP web application framework used by developers to build high-quality, scalable web applications. One common requirement in web development is the ability to generate and download PDF documents. In this article, we will explore how to use Laravel Snappy, a popular package that provides a simple and efficient way to generate PDFs in Laravel.

Laravel Snappy is a wrapper around the wkhtmltopdf command-line tool, which converts HTML pages to PDF documents. The package allows developers to generate PDFs from Laravel views, blade templates, or any HTML page. The package also provides a simple API for configuring and customizing the PDF output.

In this step-by-step guide, we will explore how to install and use Laravel Snappy to generate and download PDF documents in Laravel.

Step 1: Install Laravel Snappy

The first step is to install Laravel Snappy. To install the package, open a terminal window and navigate to the root directory of your Laravel project. Then, run the following command:

composer require barryvdh/laravel-snappy

This command will install the Laravel Snappy package and its dependencies.

Read Also : How to generate and download pdf in laravel using dompdf

Step 2: Configure Laravel Snappy

After installing the package, you need to configure it in your Laravel application. Open the config/app.php file in your Laravel project and add the following line to the providers array:

Barryvdh\Snappy\ServiceProvider::class,

Then, add the following line to the aliases array:

'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,

These lines will register the Laravel Snappy service provider and alias the SnappyPdf facade.

Next, you need to configure the package to use the wkhtmltopdf command-line tool. You can do this by adding the following lines to the config/snappy.php file:

'pdf' => [    'enabled' => true,    'binary' => '/usr/local/bin/wkhtmltopdf',    'timeout' => false,    'options' => [],    'env' => [],],

These lines configure the package to use the wkhtmltopdf binary located at /usr/local/bin/wkhtmltopdf. You may need to adjust the path to the binary depending on your server setup.

Step 3: Generate PDF Documents

Now that Laravel Snappy is installed and configured, you can start generating PDF documents. Laravel Snappy provides a simple API for generating PDFs from views or HTML pages.

To generate a PDF from a view, you can use the following code:

$pdf = PDF::loadView('pdf.invoice', $data);return $pdf->download('invoice.pdf');

This code loads the pdf.invoice view and passes in the $data variable. It then generates a PDF from the view and downloads the resulting PDF file.

To generate a PDF from an HTML page, you can use the following code:

$pdf = PDF::loadFile('http://example.com');return $pdf->download('example.pdf');

This code loads the HTML page at http://example.com and generates a PDF from it. It then downloads the resulting PDF file.

Step 4: Customize PDF Output

Laravel Snappy provides a range of options for customizing the PDF output. You can use these options to adjust the page size, orientation, margins, and more.

To customize the PDF output, you can pass an array of options to the loadView or loadFile methods. For example:

$options = [    'page-size' => 'A4',    'orientation' => 'landscape',    'margin-top' => '10mm',    'margin-bottom' => '10mm',    'margin-left' => '10mm',      'margin-right' => '10mm', ];$pdf = PDF::loadView('pdf.invoice', $data, $options); return $pdf->download('invoice.pdf');

In this example, we are passing an array of options to the loadView method. These options specify that the page size should be A4, the orientation should be landscape, and the margins should be 10mm on all sides.

Laravel Snappy provides a range of options for customizing the PDF output, including options for adding headers and footers, adjusting the DPI, and more. You can find a complete list of options in the Laravel Snappy documentation.

Conclusion

In this article, we explored how to use Laravel Snappy to generate and download PDF documents in Laravel. We started by installing and configuring Laravel Snappy, then looked at how to generate PDFs from views and HTML pages. We also explored how to customize the PDF output using a range of options.

Laravel Snappy provides a simple and efficient way to generate PDF documents in Laravel. By following the steps outlined in this guide, you should now be able to use Laravel Snappy to generate and download PDF documents in your Laravel application.

Comments