Convert HTML to PDF using Headless Chrome in PHP
There are times when you want to convert HTML to PDF. For instance, if you’re building a web application that allows users to generate PDFs of their invoices, you might want to convert the HTML of the invoice to PDF. Or if you’re building a blog, you might want to convert your blog posts to PDFs.
Sure, there are a lot of dedicated libraries like wkhtmltopdf that can do this for you.
But this can also be done using Headless Chrome as well. And the best part is, you don’t need to install any additional libraries to do this.
Using Headless Chrome
To use Headless Chrome, we can install the official headless chrome/chromium instances using the Composer package manager like so.
composer require chrome-php/chrome
A simple HTML to PDF converter
Once the chrome-php/chrome
package is installed, we can start using it to convert HTML to PDF. For this, we can create a regular class called HtmlToPdfConverter
like so.
Note: For convenience, I’m implementing this in a Laravel application. But you can use this in any PHP application.
namespace App\Services;
use HeadlessChromium\BrowserFactory;
class HtmlToPdfConverter
{
/**
* Render a PDF from the given HTML.
*/
public static function render(string $html): string
{
$browser = (new BrowserFactory())->createBrowser([
'windowSize' => [1920, 1080]
]);
$page = $browser->createPage();
$page->setHtml($html);
return base64_decode(
$page->pdf([
'printBackground' => true
])->getBase64()
);
}
}
As you can tell, this class has a static render
method which accepts the HTML as a string and returns the PDF as a string.
Here, we’re using the chrome-php/chrome
package to create a browser instance and a page instance. Then, we’re setting the HTML to the page using the setHtml
method. And finally, we’re using the pdf
method to generate the PDF from the HTML.
The pdf
method accepts an array of options. Here, we’re passing the printBackground
option which is set to true
. This option is used to print the background graphics of the HTML as well. If you don’t want to print the background graphics, you can set this option to false
.
Using the converter
Now, we can use this class to convert HTML to PDF like so.
use App\Services\HtmlToPdfConverter;
Route::get('/html-to-pdf', function () {
$pdf = HtmlToPdfConverter::render('<h1>Hello World</h1>');
return response($pdf, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="hello.pdf"'
]);
});
As you can tell, we’re calling the render
method of the HtmlToPdfConverter
class and passing the HTML to it. Then, we’re returning the PDF as a response with the Content-Type
header set to application/pdf
.
So, when you visit the /html-to-pdf
route, we’ll get the given HTML converted to PDF like how we wanted.
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.