Converting JPG images to PDF documents programmatically is a frequent requirement for PHP developers building reporting or archival features. Aspose.Words Cloud SDK for PHP enables you to convert JPG to PDF in PHP with just a few lines of code. In this guide you will see a step‑by‑step walkthrough, a complete working example, and cURL commands for the REST API. You will also learn how to fine‑tune DPI and image quality for optimal results.
Setting Up Aspose.Words Cloud SDK for PHP
Before you start, make sure you have:
- PHP 7.4 or higher
- Composer installed
- An Aspose Cloud account with Client Id and Client Secret
- Network access to
https://api.aspose.cloud
Install the SDK via Composer:
composer require aspose-cloud/aspose-words-cloud
Download the latest package from the official release page: Aspose.Words Cloud SDK for PHP Download.
Now configure the SDK with your credentials:
require __DIR__ . '/vendor/autoload.php';
use Aspose\Words\Configuration;
$config = new Configuration();
$config->setClientId('YOUR_CLIENT_ID');
$config->setClientSecret('YOUR_CLIENT_SECRET');
$config->setHost('https://api.aspose.cloud');
With the configuration ready, you can create a WordsApi instance and start converting.
JPG to PDF Conversion in PHP: Step-by-Step Walkthrough
Step 1: Load the Source Document
Open the JPG file as a stream so large images are handled efficiently.
$inputJpgPath = __DIR__ . '/sample.jpg'; // source JPG
$jpgStream = fopen($inputJpgPath, 'rb');
Step 2: Configure Conversion Options
Set the desired DPI and image quality. These settings directly affect the output PDF size and clarity, which is important when you convert JPG to PDF in PHP.
use Aspose\Words\Model\ConvertOptions;
$convertOptions = new ConvertOptions();
$convertOptions->setDpiX(300); // horizontal DPI
$convertOptions->setDpiY(300); // vertical DPI
$convertOptions->setImageQuality(80); // 0‑100
Step 3: Create the Convert Request
Prepare the request object with the input stream, target format, and options.
use Aspose\Words\Model\Requests\ConvertDocumentRequest;
$request = new ConvertDocumentRequest(
$jpgStream, // Input file stream
'pdf', // Target format
null, // outPath – null to receive binary response
null, // storage – default
null, // folder – default
$convertOptions // conversion options
);
Step 4: Execute Conversion and Save PDF
Call the API, receive binary PDF data, and write it to a file.
use Aspose\Words\WordsApi;
$wordsApi = new WordsApi($config);
try {
$pdfBinary = $wordsApi->convertDocument($request);
$outputPdfPath = __DIR__ . '/result.pdf';
$pdfHandle = fopen($outputPdfPath, 'wb');
fwrite($pdfHandle, $pdfBinary);
fclose($pdfHandle);
echo "Conversion successful: {$outputPdfPath}\n";
} catch (Exception $e) {
echo 'Error during conversion: ', $e->getMessage(), "\n";
}
Full Working Example for Converting JPG to PDF in PHP
The example below demonstrates the complete implementation from start to finish.
<?php
require __DIR__ . '/vendor/autoload.php';
use Aspose\Words\Configuration;
use Aspose\Words\WordsApi;
use Aspose\Words\Model\ConvertOptions;
use Aspose\Words\Model\Requests\ConvertDocumentRequest;
$clientId = 'YOUR_CLIENT_ID';
$clientSecret = 'YOUR_CLIENT_SECRET';
$config = new Configuration();
$config->setClientId($clientId);
$config->setClientSecret($clientSecret);
$config->setHost('https://api.aspose.cloud');
$wordsApi = new WordsApi($config);
$inputJpgPath = __DIR__ . '/sample.jpg'; // source JPG
$outputPdfPath = __DIR__ . '/result.pdf'; // destination PDF
// Open JPG as a stream to avoid loading whole file into memory for large images
$jpgStream = fopen($inputJpgPath, 'rb');
$convertOptions = new ConvertOptions();
$convertOptions->setDpiX(300); // horizontal DPI
$convertOptions->setDpiY(300); // vertical DPI
$convertOptions->setImageQuality(80); // optional, 0‑100
$request = new ConvertDocumentRequest(
$jpgStream, // Input file stream
'pdf', // Target format
null, // outPath – null to receive binary response
null, // storage – default
null, // folder – default
$convertOptions // conversion options
);
try {
// API returns binary PDF data
$pdfBinary = $wordsApi->convertDocument($request);
// Write binary data to file efficiently
$pdfHandle = fopen($outputPdfPath, 'wb');
fwrite($pdfHandle, $pdfBinary);
fclose($pdfHandle);
echo "Conversion successful: {$outputPdfPath}\n";
} catch (Exception $e) {
echo 'Error during conversion: ', $e->getMessage(), "\n";
}
// Cleanup
if (is_resource($jpgStream)) {
fclose($jpgStream);
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Using cURL to Convert JPG Images to PDF Documents
If you prefer a direct REST call, the same conversion can be performed with cURL.
-
Obtain an access token
curl -X POST "https://api.aspose.cloud/connect/token" \ -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" -
Upload the source JPG to storage
curl -X PUT "https://api.aspose.cloud/v4.0/words/storage/file/sample.jpg" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ --data-binary "@sample.jpg" -
Execute the conversion
curl -X GET "https://api.aspose.cloud/v4.0/words/sample.jpg?format=pdf" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -o result.pdf -
Download the resulting PDF (if not using
-oabove)curl -X GET "https://api.aspose.cloud/v4.0/words/result.pdf" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -o result.pdf
These commands illustrate the same workflow without writing any PHP code. For more details, see the official API documentation.
Fine-Tuning Conversion Settings for Optimal PDF Output
The SDK offers several options to control the appearance and size of the generated PDF.
- DPI (Dots Per Inch) - Higher DPI (e.g., 300) yields sharper output suitable for printing. Set with
setDpiXandsetDpiY. - ImageQuality - Controls JPEG compression level (0‑100). Lower values reduce file size at the cost of visual fidelity.
- Additional options - The
ConvertOptionsclass also supportssetPageSize,setPageOrientation, andsetColorMode. Refer to the API reference for the full list.
Example of setting DPI and quality (already shown in the walkthrough):
$convertOptions = new ConvertOptions();
$convertOptions->setDpiX(300);
$convertOptions->setDpiY(300);
$convertOptions->setImageQuality(80);
Key Takeaways
Using the Aspose.Words Cloud SDK for PHP, you can easily convert JPG to PDF in PHP without dealing with low‑level image processing. The SDK handles streaming, DPI, and image quality, allowing you to focus on business logic. Remember to secure your Client Id and Client Secret, and consider the temporary license for evaluation before purchasing a full license. Pricing details are available on the product page, and a temporary license can be obtained from the temporary license page. With the code and cURL examples provided, you’re ready to integrate JPG to PDF conversion into any web or desktop PHP application.
Questions You Might Have
-
What is the simplest way to convert JPG to PDF in PHP?
The one‑liner shown in the complete example usesConvertDocumentRequestwith the target format set to"pdf". This single call performs the entire conversion. -
Can I convert multiple JPG files in a single request?
The API processes one source file per request. To handle batches, loop over your file list and invoke the conversion for each image. -
How do I control the output PDF size?
AdjustDpiX,DpiY, andImageQualityinConvertOptions. Lower DPI and quality reduce file size, while higher values improve visual fidelity. -
Is there a limit on the size of the JPG image I can convert?
The SDK streams the input, so memory usage stays low. However, extremely large files may be subject to service limits; consult the official documentation for the exact thresholds.