How to Generate PDFs in Laravel 11: A Step-by-Step Guide

by Tarun Korat Apr 06, 2024 Total Views: 67

The package can convert a webpage to an image or pdf. The conversion is done behind the scenes by Puppeteer which controls a headless version of Google Chrome.


Learn how to create PDF documents dynamically in Laravel 11 using popular libraries and packages. This comprehensive tutorial covers everything you need to know, from setting up the environment to generating PDFs from database data and custom views. Boost your Laravel skills and enhance your application by adding PDF generation capabilities effortlessly!


Requirements


  • PHP Version greater thant 8.2 >=
  • Laravel 11


Installation


Run below command in your Terminal.

composer require spatie/browsershot

This packages requires puppeteer to be installed. You can find installation instructions on the requirements page.


Create Route
Route::get('generate-pdf', [PDFController::class, 'index']);


Create Controller
use Spatie\Browsershot\Browsershot;

// a pdf will be saved
Browsershot::url('https://example.com')->save('example.pdf');


Thats it. I hope this article will help you.

Tags: Laravel Laravel 11 PDF generation PDF in Laravel Laravel PDF tutorial

Please login to create comment.

Author
Author Image

"Tarun Korat, an avid wordsmith and passionate storyteller, pours his creativity into every piece he writes. With a knack for weaving engaging narratives, Tarun captivates readers with his insightful perspectives on life, love, and everything in between. Join him on his literary journey as he shares his thoughts and musings through the power of words."

- Tarun Korat
Follow Me: