Convert Markdown to PDF in Python – Aspose.HTML

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 is required rendering Markdown document, for instance, to the PDF file format, the following example demonstrates how that is simple:

  1. Load a source Markdown document. In the example, we create a Markdown file from code.
  2. Convert Markdown to HTML. Use the convert_markdown() method to save Markdown as an HTML document.
  3. Create a new PdfSaveOptions object and specify the required properties.
  4. Use the convert_html() method of the Converter class. You need to pass the HTMLDocument, PdfSaveOptions, and output file path save_path to the convert_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:

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.

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 our 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!

Text “MD to PDF Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.