Convert EPUB Format to PDF with Java REST API

In this short tutorial, you will learn to convert EPUB format to PDF with Java REST API. It will guide you to upload the source EPUB file to a Cloud storage and transform the loaded EPUB to PDF with Java Low Code API. You will also get details to save the API response as an output file on the disk.

Prerequisite

Steps to Change EPUB to PDF with Java RESTful Service

  1. Instantiate the PdfApi object by setting the client key and ID for working with EPUB
  2. Load the source EPUB file into the memory stream
  3. Upload the source Ebook to the Cloud storage with a defined name
  4. Invoke the GetEpubInStorageToPdf() method by passing the EPUB file name in the Cloud storage
  5. Handle the returned stream and save it as output PDF

These steps summarize the process to convert EPUB format to PDF with Java Low Code API. Read the input EPUB file into a MemoryStream object and use it for uploading the file to Cloud storage. Finally, call the GetEpubInStorageToPdf() to change the uploaded Ebook to a PDF.

Code to Convert a EPUB File 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 convertEpubToPdf() throws IOException {
// Initialize API with credentials
PdfApi pdfApi = new PdfApi("secret", "ID");
String documentName = "Sample.epub";
// Upload the EPUB file
FileInputStream fileInputStream = new FileInputStream(new File(documentName));
UploadFileResponse uploadResult = pdfApi.uploadFile(documentName, fileInputStream);
// Convert EPUB to PDF
FileResponse response = pdfApi.getEpubInStorageToPdf(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 {
convertEpubToPdf();
} catch (IOException e) {
e.printStackTrace();
}
}
}

This code has demonstrated the development of a EPUB to PDF converter with Java REST Interface. The GetEpubInStorageToPdf() method returns a stream that contains the PDF file generated from the uploaded EPUB file. If you have uploaded multiple EPUB files to the Cloud storage, mention the target EPUB file in the GetEpubInStorageToPdf() method.

This guide has provided information to export EPUB file to PDF with Java REST API. To convert an XPS file to a PDF file, refer to the article How to Convert XPS Document to PDF with Java REST API.