Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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.
If your scenario requires rendering Markdown document, for instance, to the PDF file format, the following example demonstrates how that is simple:
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:
1# Convert Markdown to PDF using Python
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6
7# Setup output directory and paths
8output_dir = "output/"
9os.makedirs(output_dir, exist_ok=True)
10source_path = os.path.join(output_dir, "document.md")
11save_path = os.path.join(output_dir, "markdown-to-pdf.pdf")
12
13# Create a simple Markdown example file
14code = "### Hello, World!\nConvert Markdown to PDF!"
15with open(source_path, "w") as file:
16 file.write(code)
17
18# Convert Markdown file to an intermediate HTMLDocument
19document = conv.Converter.convert_markdown(source_path)
20
21# Create an instance of PdfSaveOptions
22options = sav.PdfSaveOptions()
23
24# Convert HTML to PDF
25conv.Converter.convert_html(document, options, save_path)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:
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.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:
1# Convert Markdown to PDF using Python with custom settings
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6import aspose.html.drawing as dr
7
8# Setup directories and define paths
9output_dir = "output/"
10input_dir = "data/"
11if not os.path.exists(output_dir):
12 os.makedirs(output_dir)
13document_path = os.path.join(input_dir, "document.md")
14save_path = os.path.join(output_dir, "md-to-pdf-with-save-options.pdf")
15
16# Convert Markdown to HTML
17document = conv.Converter.convert_markdown(document_path)
18
19# Create an instance of PdfSaveOptions
20options = sav.PdfSaveOptions()
21options.page_setup.any_page = dr.Page(dr.Size(300, 300), dr.Margin(30, 10, 10, 10))
22options.css.media_type.PRINT
23options.jpeg_quality = 100
24
25# Convert HTML to PDF
26conv.Converter.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.
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.jpeg_quality is set to 100, which maximizes the quality of any JPEG images included in the PDF.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.
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 = sav.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.
See Also
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 download the complete examples and data files from GitHub.
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!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.