Convert EPUB to PDF in Java

To convert EPUB to PDF in Java, open the EPUB file as an input stream, create PdfSaveOptions, and call Converter.convertEPUB(). Configure PdfSaveOptions when the PDF requires a specific page size, background, resolution, or encryption.

EPUB content is reflowable, while PDF uses a fixed page layout. Converting EPUB to PDF creates a document suited to consistent viewing, sharing, and printing. Aspose.HTML for Java performs the conversion with convertEPUB() and configures PDF output through PdfSaveOptions.

Convert EPUB to PDF in Two Lines of Java Code

The following example opens an EPUB file and passes its stream, default PdfSaveOptions, and an output path to Converter.convertEPUB():

1// Convert EPUB to PDF using Java
2
3// Open an existing EPUB file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
5
6// Invoke the convertEPUB() method to convert EPUB to PDF
7Converter.convertEPUB(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");

The example reads input.epub and saves the result as convert-by-two-lines.pdf.

Convert an EPUB File to PDF

Follow these steps for a basic EPUB-to-PDF conversion:

  1. Open the source EPUB file as a FileInputStream.
  2. Create a PdfSaveOptions object. The parameterless constructor uses the default PDF save settings.
  3. Call Converter.convertEPUB(stream, options, outputPath) to convert EPUB to PDF.

In this example, the source is input.epub, and the result is saved as output-epub-to-pdf.pdf.

 1// Convert EPUB to PDF in Java
 2
 3// Open an existing EPUB file for reading
 4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
 5
 6// Create an instance of the PdfSaveOptions class
 7PdfSaveOptions options = new PdfSaveOptions();
 8
 9// Call the convertEPUB() method to convert EPUB to PDF
10Converter.convertEPUB(fileInputStream, options, "output-epub-to-pdf.pdf");

Download complete examples and data files from GitHub.

Save Options – PdfSaveOptions Class

The PdfSaveOptions class controls PDF page layout, background, internal image resolution, JPEG compression quality, CSS processing, and encryption.

MethodUse it to
setJpegQuality(value)Set JPEG compression quality when JPEG compression is used. The default is 95.
setBackgroundColor(value)Set the color that fills each output page. The default is transparent.
getPageSetup()Access the page setup used to configure output page size and margins.
setHorizontalResolution(value)Set horizontal resolution for internal images. The default is 300 dpi.
setVerticalResolution(value)Set vertical resolution for internal images. The default is 300 dpi.
getCss()Access CSS processing options.
setEncryption(value)Set PDF encryption details. No encryption is applied when this option is not set.

For additional settings, see Fine-Tuning Converters.

Customize EPUB to PDF with PdfSaveOptions

Use PdfSaveOptions when the output requires custom rendering settings:

  1. Open the source EPUB file as an input stream.
  2. Create PdfSaveOptions and configure the required PDF settings.
  3. Call Converter.convertEPUB(stream, options, outputPath) to convert EPUB to PDF.

The following example converts input.epub to input-options.pdf, sets the page background to Alice Blue, and uses a 1000 × 1000 pixel page:

 1// Convert EPUB to PDF in Java with custom settings
 2
 3// Open an existing EPUB file for reading
 4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
 5
 6// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
 7PdfSaveOptions options = new PdfSaveOptions();
 8options.setBackgroundColor(Color.getAliceBlue());
 9options.getPageSetup().setAnyPage(new Page());
10options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(1000), Length.fromPixels(1000)));
11
12// Call the convertEPUB() method to convert EPUB to PDF
13Converter.convertEPUB(fileInputStream, options, "input-options.pdf");

Related EPUB Conversion Guides

Other Platforms

FAQ

Can I convert EPUB to PDF for free?

Yes. Use the free online EPUB to PDF Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.

Why can PDF page breaks differ from an EPUB reader?

EPUB readers reflow content according to the viewport and reader settings, while PDF uses fixed pages. Page size, margins, CSS, and font availability therefore affect pagination. Configure PdfSaveOptions and test the output with the fonts used by the source publication.

Try Online EPUB to PDF Conversion

Use the free online EPUB to PDF Converter for quick manual conversion without writing Java code.

Free Online EPUB to PDF Converter