Convert Markdown to PDF in Python
This article provides information on how to convert Markdown to PDF using the Aspose.HTML for Python via .NET API. You will learn about the supported Markdown to PDF conversion scenarios and consider Python examples to illustrate them. Also, you can try an Online Markdown Converter to test the Aspose.HTML functionality and convert Markdown on the fly.
Online Markdown Converter
You can convert Markdown to other formats with Aspose.HTML in real time. Load a Markdown file, select the output format and run the example. The save options are configured by default. You will instantly receive the conversion result as a separate file.
If you want to convert Markdown to PDF programmatically, please see the following Python code examples.
Convert Markdown to PDF
If your scenario requires rendering Markdown document, for instance, to the PDF file format, the following example demonstrates how that is simple:
- Load a source Markdown document. In the example, we create a Markdown file from code.
- Convert Markdown to HTML. Use the convert_markdown() method to save Markdown as an HTML document.
- Create a new PdfSaveOptions object and specify the required properties.
- Use the
convert_html() method of the Converter class. You need to pass the
HTMLDocument
,PdfSaveOptions
, and output file pathsave_path
to theconvert_html()
method.
If your case is to create a Markdown document from a user string directly in your code and convert it to a PDF file, the following example could help you:
1import os
2from aspose.html import *
3from aspose.html.saving import *
4from aspose.html.converters import *
5
6# Prepare a path to a source Markdown file
7output_dir = "output/"
8source_path = os.path.join(output_dir, "document.md")
9
10# Prepare a simple Markdown example
11code = "### Hello, World!\nConvert Markdown to PDF!"
12
13# Create a Markdown file
14with open(source_path, "w") as file:
15 file.write(code)
16
17# Convert Markdown to HTML document
18document = Converter.convert_markdown(source_path)
19
20# Create an instance of PdfSaveOptions
21options = PdfSaveOptions()
22
23# Prepare a path to save the converted file
24save_path = os.path.join(output_dir, "markdown-to-pdf.pdf")
25
26# Convert HTML to PDF
27Converter.convert_html(document, options, save_path)
Convert Markdown to PDF using PdfSaveOptions
The process of converting Markdown to PDF can be flexibly customized. Aspose.HTML for Python via .NET provides the
PdfSaveOptions class, which gives you more control over how documents are saved in PDF format. 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.
- 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.
- 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.
- 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.
You should use the PdfSaveOptions
class to specify additional options that affect the result of saving a document as a PDF. This class contains properties that determine how the PDF output will be displayed. The following Python code snippet shows how to convert Markdown to PDF using PdfSaveOptions:
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, "document.md")
13save_path = os.path.join(output_dir, "output.pdf")
14
15# Convert Markdown to HTML document
16document = Converter.convert_markdown(document_path)
17
18# Create an instance of PdfSaveOptions
19options = PdfSaveOptions()
20options.page_setup.any_page = Page(Size(300, 300), Margin(10, 10, 10, 10))
21options.css.media_type.PRINT
22options.jpeg_quality = 100
23
24# Convert HTML to PDF
25Converter.convert_html(document, options, save_path)
In this code, the PdfSaveOptions
class from Aspose.HTML for Python via .NET is used to customize the conversion of Markdown to PDF.
- The
page_setup
property configures the page layout settings for the output PDF, setting the page size to 300x300 units with margins of 10 units on all sides. - The
jpeg_quality
is set to 100, which maximizes the quality of any JPEG images included in the PDF. - The
css.media_type
is set to PRINT, which specifies that the CSS media type for print should be used during conversion, ensuring proper styling.
These settings ensure that the resulting PDF is well-formatted, styled appropriately for print, and contains high-quality images.
How to convert Markdown to XPS
Aspose.HTML for Python via .NET supports Markdown to XPS conversion. To do this, you should use
XpsSaveOptions to get a save options object that is passed to the convert_html()
method:
options = XpsSaveOptions()
XpsSaveOptions usage enables you to customize the rendering process; you can specify the page_setup, background_color, css, horizontal_resolution, and vertical_resolution properties.
Download the Aspose.HTML for Python via .NET library allows you to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.
You can check the quality of Markdown to PDF conversion with our online MD to PDF Converter. Upload, convert your files and get results in a few seconds. Try our forceful Markdown to PDF Converter for free now!