Convert Text file to PDF using Java

The TXT extension represents a text document that contains plain text in the form of lines. The data values in a plain text file are stored as a series of characters. Each character is stored in computer memory as one or two bytes, and owing to this fact, it’s one of the popular formats on all platforms. However, for long-term archival, we may have a requirement to convert the TXT file to PDF format. In this article, we are going to discuss the details on how to convert TXT to PDF online.

TXT to PDF Conversion API

Aspose.Words Cloud SDK for Java enables java developers to implement Word document creation, manipulation, and conversion routines within Java applications. So as per the scope of this article, we can accomplish TXT to PDF conversion with fewer code lines. Now please add the following details in pom.xml of your maven build type project to add reference of Word manipulate SDK in your project.

        <name>Aspose Java API</name>


The next step in API usage is the creation of a free account by visiting Aspose.Cloud dashboard. You may Sign up by simply using an existing GitHub or Google account, or click the Create a new Account button.

Convert TXT to PDF in Java

Please follow the instructions below to convert the content of the Text file to PDF using a Java code snippet.

  • First, create an object of WordsApi using a Client ID and Client Secret as parameters
  • Secondly, read the TXT file content from the local drive using the File object
  • Thirdly, read the content of the TXT file using the UploadFileRequest instance
  • Now call uploadFile(…) method to upload the TXT file to cloud storage
  • Next step is to create an object of GetDocumentWithFormatRequest where we specify the output format as PDF
  • Finally, call the method getDocumentWithFormat(…) to perform the conversion and save the output in Cloud storage

Image 1:- TXT to PDF Conversion preview

TXT to PDF using cURL Commands

As Aspose.Words Cloud is built on top of REST API architecture, so we can also access it using cURL commands. Now the pre-requisite, we need to generate a JWT token based on client credentials. Please execute the following command in the terminal application to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-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"

Now that we have the JWT token, please execute the following command to upload the source Text file to cloud storage.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/storage/file/input.txt" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: multipart/form-data" \
-d {"fileContent":{}}

In order to load the TXT file from cloud storage and its conversion to PDF format, please execute the following cURL command.

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


This article has shared the details on how we can convert TXT to PDF using Java code snippets as well as using cURL Commands. You may also download the complete source code of SDK from GitHub and make changes as per your requirements. In order to learn about other amazing features being offered by the product, please explore the documentation.

In case you face any issues while using the API, please feel free to contact us through the technical support forum.

We highly recommend visiting the following links to learn about: