HTML to PDF API
Convert HTML files or live web page URLs to PDF with Chrome headless: pixel-perfect rendering, JS/CSS injection, AdBlock.
HTML to PDF conversion may seem like a straightforward task, but it can be challenging to implement and maintain, thereby complicating your project. Thus, many nowadays solutions come from third-party, rigid, and robust service providers. We are going to use a simple API call to take care of the conversion.
Let's begin by installing a ConvertAPI PHP Client:
composer require convertapi/convertapi-php
Note: if you don't want to use Composer, please follow the manual installation guide.
Once you have the library installed, simply run this code snippet:
ConvertApi::setApiCredentials('api_token');
$result = ConvertApi::convert('pdf', [
'File' => '/path/to/my_file.html',
], 'html'
);
$result->saveFiles('/path/to/result/dir');
That's it, just replace your API Token and path to your file, and it should work straight away.
If you want a more advanced conversion with control over whether to run javascript on the web page, remove cookie warning popups, ads, etc please refer to the complete guide including an interactive demo. You will also find an auto-generated snippet at the bottom of the page!
If you want to convert a web page that is hosted and accessible online, pass its address with the Url parameter instead of uploading a file - the HTML to PDF converter accepts either an HTML file or a live web page URL.
Happy coding!