Quick Summary:

Generating PDFs is one of the most common things to do for a number of web applications, the reason is that it enables the user to easily download and share documents that can be viewed and printed easily. Laravel, the renowned PHP framework within the web application development marketplace has also brought up a number of tools and libraries that allows you to build web applications including tools for generating PDFs. Therefore, in this blog post, we have covered one such renowned package that is Browsershot Laravel to generate PDFs with Headless Chrome and Laravel, which enables you to generate PDFs without the limitations of traditional PDF generation tools.

Table of Contents

Introduction

The current market offers numerous PDF-generation tools, but traditional options often come with limitations such as additional software dependencies or lack of customization. Browsershot Laravel, a package for Laravel, addresses these issues by enabling PDF generation with Headless Chrome, providing a solution for managing CSS while creating PDFs that may not be achievable with other packages like DomPDF or Snappy.

Laravel, a popular PHP framework for building web applications, is known for its rich features. However, some packages for generating PDF files may struggle with handling certain CSS3 rules, such as gradient backgrounds, leading to issues like incomplete rendering of webpages in PDF output.

Step-by-Step Guide to Generate PDF Using Browsershot Laravel

Browsershot Laravel Package offers a service that utilizes a headless instance of Chrome to generate PDFs or images from URL links, which can be web pages or HTML. The conversion process is handled by Puppeteer, controlling a headless version of Google Chrome behind the scenes. By using Laravel Browsershot, you can overcome CSS-related challenges encountered with other packages like MPDF, and obtain clear CSS and PDF output that aligns with your requirements.

Without further delay, let’s dive into a step-by-step guide on how to achieve PDF generation with Laravel Browsershot.

Step 1: Installation and Setup

Prerequisites for using Browsershot Laravel

The first step to start using Browsershot in Laravel for generating PDFs using Headless Chrome and Laravel is to meet up the basic requirements that are as follows:

  • PHP 8+
  • Nodejs
  • NPM

Puppeteer and Libraries

Then, install Puppeteer which will enable and install the headless chromium needed for our project.

Copy Text
npm install puppeteer --global

Setup Laravel App and Browsershot

Create your Laravel project:

Copy Text
composer create-project --prefer-dist laravel/laravel:^8.0 myapp

Install browsershot package:

Go to your Laravel myapp projects root directory and enter the following command:

Copy Text
composer require spatie/browsershot

This will update the dependency list in your composer.json file and install browsershot in your vendor directory.

Want to make sure your PDFs are flawless?
Hire Laravel developer with experience in Browsershot Laravel and get the perfect documents every time!

Step 2: Usage

Implement Package Usage and Check Library is Working Well

To implement Browsershot, firstly we need to create a controller:

php artisan make:controller TestPdfController

Copy Text
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\Browsershot\Browsershot; // reference browsershot

class TestPdfController extends Controller
{
    //write funtional logic to generate pdf
   public function printPdf()
     { 
	 $path = public_path("/pdf_temp/");
           if (!File::exists($path)) {
              File::makeDirectory($path, 0755, true);
           }
    	Browsershot::url('https://google.com')
        ->format('A4')
        ->showBackground()
        ->save($path.'urlToPdf.pdf');
echo 'Pdf generated at <i>public/pdf_temp/urlToPdf.pdf</i>';
     }
}

Step 3: Generate PDF using Laravel view

Develop a function that generates a PDF and stores it in the designated storage directory.

  • Add the below code to your controller:
Copy Text
public function printPdf()
    {           
        $view =  view('welcome')->render();        

        $path = public_path("/pdf_temp/");
        if (!File::exists($path)) {
            File::makeDirectory($path, 0755, true);
        }
        Browsershot::html($view)->save($path.'htmlToPdf.pdf');       
          
    }

Now to use the function we need the route to execute the process for that:

  • Open your web.php file
  • First reference your controller:
Copy Text
use App\Http\Controllers\TestPdfController;
  • Second, create your route:
Copy Text
Route::get('/test-pdf', [TestPdfController::class,'printPdf']);
  • Enter the URL (http://your-website.com/test-pdf) in your web browser to check if the PDF has been successfully generated. If there are no errors caused by Puppeteer, you will see a loading page.
  • After checking for errors, go to your public/pdf_temp directory to find the PDF files.

HTML to PDF.pdf

HTML to PDF.pdf

URL to PDF.pdf

URL to PDF.pdf

If you are facing any errors related to the Puppeteer please refer to the Spatie Documentation.

Conclusion

Using Browsershot Laravel for PDF generation with Headless Chrome and Laravel, or converting HTML to PDF in a Laravel application, provides a streamlined and hassle-free approach. This eliminates the need for complex third-party applications and simplifies CSS management during PDF creation.

With a user-friendly package and straightforward setup process, developers can generate PDFs with minimal effort, making it an ideal choice for simplifying the PDF generation process. However, if you are a business owner seeking expertise in Laravel-based web application development or upgrades. Hire Laravel Development Company like Bacancy to optimize existing applications or create new ones tailored to the interests and needs of the target audience.

Impress Your Clients With Beautiful PDF Documents

Browsershot Laravel is the tool you need. Step up your website game and create eye-catching PDFs. Try it now and elevate your website’s impact!

BOOK FREE CONSULTATION

Build Your Agile Team

Hire Skilled Developer From Us

[email protected]

Your Success Is Guaranteed !

We accelerate the release of digital product and guaranteed their success

We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.

How Can We Help You?