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:
- 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.
- Create an instance of PdfSaveOptions class and specify the required save options.
- 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 theconvert_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
:
- page_setup – This property provides access to a PageSetup object used to configure the layout and settings of the output PDF pages to fit specific printing or display requirements.
- jpeg_quality – This property determines the JPEG compression quality used for images embedded in a PDF document. The default quality is set to 95, providing a good balance between image fidelity and file size. Setting this property allows you to optimize file size or image quality based on your specific needs.
- encryption – This property provides detailed information about PDF document encryption, including password protection and permission settings. If it is not configured, no encryption is applied, but setting this property allows you to distribute and control access to sensitive PDF content securely.
- horizontal_resolution – This property controls the horizontal resolution for both internal images used during processing and any external images included in the HTML. By default, it is set to 300 dpi.
- vertical_resolution – Similar to horizontal_resolution, this property manages the vertical resolution for internal and external images during PDF generation. Like its horizontal counterpart, it defaults to 300 dpi.
- background_color – This property sets or retrieves the background color that fills each PDF document page. The default value is transparent, but this can be customized to suit branding or aesthetic preferences, ensuring consistency across all pages.
- css – This property uses a
CssOptions
object to configure the processing of CSS properties during HTML to PDF conversion. It allows precise control over how styles from the HTML are interpreted and applied in the resulting PDF. - document_info – This property contains metadata and information about the output PDF document, such as title, author, subject, and keywords. This metadata helps document management, indexing, and searchability, making the PDF more informative and organized.
- form_field_behaviour – This property specifies the behavior of interactive form fields in the generated PDF.
- is_tagged_pdf – When set to true, a tagged layout is created within the PDF document, enhancing accessibility for users with disabilities. This ensures that content is properly structured and navigable using assistive technology and meets accessibility standards.
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!