Convert EPUB to PDF in Java
EPUB is a format that is widely used for e-books, but it is not always compatible with all devices or software applications. Converting EPUB to PDF can be helpful in situations where you want to create a document that is easily readable across different devices and platforms. PDFs are optimized for printing, making them ideal for creating physical copies of your documents. You can configure security settings for PDF files to restrict access or editing.
In this article, you will find information about how to convert EPUB to PDF using Aspose.HTML for Java library and how to use PdfSaveOptions.
EPUB to PDF by a few lines of Java code
The static methods of the Converter class are primarily used as the easiest way to convert an EPUB file into various formats. You can convert EPUB to PDF in your Java application literally with a few lines of code!
1// Open an existing EPUB file for reading
2java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
3
4// Invoke the convertEPUB() method to convert EPUB to PDF
5Converter.convertEPUB(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");
Convert EPUB to PDF in Java
You should follow a few steps:
- Open an existing EPUB file. In the example, the code initializes a
FileInputStream
to read the contents of the EPUB file from the file system at the specified path as a stream of bytes. - Create an instance of PdfSaveOptions. Use the empty PdfSaveOptions() constructor to convert with the default save options.
- Use the
convertEPUB(stream, options, savePath)
method of the Converter class to save EPUB as a PDF file. You need to pass the EPUB file stream, PdfSaveOptions, and output file path to theconvertEPUB()
method as parameters.
The following Java code snippet shows how to convert EPUB to PDF using Aspose.HTML for Java:
1// Open an existing EPUB file for reading
2java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
3
4// Create an instance of the PdfSaveOptions class
5PdfSaveOptions options = new PdfSaveOptions();
6
7// Call the convertEPUB() method to convert EPUB to PDF
8Converter.convertEPUB(fileInputStream, options, "output-epub-to-pdf.pdf");
You can download the complete examples and data files from GitHub.
Save Options – PdfSaveOptions Class
Aspose.HTML provides the ability to programmatically convert EPUB to PDF with full control over various conversion options using PdfSaveOptions. This feature allows users to customize the rendering process by setting the page size, margins, file permissions, media type, and other parameters.
Metod | Description |
---|---|
setJpegQuality(value) | Specifies the quality of JPEG compression for images. The default value is 95. |
getCss() | Gets a CssOptions object which is used for configuration of CSS properties processing. |
setBackgroundColor(value) | Sets the color that will fill the background of every page. By default, this property is Transparent . |
setPageSetup(value) | This method sets a page setup object and uses it for configuration output page-set. |
setHorizontalResolution(value) | Sets horizontal resolution for internal images, in pixels per inch. By default this property is 300 dpi. |
setVerticalResolution(value) | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
setEncryption | This method gets or sets encryption details. If it is not set, then no encryption will be performed. |
For further information on how to customize the conversion process with PdfSaveOptions, you can refer to the Fine-Tuning Converters article.
Convert EPUB to PDF by specifying PdfSaveOptions
Setting different options of
PdfSaveOptions class gives you control over the page size, resolution, background color, and compression settings for the output PDF. The following example shows how to use PdfSaveOptions
and create a PDF file with custom custom page-size and background color:
- Open an existing EPUB file.
- Create a new
PdfSaveOptions object and specify the required save options. Use the
setPageSetup()
andsetBackgroundColor()
methods to set the page size and background color. - Call the
convertEPUB(stream, options, savePath)
method to save EPUB as a PDF file. You need to pass the EPUB file stream, PdfSaveOptions, and output file path to the convertEPUB() method as parameters.
1// Open an existing EPUB file for reading
2java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
3
4// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
5PdfSaveOptions options = new PdfSaveOptions();
6options.setBackgroundColor(Color.getAliceBlue());
7options.getPageSetup().setAnyPage(new Page());
8options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(1000), Length.fromPixels(1000)));
9
10// Call the ConvertEPUB() method to convert EPUB to PDF
11Converter.convertEPUB(fileInputStream, options, "input-options.pdf");
Conclusion
Converting EPUB to PDF using the Aspose.HTML library for Java is a simple and effective way to create universally compatible, print-ready documents. Using the PdfSaveOptions
class, developers can customize the output with options such as page size, background color, resolution, and encryption, ensuring that the final PDF file meets specific requirements. The considered Java examples demonstrate how to implement the conversion features in your Java applications.
Check the quality of EPUB to PDF conversion with our online EPUB to PDF Converter. Upload, convert your files and get the result in a few seconds. Try our forceful EPUB to PDF Converter for free now!