How to convert an HTML Document to PNG Array in PHP
1 min readApr 15, 2020
Today’s goal is pretty straightforward: to use PHP to render an HTML document, then export it as a series of PNG images. Want to know a secret? I know how to get this done instantly, with almost no work involved. How about I show you.
Kicking things off: client installation.
composer require cloudmersive/cloudmersive_document_convert_api_client
Following that is our function call:
<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\ConvertDocumentApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$input_file = "/path/to/file"; // \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;}?>
Whew! Already done. APIs make everything a breeze, don’t they. This same client contains numerous other functions, such as more format conversions as well as document editing and template application.