Convert EPUB to PDF in Python

EPUB is an open XML-based format for digital books and publications designed for optimal viewing on various devices. It is created by International Digital Publishing Forum ( IDPF), and now it is supported by many e-readers and software applications. EPUB to PDF conversion is often required to take advantage of PDF format. With Aspose.HTML for Python via .NET, you can convert EPUB to PDF programmatically with full control over a wide range of conversion parameters.

In this article, you find information on how to convert EPUB to PDF using convert_epub() methods of the Converter class and how to apply PdfSaveOptions.

To continue following this tutorial, install and configure Aspose.HTML for Python via .NET in your Python project.

Online EPUB Converter

You can check the Aspose.HTML for Python via .NET API functionality and convert EPUB in real-time. Please load an EPUB file from a local file system or URL, select the output format and run the example. In the example, the save options are set by default. You will immediately receive the result as a separate file.

                
            

If you want to convert EPUB to PDF using PdfSaveOptions programmatically, please see the following Python code example.

Convert EPUB to PDF

Using convert_epub() methods is the most common way to convert EPUB files into various formats. To convert EPUB to PDF, you should follow a few steps:

  1. Open an existing EPUB file. In the example, we use the open() method to open and read an EPUB file from the file system at the specified path.
  2. Create an instance of PdfSaveOptions class and specify the required save options.
  3. Use one of the convert_epub() methods of the Converter class to save EPUB as a PDF file. In the following example, you need to pass the EPUB file stream, PdfSaveOptions, and output file path to the convert_epub().

The following example shows how to use PdfSaveOptions and create a PDF file with custom save options:

 1import os
 2from aspose.html import *
 3from aspose.html.converters import *
 4from aspose.html.saving import *
 5from aspose.html.drawing import *
 6
 7# Setup directories and define paths
 8output_dir = "output/"
 9input_dir = "data/"
10if not os.path.exists(output_dir):
11    os.makedirs(output_dir)
12document_path = os.path.join(input_dir, "input.epub")
13save_path = os.path.join(output_dir, "output.pdf")
14
15# Open an existing EPUB file for reading
16with open(document_path, "rb") as stream:
17
18    # Create an instance of PdfSaveOptions
19    options = PdfSaveOptions()
20    options.page_setup.any_page = Page(Size(800, 600), Margin(10, 10, 10, 10))
21    options.css.media_type.PRINT
22
23    # Convert EPUB to PDF
24    Converter.convert_epub(stream, options, save_path)

In the example, we open and read source files from the file system at the specified path. The PdfSaveOptions() constructor initializes an instance of the PdfSaveOptions class that is passed to convert_epub() method that takes the stream, options, output file path save_path and performs the conversion operation. The DocSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting EPUB to PDF format. In the example, we use the page_setup and css.media_type properties.

Save Options – PdfSaveOptions Class

Aspose.HTML for Python via .NET provides the PdfSaveOptions class, which gives you more control over how documents are saved in PDF format. Some properties of this class inherit properties of base classes, such as PdfRenderingOptions or RenderingOptions. Here is a description of properties available in PdfSaveOptions:

Download the Aspose.HTML for Python via .NET library to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.

Aspose.HTML offers a free online EPUB to PDF Converter that converts EPUB to PDF with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!

Text “EPUB to PDF Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.