Convert HTML to a PNG Image Array in PHP

Cloudmersive
1 min readAug 4, 2021

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.

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


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

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet

Write a response