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 also 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 and MemoryStreamProvider.
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
2 final FileStream stream = File.openRead(StringExtensions.concat(getDataDir(), "input.epub"));
3 try { }
4 finally { if (stream != null) ((IDisposable)stream).dispose(); }
5
6 // Invoke the convertEPUB() method
7 com.aspose.html.converters.Converter.convertEPUB(stream, new PdfSaveOptions(), Path.combine(getOutputDir(), "convert-by-few-lines.pdf"));
Convert EPUB to PDF in Java
You should follow a few steps:
- Open an existing EPUB file. In the example, we use the
openRead()
method of System.IO.FileStream class to open and read an EPUB file from the file system at the specified path. - 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 the ConvertEPUB() method as parameters.
The following Java code snippet shows how to convert EPUB to PDF using Aspose.HTML:
1 // Open an existing EPUB file for reading
2 final FileStream stream = File.openRead(StringExtensions.concat(getDataDir(), "input.epub"));
3 try { }
4 finally { if (stream != null) ((IDisposable)stream).dispose(); }
5
6 // Prepare a path to save the converted file
7 String savePath = Path.combine(getOutputDir(), "input-output.pdf");
8
9 // Create an instance of PdfSaveOptions
10 PdfSaveOptions options = new PdfSaveOptions();
11
12 // Call the convertEPUB() method
13 com.aspose.html.converters.Converter.convertEPUB(stream, options, savePath);
You can download the complete examples and data files from GitHub.
Save Options
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, CSS 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.
Please note that you cannot set values against the Application and Producer fields, because Aspose Ltd. and Aspose.HTML for Java x.x.x will be displayed against these fields.
Convert EPUB to PDF using PdfSaveOptions
Setting different options of PdfSaveOptions class gives you control over the page size, resolution, background color, and compression settings for the output PDF. These are the steps to convert an EPUB file to PDF format with PdfSaveOptions specifying.
- 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.
The following example shows how to use PdfSaveOptions and create a PDF file with custom custom page-size and background color:
1 // Open an existing EPUB file for reading
2 final FileStream stream = File.openRead(StringExtensions.concat(getDataDir(), "input.epub"));
3 try { }
4 finally { if (stream != null) ((IDisposable)stream).dispose(); }
5
6 // Prepare a path to save the converted file
7 String savePath = Path.combine(getOutputDir(), "input-options.pdf");
8
9 // Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
10 PdfSaveOptions options = new PdfSaveOptions();
11 PageSetup pageSetup = new PageSetup();
12 Page anyPage = new Page();
13 anyPage.setSize(new com.aspose.html.drawing.Size(Length.fromPixels(1000),Length.fromPixels(1000)));
14 pageSetup.setAnyPage(anyPage);
15 options.setPageSetup(pageSetup);
16 options.setBackgroundColor(Color.getAliceBlue());
17
18 // Call the convertEPUB() method
19 com.aspose.html.converters.Converter.convertEPUB(stream, options, savePath);
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!