Convert TEX to PDF with Java REST API

This quick guide describes how to convert TEX to PDF with Java REST API. You will learn to transform the file format from TEX to PDF with Java Low Code API using a Java-based Cloud SDK. The sample code will demonstrate the complete process of loading the TEX file from the disk to save the output PDF file on the local system.

Prerequisite

Steps to Convert LaTex File to PDF with Java-based API

  1. Create an object of the PdfApi class by setting the client secret and ID to change TEX to PDF
  2. Read the TEX file from the disk and store it in a MemoryStream
  3. Upload this TEX file to a Cloud storage for further processing using the UploadFile() method
  4. Call the GetTeXInStorageToPdf() method to transform the uploaded TEX file into PDF
  5. Parse the API response and save the resultant stream as a PDF file on the disk

These steps encompass the process of changing a TEX file to PDF with Java RESTful Service. Load the source TEX file from the disk to a MemoryStream that is later used to upload it to a Cloud storage for processing by the online SDK. Finally, call the GetTeXInStorageToPdf() method by providing the uploaded TEX file in the storage for creating a PDF file from it.

Code to Convert LaTex to PDF with Java Low Code API

import com.aspose.pdf.cloud.sdk.Api.PdfApi;
import com.aspose.pdf.cloud.sdk.Model.UploadFileResponse;
import com.aspose.pdf.cloud.sdk.Model.FileResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class PdfTasks {
public static void convertTexToPdf() throws IOException {
// Initialize API with credentials
PdfApi pdfApi = new PdfApi("secret", "ID");
String documentName = "Sample.tex";
// Upload the TeX file
try (FileInputStream fileInputStream = new FileInputStream(new File(documentName))) {
UploadFileResponse uploadResult = pdfApi.uploadFile(documentName, fileInputStream);
}
// Convert TeX to PDF
FileResponse response = pdfApi.getTeXInStorageToPdf(documentName);
// Save the output PDF
try (FileOutputStream fileOutputStream = new FileOutputStream(new File("output.pdf"))) {
response.getFileStream().transferTo(fileOutputStream);
}
}
public static void main(String[] args) {
try {
convertTexToPdf();
} catch (IOException e) {
e.printStackTrace();
}
}
}

This code has demonstrated how to convert TEX file to PDF with Java REST API. You may fill the MemoryStream with TEX file data from a disk, network stream or database. The returned stream containing the PDF contents can be saved on a disk, transmitted over the network, or saved in a database as per the requirements.

This article has guided us to change the TEX format to PDF. To transform an EPUB file to PDF, refer to the article on Convert EPUB format to PDF with Java REST API.