How to Convert WebP to PDF using a Free API in PHP

Cloudmersive
2 min readMay 6, 2024

We can easily store WebP files as PDFs by calling a free PDF conversion API in our image upload/image processing workflows.

We just need to copy from the complementary, ready-to-run PHP code examples provided further down the page to structure our API call, and we can then authorize our API calls with a free Cloudmersive API key (this allows up to 800 API calls per month with zero commitments).

It’s worth noting that this API isn’t limited to just WebP file inputs. Over 100 common image formats (and major Office file formats) are supported, so we can use this same solution to convert PNGs, JPGs, and other ubiquitous image formats to PDF as well.

Our first step is to install the PHP client. Let’s use the below command to install via Composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

Our next (and final) step is to call the function. Let’s copy the below examples and provide our API key in the $config snippet:

<?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->convertDocumentAutodetectToPdf($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentAutodetectToPdf: ', $e->getMessage(), PHP_EOL;
}
?>

The API will autodetect the input image format to ensure a high quality conversion to PDF.

That’s all there is to it — now we can easily convert WebP and dozens of other common image formats to PDF with ease.

--

--

Cloudmersive

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