How to convert a PNG Array to PDF in PHP

Oftentimes the best way to present content is in PDF format. Today, we are not going to let the hassle of format conversion stand in the way of that goal. We will be skipping straight past it, in fact, with the use of a helpful API that will allow us to convert a PNG array directly into a PDF. Let me show you how.

Our first step is simply that of installation for our API client. To do this, simply run this command here from your command line.

composer require cloudmersive/cloudmersive_document_convert_api_client

After installation has finished, we will have access to our API’s functions, from which we will call this one:

<?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_file1 = "/path/to/file"; // \SplFileObject | First input file to perform the operation on.$input_file2 = "/path/to/file"; // \SplFileObject | Second input file to perform the operation on.$input_file3 = "/path/to/file"; // \SplFileObject | Third input file to perform the operation on.$input_file4 = "/path/to/file"; // \SplFileObject | Fourth input file to perform the operation on.$input_file5 = "/path/to/file"; // \SplFileObject | Fifth input file to perform the operation on.$input_file6 = "/path/to/file"; // \SplFileObject | Sixth input file to perform the operation on.$input_file7 = "/path/to/file"; // \SplFileObject | Seventh input file to perform the operation on.$input_file8 = "/path/to/file"; // \SplFileObject | Eighth input file to perform the operation on.$input_file9 = "/path/to/file"; // \SplFileObject | Ninth input file to perform the operation on.$input_file10 = "/path/to/file"; // \SplFileObject | Tenth input file to perform the operation on.try {$result = $apiInstance->convertDocumentPngArrayToPdf($input_file1, $input_file2, $input_file3, $input_file4, $input_file5, $input_file6, $input_file7, $input_file8, $input_file9, $input_file10);print_r($result);} catch (Exception $e) {echo 'Exception when calling ConvertDocumentApi->convertDocumentPngArrayToPdf: ', $e->getMessage(), PHP_EOL;}?>

You can input up to 10 PNG files at once to be converted. If you need to work with more than 10 files, you can call the function mergeDocumentPdf to merge the finished PDFs together.

Image for post
Image for post

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