How to Convert JPG to PDF with Java REST API

This step by step tutorial shows you how to convert JPG to PDF with Java REST API. JPG (also known as JPEG) is a popular image file format used for storing digital photographs, graphics, and images. It is a popular format for web graphics and is often used for storing digital photos due to its small size and high compression rate. JPG images are typically created using a lossy compression method, which means some image data is lost when the file is saved.

On the other hand, PDF (Portable Document Format) is a file format developed by Adobe Systems in 1993 to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems. PDF files are commonly used for product manuals, application forms, and tax forms. If you would like JPG to PDF Conversion in Java Low Code API then this can be performed with the help of code given below.

Prerequisite

Steps to Convert JPG to PDF in Java REST API

  1. Set Client ID and Client Secret for the API
  2. Create an object of ImagingAPI class with client credentials
  3. Specify input and output files
  4. Read input JPG file and upload to cloud storage
  5. Create an object of ConvertImageRequest with input and output file formats
  6. Call convertImage method to Convert JPG to PDF using REST API
  7. Save the output PDF file on local disk

Code for JPG to PDF Conversion in Java Low Code API

String ClientID = Client.getID(); //replace Client.getID() with your own client ID here
String ClientSecret = Client.getSecret(); //replace3 Client.getSecret() with your own client secret here
String APIBaseUrl="https://api.aspose.cloud";
String Local_Path = "C:/Temp/";
ImagingApi imagingApiSdk = new ImagingApi(ClientSecret, ClientID, APIBaseUrl);
// Input & output file names
String inputFileName = "JPGtoPDF.jpg";
String outputFileName = "JPGtoPDF.pdf";
try {
// Upload local image to Cloud Storage
File inputFile = new File(Local_Path + inputFileName);
FileInputStream inputFileStream = new FileInputStream(inputFile);
byte[] inputImageData = IOUtils.toByteArray(inputFileStream);
UploadFileRequest uploadFileRequest = new UploadFileRequest(inputFileName, inputImageData, null);
FilesUploadResult filesUploadResult = imagingApiSdk.uploadFile(uploadFileRequest);
String outputFormat = "pdf";
String remoteFolder = null; // Input file is saved at the root of the storage
String remoteStorage = null; // Cloud Storage name
ConvertImageRequest convertImageRequest = new ConvertImageRequest(inputFileName, outputFormat, remoteFolder, remoteStorage);
byte[] convertedImageData = imagingApiSdk.convertImage(convertImageRequest);
// Save exported image to local storage
FileOutputStream fileOutputStream = new FileOutputStream(Local_Path + outputFileName);
fileOutputStream.write(convertedImageData);
fileOutputStream.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}

The code sample shown above enables you to convert JPG to PDF with Java REST API. You only have to supply JPG file with the help of the Aspose.Imaging REST API SDK and download output PDF file to save it locally.

This JPG to PDF Conversion feature can be consumed with any no code or low code apps on any device or computer.

Another relevant feature can be found at the following URL: How to Convert BMP to JPG with Java REST API

 English