How to convert HTML to PDF in PHP

Here is a very simple way to handle conversion of HTML to PDF using our API. Let’s begin with adding the following reference:

"require": {
"cloudmersive/cloudmersive_document_convert_api_client": "^1.4",
}

Now, we call convertWebHtmlToPdf as follows, with our desired URL as the input:

<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\ConvertWebApi(


new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\HtmlToPdfRequest(); // \Swagger\Client\Model\HtmlToPdfRequest | HTML to PDF request parameters
try {
$result = $apiInstance->convertWebHtmlToPdf($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertWebApi->convertWebHtmlToPdf: ', $e->getMessage(), PHP_EOL;
}
?>

Done! Almost too easy, right? Our Convert API can be used for all sorts of other useful file conversions and alterations, so be sure to look at our documentation for the full list.

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store