TXT to PDF

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.

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://repository.aspose.cloud/repo/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
		<groupId>com.aspose</groupId>
		<artifactId>aspose-words-cloud</artifactId>
		<version>22.5.0</version>
	</dependency>
</dependencies>

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 Text to PDF in Java

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

  • Firstly, create an object of WordsApi class using 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 the uploadFile(…) method to upload the TXT file to cloud storage.
  • The next step is to create an object of GetDocumentWithFormatRequest class where we specify the output format as PDF.
  • Finally, call the method getDocumentWithFormat(…) to perform the conversion operation and save the resultant PDF to Cloud storage.
TXT to PDF

Image 1:- TXT to PDF Conversion preview

TXT to PDF using cURL Commands

Converting TXT to PDF using Aspose.Words Cloud with cURL commands offers a flexible and scriptable solution for developers. This approach not only simplifies integration into diverse development workflows but also ensures a consistent and reliable process for document generation. Furthermore, you also get the capability to tailor the cURL commands to specific project requirements, making this approach adaptable and scalable for various applications.

The pre-requisite for this approach is to generate a JWT token based on client credentials. So, please execute the following command in the terminal application to generate the JWT 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"

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>"

Conclusion

In conclusion, our exploration into converting TXT to PDF using both Java Cloud SDK and Aspose.Words Cloud with cURL commands illuminates the dynamic landscape of document transformation. Whether through the robust capabilities of Java Cloud SDK or the scriptable efficiency of cURL commands with Aspose.Words Cloud, you gain powerful tools to seamlessly transition plain text files into professional and shareable PDF documents. Nonetheless, with these versatile methods, document conversion becomes a harmonious blend of efficiency and precision, catering to the diverse needs of data processing workflows.

We highly recommend visiting the following links to learn about: