How to Convert JPG to PDF in PHP

Cloudmersive
2 min readMay 6, 2024

We can easily convert JPG images to PDF format in PHP by incorporating a free low-code API call into our workflow.

To structure our API call, we’ll just need to copy and paste from complementary PHP code examples included further down the page. We’ll call a service that quickly converts JPGs (along with 100+ additional common image formats) to PDF documents, first autodetecting the input format to ensure a high-quality conversion.

First things first — let’s start by installing the PHP client. We can install via Composer using the below command:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next, let’s acquire a free Cloudmersive API key to authorize our requests. This will allow us to make up to 800 API calls per month with zero commitments (once we reach our total, it’ll reset the following month).

Finally, let’s copy the below code to call the function. We can 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;
}
?>

Now we can easily make JPG to PDF conversions with a scalable, low-code service in PHP. No more steps required!

--

--

Cloudmersive

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