Aug 4, 2021

1 min read

Convert HTML to a PNG Image Array in PHP

Converting just about any file format to an image can be quite the ordeal if you’re attempting it manually; between rendering the file, rasterizing it into an image, and splitting the image into pages, you can be looking at a significant amount of processing time. However, by utilizing the following API in PHP, you can instantaneously convert HTML to a PNG image array, one for each page.

To begin the operation, we run this command to install the SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next, we can call the function by inputting the HTML file into the following code — if you are using external files in your HTML (such as images), you will input an absolute URL to the file instead.

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\ConvertDocumentApi(

new GuzzleHttp\Client(),
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
try {
$result = $apiInstance->convertDocumentHtmlToPng($input_file);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentHtmlToPng: ', $e->getMessage(), PHP_EOL;

And boom, we’re done! Nothing to it.