Alright, For converting the html to pdf using Puppeteer in Laravel is one of the powerful and more effective solutions whenever we are generating the very high quality pdf documents. We know that this is the one of the most work when we want a pdf file of the html file. For generating the pdf’s file from the html files is one of the common requirements in our web development journey.
Introduction
These features help us on various web applications such as generating the invoices, creating the reports and the users related documentation. While various developers are strictly reliant on the different libraries like Laravel DOM-PDF HTML to PDF conversion, and laravel browsershot for pdf generation.
There is one of the most popular tools like Puppeteer that can offer the most reliable and powerful results especially for the very complex layouts. A real example is trying to convert the html to pdf of an invoice. At that time I used many libraries like dom pdf , spatie as well. But I didn’t receive the result which I wanted.
In this guide we will show you how to convert HTML to PDF in laravel 10 applications using Puppeteer. Also we will explore their alternatives as well like Dompdf and other Laravel PDF Packages. But I gave the most priority to the Puppeteer while converting HTML to PDF using Puppeteer in Laravel.
Converting HTML to PDF using Puppeteer in Laravel 10
Before we starting this process to implementing PDF generation, ensure you have the following things in that place:
Laravel : At this time we assume that you have the laravel 10 or the latest version.
Node.js : Puppeteer is working on Node.js. Because it is a node.js library. So make sure node.js is already installed in your system.
Composer : For using the alternative like laravel dompdf, you need to install the packages. That’s why we need the composer as well.
Installation Puppeteer
You know that we need to install the puppeteer in the node.js. To integrate this with laravel, you need to install it in your project directory.
npm install puppeteer
Puppeteer provides you a most reliable and feature-rich solution for rendering the HTML file into a PDF via the Headless chrome. It is a perfect library for generating PDF files in your laravel application.
Step 1 : Setting Up Puppeteer with Laravel Application.
To use Puppeteer within your laravel application, you’ll need to call the node.js script from PHP. Hoere is the process and how to set that.
1. Create a Node.js Script : You need to create a script that will handle the puppeteer logic for generating the PDF. You can create this script in your main resources folder of your laravel project. Create a file called GeneratePDF.js.
const puppeteer = require('puppeteer');
async function generatePDF(htmlContent, outputPath) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlContent);
await page.pdf({
path: outputPath,
format: 'A4',
printBackground: true
});
await browser.close();
}
const htmlContent = process.argv[2];
const outputPath = process.argv[3];
generatePDF(htmlContent, outputPath).then(() => {
console.log('PDF generated successfully');
}).catch(err => {
console.error('Error generating PDF:', err);
});
2. Call the Node.js Script from Laravel : You can use shell_eec or Laravel’s Process component to run this script from PHP. There is a process given below to integrate it.
Add the following code in your controller file like “GeneratePDFController.php”:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class GeneratePDFController extends Controller
{
public function generate(Request $request)
{
$htmlContent = $request->input('html');
$outputPath = storage_path('app/pdf/output.pdf');
$command = "node " . base_path('generate-pdf.js') . " '$htmlContent' $outputPath";
$output = shell_exec($command);
return response()->download($outputPath);
}
}
3. Make an API Request : Sending the HTML Content via a POST request to this controller. So make sure you already create a route for this. And it will generate the PDF using Puppeteer.
POST /generate-pdf
This method allows you to integrate between the Puppeteer direction into your laravel application for making the conversion of HTML to PDF using Puppeteer in Laravel.
Alternative : By Using DomPDF with Laravel Application
As we know that puppeteer is one of the most reliable solutions for this. But in case your pdf is not more complex and it is not so much massy then you can use DomPDF in your laravel for converting HTML to PDF. Because it also provides a good feature as well.
Step 1 : Install DomPdf
composer require barryvdh/laravel-dompdf
If you decide to go with DomPdf for Laravel HTML to PDF conversion then you can integrate it easily into Laravel. It does not require the node.js.
Step 2 : Generate PDF Using DomPdf in Laravel
Add the Namespace and its class in your laravel controller and go to use it.
GeneratePDFController.php
namespace App\Http\Controllers;
use Barryvdh\DomPDF\Facade as PDF;
use Illuminate\Http\Request;
class GeneratePDFController extends Controller
{
public function generate(Request $request)
{
$htmlContent = $request->input('html'); // Get HTML from the request
$pdf = PDF::loadHTML($htmlContent);
return $pdf->download('generated.pdf'); // Download the generated PDF
}
}
Step 3 : Make an API Request
It is exactly similar to the Puppeteer, you need to send the HTML file or content to this controller and download the generation PDF via POST request.
POST /generate-pdf
By using DomPdf, you can easily generate the PDF directly in your laravel application without using node.js and puppeteer. But keep in mind that the complex CSS and Javascript might not be rendered with the DomPdf as compared to Puppeteer.
There are other packages as well like laravel-pdf, laravel-snappy. But I don’t recommend that because they now provide me with that much results. But it depends on your project’s requirements.
Conclusions
Puppeteer offers us the modern and most powerful solution for converting the HTML to PDF using headless Chrome. There are several other Laravel packages as well like DomPdf that provide easy alternatives for developers who prefer to work with the PHP based tools.
Now it depends on your project requirements, if you are using javascript and more complex CSS then make sure to go with Puppeteer. But if you are looking for a lightweight PHP solution and that does not require much css then you can go with DomPdf. Both options are good for you based on your requirements.
Now at last, we provide you with full knowledge about it. If you like this article then please share with your friends who are working in coding. Also please share this with your social media handles as well.