Convert MHT File to PDF using Java REST API

This short tutorial explains the process to convert MHT file to PDF using Java REST API. You will learn to automatically transform MHT to PDF using Java REST API using the Java-based Cloud SDK. It will also share details to retrieve the result of the conversion from the Cloud storage.

Prerequisite

Steps to Convert MHT to PDF using Java RESTful Service

  1. Instantiate the PdfApi service with the Client key and secret to change MHT to PDF
  2. Read the source MHT file into a memory stream
  3. Upload the memory stream to the Cloud storage by assigning a unique name
  4. Call the GetMhtInStorageToPdf() method to transform the uploaded MHT file into a PDF file
  5. Parse the API response to retrieve the resultant PDF file
  6. Save the returned stream as a PDF file on the disk

These steps define the process of exporting a file from MHT format to PDF using Java REST Interface. Commence the process by uploading the source MHT file to the Cloud storage and calling the GetMhtInStorageToPdf() method to convert it to a PDF in the Cloud. The API response contains the stream object that is saved as a PDF file.

Code to Change MHT to PDF using 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 { // Class for MHT to PDF
public static void convertMhtToPdf() throws IOException {
// Initialize API with credentials
PdfApi pdfApi = new PdfApi("Client Secret", "Client ID");
String documentName = "MhtExample.mht";
// Upload the MHT file
try (FileInputStream fileInputStream = new FileInputStream(new File(documentName))) {
UploadFileResponse uploadResult = pdfApi.uploadFile(documentName, fileInputStream);
}
// Retrieve the PDF file
FileResponse response = pdfApi.getMhtInStorageToPdf(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 {
convertMhtToPdf();
} catch (IOException e) {
e.printStackTrace();
}
}
}

This code has demonstrated the MHT to PDF conversion using Java-based API. The UploadFile() method in the PdfApi class is used to save the source MHT file into the Cloud storage. The API response contains the PDF file that can be used for further processing.

This guide has taught us to convert MHT to PDF. If you want to convert TEX format to PDF, refer to the article on Convert TEX to PDF with Java REST API.