How To Create Better PDFs With Laravel Easily

How To Create Better PDFs With Laravel Easily

laravel pdf

Summary

  1. Introduction
  2. Installation & Setup
  3. Usage
  4. Conclusion
  5. References

1. Introduction

Generating PDF files from HTML is a common use case for web applications. Whether you need to create invoices, reports, or any other type of document, the ability to generate PDFs can be a valuable feature for your users.

Laravel is a popular PHP framework that provides a range of tools and features for building web applications. There are many different packages to generate PDF files, but most of them are not able to handle some CSS3 rules, like DomPDF and Snappy.

I had some issues using DomPDF because it doesn’t handle all the css3 rules, like gradient background. So I needed to use a tool that be able to print the page as we can see it in our browser.

In this article, we will discuss a tool that uses a headless Chrome/Chromium in the background to generate the PDF file same as in the HTML page, even with CSS3 style rules. It is called Browsershot.

By the end of this article, you will have a good understanding of how to generate PDF files in Laravel in a easy way using the Browsershot library.

2. Installation & Setup

2.1. Requirements

I’m working on Windows with WSL2 (Windows Subsystem for Linux). I really like it, but I couldn’t install and setup it in WSL2 + Docker. Anyway, using the artisan server is quite simple to setup it. So the commands below are assuming you are in a linux system, artisan server with already composer, php8+, Nodejs, and NPM installed.

2.2. Puppeteer and Libraries

We need install some requirements before adding the browsershot package.

First, we need to install the puppeteer. This library will install a headless chromium that we will use.

npm i puppeteer

I needed installing some libs in order to get the library working

sudo apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libnss3 lsb-release xdg-utils wget ca-certificates

2.3. Browsershot

And finally, Browsershot can be installed through Composer by running the command:

composer require spatie/browsershot

We’re all ready to get our hands dirty.

3. Usage

3.1. Check whether everything is working well

Now, we’ll create a route in order to test if the library is correctly installed.

//file routes/web.php
//import the Browsershot class
use Spatie\Browsershot\Browsershot;

//This route will generate a PDF with the google page printed
Route::get('browsershot', function() {
    Browsershot::url('https://google.com')
        ->format('A4')
        ->showBackground()
        ->save('browsershot.pdf');
    echo 'Pdf generated at <i>public/browsershot.pdf</i>';
});

Run this to start the artisan server

php artisan serve

Now, we can access our route: http://127.0.0.1:8000/browsershot and check if the file was generated.

Pdf generated at public/browsershot.pdf

3.2. Generating PDF using a Laravel View

As we’re going to print our own route, we don’t need to create a route, access the url and print it. It’s simpler than this.

We can render our view and call the method ->html():

//assuming we already have a view created at /resources/views/pdf.blade.php

Route::get('browsershot', function() {
  //Getting the view rendering
  $view = view('pdf', [
      'name' => 'Robson Sousa',
      'country' => 'Brazil',
  ])->render();

  //We can use the ->html method passing the rendered view
  Browsershot::html($view)
      ->format('A4')
      ->showBackground()
      ->save('browsershot.pdf');
});

You can check the whole documentation for PDF generation here.

4. Conclusion

Generating PDFs with Laravel and Browsershot is a powerful and efficient way to create high-quality and dynamic documents. Browsershot’s ability to render HTML and CSS allows for greater flexibility and control over the final product, and provides a robust solution for generating PDFs in any web application.

With a little bit of setup, you can easily create professional-looking documents with minimal effort, making this an ideal choice for any developer looking to add PDF generation capabilities to their application, without needed to be tied to old HTML/CSS.

I strongly recommend you read this post about a Definitive Guide for Handling Date with Laravel.

5. References

Browsershot Docs

Puppeteer

Share this content:

Robson Sousa

I’m Brazilian, married to Ludmila and Júlia’s father. I have lived in Timon, Teresina, Uberaba and now Goiânia. I had my first job as a Software Developer at 2010. From there to here, I have met so much talented people, learnt a lot from each different experience and collegues, and shared knowledge.

Subscribe
Notify of
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
11 meses atrás

[…] I strongly recommend you read this post about How To Create Better PDFs With Laravel Easily. […]