Develop Online PDF Converter using Java SDK

Learn how to convert Word to PDF using Java SDK. Perform DOC to PDF conversion on any platform using REST API.

PDF Converter
PDF Converter | Convert Word to PDF online

This article helps you in developing a PDF converter using Java Cloud SDK. We understand that MS Word file formats including DOC, DOT, DOCX, DOCM, DOTX, etc are quite popular for information sharing within organizations as well as for academic purposes. One of the factors for their popularity is their support for rich text that allows formatting options such as setting fonts type, styles (bold, italic, underline, etc.), page margins, headings, bullets and numbers, and several other formatting features. Furthermore, they provide amazing support for images, tables, graphs, charts, page formatting, and print settings. But in order to view these documents, certain applications WordPad, WordPerfect, OpenOffice, MS Word, LibreOffice, etc are required. Also, when viewing the document on a platform that does not have a specific font installed (the one used in the document), the overall formatting of the document is compromised. Therefore, the conversion of these documents to PDF format is a viable solution because it maintains document fidelity on all platforms.

In this article, we are going to discuss the following topics in detail

Word Document processing API

Aspose.Words Cloud is an impeccable Word document processing Cloud API. It enables you to create, update as well as convert MS Word documents to other Supported File Formats including PDF. Also please note that we have created Cloud SDKs which are a wrapper around REST APIs so that the programmers can use them directly within their favorite programming language. We are going to emphasize Aspose.Words Cloud SDK for Java which provides word document processing capabilities within Java code.

Installation & Configuration

The first step is to install the SDK on the system in which document conversion will be performed. The SDK is available for download at Maven and GitHub. Given below are the details on how to download and use aspose-words-cloud-21.6.0.jar in the Maven build project.

Add the following dependencies in your pom.xml file.

<repositories>
 <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>
<dependencies>
 <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words-cloud</artifactId>
        <version>21.6.0</version>
    </dependency>
 </dependencies>

The aspose-words-cloud-21.6.0.jar appears under the Maven Dependencies folder.

Aspose.Words Cloud SDK for Java
Image 1: Aspose.Words Cloud SDK for Java

Aspose.Cloud account subscription

In order to use the SDK, the next step is to obtain your personalized ClientID and ClientSecret details. Therefore, the first step is to create an account by visiting Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client Credentials
Image 2: Client Credentials on Cloud Dashboard.

Convert Word to PDF in Java

Document conversion is a complex process. Many factors contribute to the complexity and therefore, should be taken into account during the transformation process. However, when utilizing Aspose.Words Cloud, it gets quite handy because of its impeccable ability to convert Word documents from one format to another with precise professional quality.

Please follow the instructions given below to perform the MS Word files (DOCX/DOC) to PDF conversion using Java.

  • First of all, we need to define Client ID and Client Secret details in string objects
  • Secondly, create an instance of ApiClient while providing Client ID and Client Secret as arguments
  • Thirdly, create a WordsApi object by passing the ApiClient object as an argument
  • Now create an object of ConvertDocumentRequest class which takes input DOC file as a byte array, resultant output format, and destination to store the file
  • Finally, call the convertDocument(..) method of WordsApi to perform the document conversion
doc to pdf conversion preview
Image 3: DOC to PDF conversion preview

DOC to PDF using cURL Command

The client URL is a free command-line tool popular for data transfer to or from a network server, using one of the supported (HTTP, HTTPS, FTP, etc). It uses URL syntax to transfer data to and from servers. cURL is widely popular because of its ability to be flexible and complete complex tasks. Since our Cloud APIs are developed as per REST architecture, so you can also use the cURL command-line tool to access Aspose.Words web services easily.

But before we proceed, the first step is to generate a JWT access token because our REST APIs are only accessible to authorized persons. So the JSON Web Token (JWT) is based on your ClientID and ClientSecret details retrieved from Aspose.Cloud dashboard. Please execute the following command in the terminal to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=718e4235-8866-4ebe-bff4-f5a14a4b6466&client_secret=388e864b819d8b067a8b1cb625a2ea8e" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once we have the JWT Token, please execute the following command to perform the word document (already available on cloud storage) to pdf conversion.

curl -X GET "https://api.aspose.cloud/v4.0/words/TableDocument.doc?format=pdf&outPath=converted.pdf" \
-H  "accept: application/octet-stream" \
-H  "Authorization: Bearer <JWT Token>"

Please visit the following links to download input TableDocument.doc and converted.pdf.

Conclusion

In this article, we have learned the steps to develop a PDF converter using REST API. In a step-by-step process, we have explored the options to convert word to PDF using Java code snippets. Similarly, we have seen how we can convert DOC to PDF using cURL commands. Please note that before making a purchase decision, you can use the free account to perform 150 file manipulation requests and once you are satisfied with the API, you may consider purchasing the subscription. Our charging mechanism is so flexible that you are only charged for the services you utilize, unlike other conventional solutions that charge the whole amount upon the license purchase. Please note that the charging amount can be as minimum as $0.005/API Call.

Furthermore, using the same API you can save PDF to Word, PDF to JPG, or perform JPG to PDF conversion. Please note that all our Cloud SDKs (the wrapper around Cloud API) are open-source and their complete source code is available on GitHub. In case you encounter any issues while using the API, please feel free to contact us via the Free customer support forum.

Related links

We also recommend visiting the following links to explore