This article explains easy and simple steps to convert JPG to PDF using Java Cloud SDK. We know that the JPG format is one of the widely used raster image formats and is the default format for capturing images from digital cameras, mobile phones, etc. Owing to their compressed size, they are commonly shared over the internet as well as displayed on websites. However, if you have a plethora of images that need to be shared online, the conversion to PDF is the right option. We can also create a beautiful photo album, reduce file size easily, get better resolution, etc.
JGP to PDF Conversion API
Aspose.PDF Cloud SDK for Java provides the capabilities to create, edit and transform a variety of file formats to PDF format. It also supports the feature to convert JPG to PDF / Image to PDF / Photo to PDF in Java applications. Now in order to use the SDK, please add the following details in pom.xml of the maven build type project.
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Cloud Repository</name>
<url>https://repository.aspose.cloud/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf-cloud</artifactId>
<version>21.11.0</version>
</dependency>
</dependencies>
After the installation, we need to create a free account by visiting Aspose.Cloud dashboard. Simply Sign up using your existing GitHub or Google account, or click the Create a new Account button.
JPG to PDF in Java
In this section, we are going to discuss the details for converting JPG to PDF using java code snippets.
- First of all, create an object of PdfApi while passing ClientID and ClientSecret details are arguments
- Secondly, create a blank PDF file using the putCreateDocument(…) method of PdfApi class to create an empty PDF document
- Now call postInsertImage(..) method which takes input PDF filename, PageNumber, XY coordinates, and image file name as arguments
Image to PDF using cURL Commands
We can also perform the conversion of JPG to PDF using cURL commands. As a pre-requisite, we need to execute the following command to generate the JWT access token.
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Once the JWT is generated, please execute the following command to generate a blank PDF document and save it in cloud storage.
curl -v -X PUT "https://api.aspose.cloud/v3.0/pdf/input.pdf" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>"
Now we need to execute the following command to place the JPG image inside a PDF document.
curl -v -X POST "https://api.aspose.cloud/v3.0/pdf/input.pdf/pages/1/images?llx=10.0&lly=850.0&urx=580.0&ury=650.0&imageFilePath=source.JPG" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: multipart/form-data" \
-d {"image":{}}
Conclusion
In this blog, we have discussed the steps to convert JPG to PDF using Java code snippets. We have also explored the option of converting Image to PDF / photo to PDF using cURL commands. You may also explore other examples available on GitHub repository Please try using our APIs and in case you encounter any issues while using the API, please feel free to contact the Free product support forum.
Related Articles
We also recommend visiting the following blogs for further details on: