Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/bb92a013af4435240c695d69fee95862 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/bb92a013af4435240c695d69fee95862 to your computer and use it in GitHub Desktop.
Convert HTML, MHTML, EPUB, MD, and SVG to PDF, XPS, DOCX, PNG, JPG, and other formats in Python

Convert HTML, XHTML, MHTML, SVG, EPUB, and Markdown to Other Formats Using Python

This gist repository contains Python code examples used in the Converting Between Formats chapter of the Aspose.HTML for Python via .NET documentation. You will find examples for various common scenarios for HTML, XHTML, MHTML, SVG, EPUB, and Markdown conversion.

Topics Covered

  1. File Conversion – Convert HTML, SVG, EPUB, MHTML, and Markdown files into various output formats, including PDF, DOCX, XPS, and popular image formats, while preserving layout, styles, and embedded resources.
  2. Customizable Rendering Options – Fine-tune the conversion process using device-specific settings, such as page size, margins, resolution, compression, and other output format options to achieve precise control over the final document.
  3. Convert HTML programmatically in Python in different ways – Use convert_html() from the Converter class, render_to() from HTMLDocument, or render() from Renderer, depending on your workflow.

What's Included?

This repository provides examples for converting:

  • HTML – Shows how to convert an HTML document to PDF, XPS, DOCX, images, MD, and MHTML formats.
  • SVG – Provides information on how to convert SVG to PDF and image formats.
  • EPUB – Demonstrates how to convert EPUB to PDF, XPS, DOCX, and image formats.
  • MHTML – Describes how to convert MHTML to PDF, XPS, DOCX, and image formats.
  • Markdown – Provides information on how to convert MD to HTML, PDF, XPS, DOCX, and various image file formats.
  • Fine-Tuning Converters – Alternative ways to render HTML and HTML-based documents that can give you more control over the rendering process in your Python application.

Prerequisites

  • Python 3.5 or newer
  • .NET Core / .NET 5+ runtime installed
  • Supported OS: Windows, macOS, Linux
  • Aspose.HTML for Python via .NET installed from PyPI

How to Use These Examples

  1. Install the Aspose.HTML Python package:
   pip install aspose-html-net
  1. Clone or download this Gist to your local computer to access the code snippets.
  2. Verify and set the correct input/output paths, data directories, and any custom font folders referenced in the code.
  3. Run the specific Python example to execute the conversion logic and see the result in action.

About Aspose.HTML for Python via .NET

Aspose.HTML for Python via .NET is a robust API that provides headless browser capabilities for working with HTML documents and other supported web formats, such as MHTML, Markdown, SVG, and EPUB. It allows you to create new HTML files or load existing ones from various sources. Once loaded, you can manipulate the document's contents, such as adding, removing, or replacing nodes, as well as render it or convert it to popular formats, including PDF, DOCX, XPS, or images.

Related Resources

For detailed API usage, download instructions, and advanced conversion scenarios, please consult the official Aspose.HTML resources:

Aspose.HTML for Python via .NET – Converting Between Formats
# Convert EPUB to DOCX using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-epub-to-docx/
import aspose.html.converters as conv
import aspose.html.saving as sav
# Open an existing EPUB file for reading
with open("input.epub", "rb") as stream:
# Create an instance of DocSaveOptions
options = sav.DocSaveOptions()
# Convert EPUB to DOCX
conv.Converter.convert_epub(stream, options, "output.docx")
# Convert EPUB to DOCX using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-epub-to-docx/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "input.epub")
save_path = os.path.join(output_dir, "epub-to-docx.docx")
# Open an existing EPUB file for reading
with open(document_path, "rb") as stream:
# Create an instance of DocSaveOptions
options = sav.DocSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(800, 600), dr.Margin(10, 10, 10, 10))
options.font_embedding_rule.FULL
options.document_format.DOCX
options.css.media_type.SCREEN
# Convert EPUB to DOCX
conv.Converter.convert_epub(stream, options, save_path)
# Convert EPUB to JPG using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-epub-to-jpg/
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
# Open an existing EPUB file for reading
with open("input.epub", "rb") as stream:
# Create an instance of ImageSaveOptions
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
# Convert EPUB to JPG
conv.Converter.convert_epub(stream, options, "output.jpg")
# Convert EPUB to JPG using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-epub-to-jpg/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
import aspose.html.drawing as dr
import aspose.pydrawing as pd
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "input.epub")
save_path = os.path.join(output_dir, "epub-to-image.jpg")
# Open an existing EPUB file for reading
with open(document_path, "rb") as stream:
# Create an instance of ImageSaveOptions
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(150.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(150.0)
options.background_color = pd.Color.bisque
options.page_setup.any_page.size = dr.Size(500, 1000)
# Convert EPUB to JPG
conv.Converter.convert_epub(stream, options, save_path)
# Convert EPUB to DOCX using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-epub-to-pdf/
import aspose.html.converters as conv
import aspose.html.saving as sav
# Open an existing EPUB file for reading
with open("input.epub", "rb") as stream:
# Create an instance of PdfSaveOptions
options = sav.PdfSaveOptions()
# Convert EPUB to PDF
conv.Converter.convert_epub(stream, options, "output.pdf")
# Convert EPUB to PDF using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-epub-to-pdf/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "input.epub")
save_path = os.path.join(output_dir, "epub-to-pdf.pdf")
# Open an existing EPUB file for reading
with open(document_path, "rb") as stream:
# Create an instance of PdfSaveOptions
options = sav.PdfSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(800, 600), dr.Margin(10, 10, 10, 10))
options.css.media_type.PRINT
# Convert EPUB to PDF
conv.Converter.convert_epub(stream, options, save_path)
# Convert EPUB to PNG using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-epub-to-png/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
import aspose.pydrawing as pd
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "input.epub")
save_path = os.path.join(output_dir, "epub-to-image.png")
# Open an existing EPUB file for reading
with open(document_path, "rb") as stream:
# Create an instance of ImageSaveOptions
options = sav.ImageSaveOptions()
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(150.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(150.0)
options.page_setup.any_page.size = dr.Size(500, 1000)
options.css.media_type.SCREEN
options.text.use_hinting = True
# Convert EPUB to PNG
conv.Converter.convert_epub(stream, options, save_path)
# Convert EPUB to XPS using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/epub-converter/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
import aspose.pydrawing as pd
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "input.epub")
save_path = os.path.join(output_dir, "output.xps")
# Open an existing EPUB file for reading
with open(document_path, "rb") as stream:
# Create an instance of XpsSaveOptions
options = sav.XpsSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(600, 600), dr.Margin(40, 40, 10, 10))
options.css.media_type.PRINT
options.background_color = pd.Color.bisque
# Convert EPUB to XPS
conv.Converter.convert_epub(stream, options, save_path)
# Convert HTML to DOCX using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-docx/
import aspose.html.saving as sav
import aspose.html.converters as conv
# Convert HTML to DOCX
conv.Converter.convert_html("document.html", sav.DocSaveOptions(), "document.docx")
# Convert HTML to DOCX using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-docx/
import os
import aspose.html as ah
import aspose.html.saving as sav
import aspose.html.drawing as dr
import aspose.html.converters as conv
import aspose.pydrawing as pd
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "document.html")
save_path = os.path.join(output_dir, "document.docx")
# Load an HTML document from a file or URL
doc = ah.HTMLDocument(document_path)
# Initialize saving options
options = sav.DocSaveOptions()
options.page_setup.any_page.size = dr.Size(300, 300)
page_margin = dr.Margin(40, 40, 10, 10)
options.page_setup.any_page.margin = page_margin
options.document_format.DOCX
options.font_embedding_rule.FULL
options.css.media_type.PRINT
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(300.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(300.0)
options.background_color = pd.Color.bisque
# Convert HTML to DOCX
conv.Converter.convert_html(doc, options, save_path)
# Convert HTML to GIF using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-gif/
import os
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "document.html")
save_path = os.path.join(output_dir, "html-to-image.gif")
# Initialize an HTML document from the file
document = ah.HTMLDocument(document_path)
# Initialize ImageSaveOptions
options = sav.ImageSaveOptions(rim.ImageFormat.GIF)
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(96.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(96.0)
options.css.media_type.PRINT
# Convert HTML to GIF
conv.Converter.convert_html(document, options, save_path)
# Convert HTML to JPEG using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-jpeg/
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
# Load an HTML document from a file or URL
document = ah.HTMLDocument("document.html")
# Initialize saving options
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
# Convert HTML to JPEG
conv.Converter.convert_html(document, options, "output.jpeg")
# Convert HTML to JPEG using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-jpeg/
import os
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
import aspose.html.drawing as dr
import aspose.pydrawing as pd
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "document.html")
save_path = os.path.join(output_dir, "convert-html-with-options.jpg")
# Load an HTML document from a file or URL
document = ah.HTMLDocument(document_path)
# Initialize saving options
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(50.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(50.0)
options.background_color = pd.Color.bisque
options.page_setup.any_page = dr.Page(dr.Size(600, 500), dr.Margin(10, 10, 10, 10))
options.use_antialiasing = True
options.text.use_hinting = True
options.css.media_type.SCREEN
# Convert HTML to JPEG
conv.Converter.convert_html(document, options, save_path)
# Convert HTML to Markdown using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-markdown/
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
# Load an HTML document from a file or URL
document = ah.HTMLDocument("document.html")
# Initialize saving options
options = sav.MarkdownSaveOptions()
# Convert HTML to Markdown
conv.Converter.convert_html(document, options, "output.md")
# Convert HTML to Markdown using features property in Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-markdown/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
# Prepare directories and paths
output_dir = "output/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
save_path = os.path.join(output_dir, "options-output.md")
# Prepare HTML code and save it to a file
code = "<h1>Header 1</h1>" \
"<h2>Header 2</h2>" \
"<p>Hello, World!!</p>" \
"<a href='https://docs.aspose.com/'>aspose</a>"
with open(os.path.join(output_dir, "options.html"), "w") as file:
file.write(code)
# Create an instance of SaveOptions and set up the rule:
# – only <a> and <p> elements will be converted to Markdown
options = sav.MarkdownSaveOptions()
options.features = sav.MarkdownFeatures.LINK | sav.MarkdownFeatures.AUTOMATIC_PARAGRAPH
# Call the convert_html() method to convert HTML to Markdown
conv.Converter.convert_html(os.path.join(output_dir, "options.html"), options, save_path)
# Convert HTML to Markdown using git property in Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-markdown/
import aspose.html.converters as conv
import aspose.html.saving as sav
# Prepare HTML code and save it to a file
code = "<h1>Header 1</h1>" \
"<h2>Header 2</h2>" \
"<p>Hello, World!!</p>"
with open("document.html", "w", encoding="utf-8") as f:
f.write(code)
f.close()
# Call the convert_html() method to convert HTML to Markdown
conv.Converter.convert_html("document.html", sav.MarkdownSaveOptions.git, "output.md")
# Convert HTML to MHTML using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-mhtml/
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
# Load an HTML document from a file or URL
document = ah.HTMLDocument("document.html")
# Initialize saving options
options = sav.MHTMLSaveOptions()
# Convert HTML to MHTML
conv.Converter.convert_html(document, options, "output.mhtml")
# Convert HTML to MHTML using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-mhtml/
import aspose.html.converters as conv
import aspose.html.saving as sav
# Convert HTML to MHTML
conv.Converter.convert_html("document.html", sav.MHTMLSaveOptions(), "document.mht")
# Convert HTML to MHTML using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-mhtml/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
# Prepare directories and paths
output_dir = "output/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Prepare HTML code with a link to another file and save it to "document1.html"
code = "<span>Hello, World!!</span> <a href='document2.html'>click</a>"
with open("document1.html", "w") as file:
file.write(code)
# Prepare HTML code and save it to "document2.html"
code = "<span>Hello, World!!</span>"
with open("document2.html", "w") as file:
file.write(code)
save_path = os.path.join(output_dir, "output-options.mht")
# Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
options = sav.MHTMLSaveOptions()
options.resource_handling_options.max_handling_depth = 1
# Convert HTML to MHTML
conv.Converter.convert_html("document.html", options, save_path)
# Convert HTML to PDF using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-pdf/
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
# Load an HTML document from a file or URL
document = ah.HTMLDocument("document.html")
# Initialize saving options
options = sav.PdfSaveOptions()
# Convert HTML to PDF
conv.Converter.convert_html(document, options, "output.pdf")
# Flatten PDF during HTML to PDF conversion using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-pdf/
import os
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.pdf as rp
# Setup directories and define paths
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
source_path = os.path.join(data_dir, "SampleHtmlForm.html")
result_path = os.path.join(output_dir, "form-flattened.pdf")
# Load an HTML document
doc = ah.HTMLDocument(source_path)
# Initialize PdfSaveOptions
options = sav.PdfSaveOptions()
options.form_field_behaviour = rp.FormFieldBehaviour.FLATTENED
# Convert HTML to PDF
conv.Converter.convert_html(doc, options, result_path)
# Convert HTML to PDF using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-pdf/
import aspose.html.converters as conv
import aspose.html.saving as sav
# Convert HTML to PDF
conv.Converter.convert_html("document.html", sav.PdfSaveOptions(), "output.pdf")
# Convert HTML to PDF in Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-pdf/
import os
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
import aspose.pydrawing as pd
import aspose.html.rendering.pdf.encryption as rpe
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "document.html")
save_path = os.path.join(output_dir, "document-options.pdf")
# Load an HTML document from a file or URL
doc = ah.HTMLDocument(document_path)
# Initialize saving options
options = sav.PdfSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(800, 600), dr.Margin(10, 10, 10, 10))
options.css.media_type.PRINT
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(100.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(100.0)
options.background_color = pd.Color.bisque
options.is_tagged_pdf = True
options.jpeg_quality = 90
doc_info = options.document_info
doc_info.title = "Aspose HTML Example"
doc_info.author = "Your Name"
doc_info.subject = "PDF Conversion"
doc_info.keywords = "Aspose, HTML, PDF"
options.encryption = rpe.PdfEncryptionInfo(
user_password="user123",
owner_password="owner123",
permissions=rpe.PdfPermissions.PRINT_DOCUMENT | rpe.PdfPermissions.EXTRACT_CONTENT,
encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
)
# Convert HTML to PDF
conv.Converter.convert_html(doc, options, save_path)
# Convert HTML to PNG using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-png/
import aspose.html.converters as conv
import aspose.html.saving as sav
# Convert HTML to PNG
conv.Converter.convert_html("document.html", sav.ImageSaveOptions(), "output.png")
# Convert HTML to PNG with custom resolutions using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-png/
import os
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "banner.html")
save_path = os.path.join(output_dir, "banner-options-resolution.png")
# Initialize an HTML document from the file
document = ah.HTMLDocument(document_path)
# Initialize ImageSaveOptions
options = sav.ImageSaveOptions()
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(200.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(200.0)
# Convert HTML to PNG
conv.Converter.convert_html(document, options, save_path)
# Save HTML as XHTML using Python
# Learn more: https://docs.aspose.com/html/python-net/converting-between-formats/
import aspose.html as ah
import aspose.html.saving as sav
# Load an HTML document from a file or URL
document = ah.HTMLDocument("document.html")
# Initialize saving options
options = sav.HTMLSaveOptions()
options.document_type = sav.HTMLSaveOptions.XHTML
# Save HTML as XHTML
document.save("output.xhtml", options)
# Convert HTML to Markdown using git property in Python
# Learn more: https://docs.aspose.com/html/python-net/convert-html-to-markdown/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
# Prepare directories and paths
output_dir = "output/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
save_path = os.path.join(output_dir, "inline-html.md")
# Prepare HTML code and save it to a file
code = "text<div markdown='inline'><code>text</code></div>"
with open(os.path.join(output_dir, "inline.html"), "w") as file:
file.write(code)
# Call the convert_html() method for HTML to Markdown conversion
conv.Converter.convert_html(os.path.join(output_dir, "inline.html"), sav.MarkdownSaveOptions(), save_path)
# Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>
# Convert Markdown to DOCX using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-docx/
import aspose.html.converters as conv
import aspose.html.saving as sav
# Convert Markdown to HTML
document = conv.Converter.convert_markdown("document.md")
# Create an instance of DocSaveOptions
options = sav.DocSaveOptions()
# Convert HTML to DOCX
conv.Converter.convert_html(document, options, "document.docx")
# Convert Markdown to DOCX using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-docx/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
# Prepare a path to a source Markdown file
output_dir = "output/"
source_path = os.path.join(output_dir, "document.md")
# Prepare a simple Markdown example
code = "### Hello, World!\nConvert Markdown to DOCX!"
# Create a Markdown file
with open(source_path, "w") as file:
file.write(code)
# Prepare a path to save the converted file
save_path = os.path.join(output_dir, "document-output.docx")
# Convert Markdown to HTML document
document = conv.Converter.convert_markdown(source_path)
# Convert HTML document to DOCX file format
conv.Converter.convert_html(document, sav.DocSaveOptions(), save_path)
# Convert Markdown to DOCX using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-docx/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "document.md")
save_path = os.path.join(output_dir, "md-to-docx-with-save-options.docx")
# Convert Markdown to HTML
document = conv.Converter.convert_markdown(document_path)
# Create an instance of DocSaveOptions
options = sav.DocSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(900, 700), dr.Margin(40, 10, 10, 10))
options.document_format.DOCX
options.font_embedding_rule.FULL
# Convert HTML to DOCX
conv.Converter.convert_html(document, options, save_path)
# Convert Markdown to HTML using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-html/
import os
import aspose.html.converters as conv
# Setup directories and define paths
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
source_path = os.path.join(output_dir, "document.md")
# Prepare a simple Markdown example
code = "### Hello, World!\nConvert Markdown to HTML!"
# Create a Markdown file
with open(source_path, "w", encoding="utf-8") as file:
file.write(code)
# Prepare a path to save the converted file
save_path = os.path.join(output_dir, "document-output.html")
# Convert Markdown to HTML document
conv.Converter.convert_markdown(source_path, save_path)
# Convert Markdown to HTML using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-html/
import aspose.html.converters as conv
# Convert Markdown to HTML
document = conv.Converter.convert_markdown("document.md", "md-to-html.html")
# Convert Markdown to JPG using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-image/
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
# Convert Markdown to HTML
document = conv.Converter.convert_markdown("document.md")
# Create an instance of ImageSaveOptions
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
# Convert HTML to JPEG
conv.Converter.convert_html(document, options, "output.jpeg")
# Convert Markdown to JPG using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-image/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
import aspose.html.rendering.image as rim
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "document.md")
save_path = os.path.join(output_dir, "md-to-jpg-with-save-options.jpg")
# Convert Markdown to HTML
document = conv.Converter.convert_markdown(document_path)
# Create an instance of ImageSaveOptions
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(200.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(200.0)
options.css.media_type.PRINT
# Convert HTML to JPG
conv.Converter.convert_html(document, options, save_path)
# Convert Markdown to PDF using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-pdf/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
# Setup output directory and paths
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
source_path = os.path.join(output_dir, "document.md")
save_path = os.path.join(output_dir, "markdown-to-pdf.pdf")
# Create a simple Markdown example file
code = "### Hello, World!\nConvert Markdown to PDF!"
with open(source_path, "w") as file:
file.write(code)
# Convert Markdown file to an intermediate HTMLDocument
document = conv.Converter.convert_markdown(source_path)
# Create an instance of PdfSaveOptions
options = sav.PdfSaveOptions()
# Convert HTML to PDF
conv.Converter.convert_html(document, options, save_path)
# Convert Markdown to PDF using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-pdf/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "document.md")
save_path = os.path.join(output_dir, "md-to-pdf-with-save-options.pdf")
# Convert Markdown to HTML
document = conv.Converter.convert_markdown(document_path)
# Create an instance of PdfSaveOptions
options = sav.PdfSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(300, 300), dr.Margin(30, 10, 10, 10))
options.css.media_type.PRINT
options.jpeg_quality = 100
# Convert HTML to PDF
conv.Converter.convert_html(document, options, save_path)
# Convert Markdown to PNG using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-image/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
# Setup output directory and paths
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
source_path = os.path.join(output_dir, "document.md")
# Create a simple Markdown example file
code = "### Hello, World!\nConvert Markdown to PNG!"
with open(source_path, "w") as file:
file.write(code)
# Convert Markdown file to an intermediate HTMLDocument
document = conv.Converter.convert_markdown(source_path)
# Create ImageSaveOptions with PNG format
options = sav.ImageSaveOptions()
# Prepare output file path
save_path = os.path.join(output_dir, "markdown-to-image.png")
# Convert HTMLDocument to PNG image
conv.Converter.convert_html(document, options, save_path)
# Convert Markdown to PNG using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-image/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
import aspose.pydrawing as pd
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "document.md")
save_path = os.path.join(output_dir, "md-to-png-with-save-options.png")
# Convert Markdown to HTML
document = conv.Converter.convert_markdown(document_path)
# Create an instance of ImageSaveOptions
options = sav.ImageSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(500, 500), dr.Margin(40, 10, 10, 10))
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(150.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(150.0)
options.background_color = pd.Color.bisque
# Convert HTML to PNG
conv.Converter.convert_html(document, options, save_path)
# Convert Markdown to XPS using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-markdown-to-pdf/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "document.md")
save_path = os.path.join(output_dir, "md-to-xps-with-save-options.xps")
# Convert Markdown to HTML
document = conv.Converter.convert_markdown(document_path)
# Create an instance of XpsSaveOptions
options = sav.XpsSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(300, 300), dr.Margin(30, 10, 10, 10))
options.css.media_type.PRINT
# Convert HTML to XPS
conv.Converter.convert_html(document, options, save_path)
# Convert MHTML to DOCX using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-mhtml-to-docx/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "document.mht")
save_path = os.path.join(output_dir, "document.docx")
# Open an existing MHTML file for reading
with open(document_path, "rb") as stream:
# Create an instance of DocSaveOptions
options = sav.DocSaveOptions()
options.page_setup.any_page.size = dr.Size(1000, 800)
options.document_format.DOCX
options.css.media_type.SCREEN
# Convert MHTML to DOCX
conv.Converter.convert_mhtml(stream, options, save_path)
# Convert MHTML to JPG using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-mhtml-to-jpg/
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
# Open an MHTML file for reading
with open("document.mht", 'rb') as stream:
# Initialize saving options
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
# Convert MHTML to JPEG
conv.Converter.convert_mhtml(stream, options, "document.jpeg")
# Convert MHTML to JPG using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-mhtml-to-jpg/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "document.mht")
save_path = os.path.join(output_dir, "mhtml-to-image.jpg")
# Open an existing MHTML file for reading
with open(document_path, "rb") as stream:
# Create an instance of ImageSaveOptions
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(200.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(200.0)
options.css.media_type.SCREEN
options.text.use_hinting = True
# Convert MHTML to JPG
conv.Converter.convert_mhtml(stream, options, save_path)
# Convert MHTML to PDF using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-mhtml-to-pdf/
import aspose.html.converters as conv
import aspose.html.saving as sav
# Open an MHTML file for reading
with open("document.mht", 'rb') as stream:
# Initialize saving options
options = sav.PdfSaveOptions()
# Convert MHTML to PDF
conv.Converter.convert_mhtml(stream, options, "document.pdf")
# Convert MHTML to PDF with custom settings using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-mhtml-to-pdf/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "document.mht")
save_path = os.path.join(output_dir, "document.pdf")
# Open an existing MHTML file for reading
with open(document_path, "rb") as stream:
# Create an instance of PdfSaveOptions
options = sav.PdfSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(800, 600), dr.Margin(10, 10, 10, 10))
options.css.media_type.PRINT
options.jpeg_quality = 100
# Convert MHTML to PDF
conv.Converter.convert_mhtml(stream, options, save_path)
# Convert MHTML to PNG using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-mhtml-to-png/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "document.mht")
save_path = os.path.join(output_dir, "mhtml-to-image.png")
# Open an existing MHTML file for reading
with open(document_path, "rb") as stream:
# Create an instance of ImageSaveOptions
options = sav.ImageSaveOptions()
options.page_setup.any_page = dr.Page(dr.Size(800, 600), dr.Margin(40, 40, 10, 10))
options.css.media_type.PRINT
# Convert MHTML to PNG
conv.Converter.convert_mhtml(stream, options, save_path)
# Convert MHTML to XPS using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-mhtml-to-pdf/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
os.makedirs(output_dir, exist_ok=True)
document_path = os.path.join(input_dir, "document.mht")
save_path = os.path.join(output_dir, "document.xps")
# Open an existing MHTML file for reading
with open(document_path, "rb") as stream:
# Create an instance of XpsSaveOptions
options = sav.XpsSaveOptions()
options.page_setup.any_page.size = dr.Size(600, 600)
page_margin = dr.Margin(20, 20, 10, 10)
options.page_setup.any_page.margin = page_margin
options.css.media_type.PRINT
# Convert MHTML to XPS
conv.Converter.convert_mhtml(stream, options, save_path)
# Convert SVG to JPG using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-svg-to-jpg/
import aspose.html.dom.svg as ahsvg
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
# Load an SVG document from a file or URL
document = ahsvg.SVGDocument("flower.svg")
# Initialize saving options
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
# Convert SVG to JPEG
conv.Converter.convert_svg(document, options, "output.jpeg")
# Convert SVG code to JPG image using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-svg-to-jpg/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
# Setup directories and define paths
output_dir = "output/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
save_path = os.path.join(output_dir, "circle.jpg")
# Prepare SVG code
svg_code = """<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="70" fill="teal" stroke="pink" stroke-width="10" />
</svg>"""
# Initialize ImageSaveOptions
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
# Convert SVG to JPG
conv.Converter.convert_svg(svg_code, ".", options, save_path)
# Convert SVG to JPG using Python with custom settings
# Learn more: https://docs.aspose.com/html/python-net/convert-svg-to-jpg/
import os
import aspose.html.dom.svg as ahsvg
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.rendering.image as rim
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "tulips.svg")
save_path = os.path.join(output_dir, "svg-to-image.jpg")
# Load an SVG document
document = ahsvg.SVGDocument(document_path)
# Initialize ImageSaveOptions
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
options.horizontal_resolution = dr.Resolution.from_dots_per_inch(200.0)
options.vertical_resolution = dr.Resolution.from_dots_per_inch(200.0)
# Convert SVG to JPG
conv.Converter.convert_svg(document, options, save_path)
# Convert SVG to PDF using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-svg-to-pdf/
import aspose.html.dom.svg as ahsvg
import aspose.html.converters as conv
import aspose.html.saving as sav
# Load an SVG document from a file or URL
document = ahsvg.SVGDocument("flower.svg")
# Initialize saving options
options = sav.PdfSaveOptions()
# Convert SVG to PDF
conv.Converter.convert_svg(document, options, "output.pdf")
# Convert SVG code to PDF document using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-svg-to-pdf/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
# Setup directories and define paths
output_dir = "output/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
save_path = os.path.join(output_dir, "circles.pdf")
# SVG code
svg_code = """
<svg xmlns="http://www.w3.org/2000/svg">
<circle id="base" cx="100" cy="100" r="80" fill="teal" stroke="salmon" stroke-width="10" />
<g>
<use href="#base" transform="translate(120, 10) scale(0.9)" />
<use href="#base" transform="translate(240, 20) scale(0.8)" />
<use href="#base" transform="translate(360, 30) scale(0.7)" />
<use href="#base" transform="translate(480, 40) scale(0.6)" />
<use href="#base" transform="translate(600, 50) scale(0.5)" />
</g>
</svg>
"""
# Initialize PdfSaveOptions
options = sav.PdfSaveOptions()
# Convert SVG to PDF
conv.Converter.convert_svg(svg_code, ".", options, save_path)
# Convert SVG to PDF with custom settings using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-svg-to-pdf/
import os
import aspose.html.dom.svg as ahsvg
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "flower.svg")
save_path = os.path.join(output_dir, "svg-to-pdf.pdf")
# Load an SVG document
document = ahsvg.SVGDocument(document_path)
# Initialize PdfSaveOptions
options = sav.PdfSaveOptions()
# Customize save options for PDF
options.page_setup.any_page = dr.Page(dr.Size(600, 500), dr.Margin(20, 20, 10, 10))
options.css.media_type.PRINT
options.jpeg_quality = 80
options.is_tagged_pdf = True
# Convert SVG to PDF
conv.Converter.convert_svg(document, options, save_path)
# Convert SVG to PNG using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-svg-to-png/
import os
import aspose.html.converters as conv
import aspose.html.saving as sav
# Setup directories and define paths
output_dir = "output/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
save_path = os.path.join(output_dir, "circle.png")
# Prepare SVG code
svg_code = """<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="60" fill="teal" stroke="salmon" stroke-width="10" />
</svg>"""
# Initialize ImageSaveOptions
options = sav.ImageSaveOptions()
# Convert SVG to PNG
conv.Converter.convert_svg(svg_code, ".", options, save_path)
# Convert SVG to PNG with custom settings using Python
# Learn more: https://docs.aspose.com/html/python-net/convert-svg-to-png/
import os
import aspose.html.dom.svg as ahsvg
import aspose.html.converters as conv
import aspose.html.saving as sav
import aspose.html.drawing as dr
# Setup directories and define paths
output_dir = "output/"
input_dir = "data/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
document_path = os.path.join(input_dir, "tulips.svg")
save_path = os.path.join(output_dir, "tulips.png")
# Load an SVG document
document = ahsvg.SVGDocument(document_path)
# Initialize ImageSaveOptions
options = sav.ImageSaveOptions()
options.page_setup.first_page = dr.Page(dr.Size(500, 500), dr.Margin(10, 10, 10, 10))
options.css.media_type.PRINT
# Convert SVG to PNG
conv.Converter.convert_svg(document, options, save_path)
# Render EPUB to DOCX with custom page settings using Python
# Learn more: https://docs.aspose.com/html/python-net/renderers/
import os
import aspose.html.rendering.doc as rd
import aspose.html.rendering as rn
import aspose.html.drawing as dr
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare path to the source EPUB file
epub_path = os.path.join(data_dir, "input.epub")
save_path = os.path.join(output_dir, "render-epub-with-options.docx")
# Open the EPUB file in binary mode
with open(epub_path, "rb") as stream:
# Create an instance of EPUB Renderer
renderer = rn.EpubRenderer()
# Create DOC rendering options and set a custom page size
options = rd.DocRenderingOptions()
options.page_setup.any_page = dr.Page(dr.Size(800, 400))
# Create an instance of DocDevice for output
device = rd.DocDevice(options, save_path)
# Render EPUB to DOCX
renderer.render(device, stream)
# Render HTML to DOCX using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-device/
import os
import aspose.html as ah
import aspose.html.rendering.doc as rd
# Prepare output directory and path for the DOCX file
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
save_path = os.path.join(output_dir, "document.doc")
# Initialize an HTML document from URL
doc = ah.HTMLDocument("https://docs.aspose.com/html/files/document.html")
# Create a DocDevice and specify the output file to render
device = rd.DocDevice(save_path)
# Render HTML to DOCX
doc.render_to(device)
# Render HTML to DOCX with custom settings using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.rendering.doc as rd
import aspose.html.drawing as dr
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare a path to the source HTML file
document_path = os.path.join(data_dir, "nature.html")
# Initialize the HTML document from the file
doc = ah.HTMLDocument(document_path)
# Create an instance of DocRenderingOptions and set a custom page size
options = rd.DocRenderingOptions()
options.page_setup.any_page = dr.Page(dr.Size(dr.Length.from_inches(8.0), dr.Length.from_inches(10.0)))
options.font_embedding_rule = rd.FontEmbeddingRule.FULL
# Prepare a path to save the converted file
save_path = os.path.join(output_dir, "nature-options.docx")
# Create a DocDevice with the specified options and output file
device = rd.DocDevice(options, save_path)
# Render HTML to DOCX
doc.render_to(device)
# Render HTML to JPG using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-device/
import os
import aspose.html as ah
import aspose.html.rendering.image as ri
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare paths for input HTML and output JPG file
document_path = os.path.join(data_dir, "drawing.html")
save_path = os.path.join(output_dir, "drawing-output.jpg")
# Initialize an HTML document from the file
doc = ah.HTMLDocument(document_path)
# Create rendering options for image format (JPEG in this case)
image_options = ri.ImageRenderingOptions(ri.ImageFormat.JPEG)
# Create an ImageDevice and specify options and output file
device = ri.ImageDevice(image_options, save_path)
# Render HTML to JPG
doc.render_to(device)
# Render HTML to Pdf using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-device/
import os
import aspose.html as ah
import aspose.html.rendering.pdf as rp
# Prepare output directory and path for the PDF file
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
save_path = os.path.join(output_dir, "document.pdf")
# Prepare HTML code
code = "<span>Hello, World!!</span>"
# Initialize an HTML document from the HTML code
doc = ah.HTMLDocument(code, ".")
# Create a PDF Device and specify the output file to render
device = rp.PdfDevice(save_path)
# Render HTML to PDF
doc.render_to(device)
# Render HTML to PDF with background color setting using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.rendering.pdf as rp
import aspose.pydrawing as pd
# Prepare output directory and path for the PDF file
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare paths for input HTML and output PDF file
document_path = os.path.join(data_dir, "drawing.html")
save_path = os.path.join(output_dir, "drawing-background.pdf")
# Initialize an HTML document from the HTML code
doc = ah.HTMLDocument(document_path)
# Create rendering options for PDF format
pdf_options = rp.PdfRenderingOptions()
pdf_options.background_color = pd.Color.bisque
# Create a PDF Device and specify the output file to render
device = rp.PdfDevice(pdf_options, save_path)
# Render HTML to PDF
doc.render_to(device)
# Render HTML to PDF with custom page size settings using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.drawing as dr
import aspose.html.rendering.pdf as rp
# Setup output directory
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare HTML code with different widths for pages
code = """<style>
div { page-break-after: always; }
</style>
<div style='border: 1px solid red; width: 300px'>First Page</div>
<div style='border: 1px solid red; width: 500px'>Second Page</div>"""
# Initialize an HTML document from the HTML code
doc = ah.HTMLDocument(code, ".")
# Create PDF rendering options and set a custom base page size
options = rp.PdfRenderingOptions()
options.page_setup.any_page = dr.Page(dr.Size(400, 200))
# Enable auto-adjusting to the widest page content
options.page_setup.adjust_to_widest_page = True
# Prepare a path to save the converted file
save_path = os.path.join(output_dir, "output-widest-page-size.pdf")
# Create the PDF device with options and output path
device = rp.PdfDevice(options, save_path)
# Render HTML to PDF
doc.render_to(device)
# Render HTML to PDF with media type setting using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.rendering.pdf as rp
# Prepare output directory and path for the PDF file
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare paths for input HTML and output JPG file
document_path = os.path.join(data_dir, "drawing.html")
save_path = os.path.join(output_dir, "drawing-media.pdf")
# Initialize an HTML document from the HTML code
doc = ah.HTMLDocument(document_path)
# Create rendering options for PDF format
pdf_options = rp.PdfRenderingOptions()
pdf_options.css.media_type.PRINT
# Create a PDF Device and specify the output file to render
device = rp.PdfDevice(pdf_options, save_path)
# Render HTML to PDF
doc.render_to(device)
# Render HTML to PDF with password protection using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.rendering as rn
import aspose.html.rendering.pdf as rp
import aspose.html.rendering.pdf.encryption as rpe
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare path to the source HTML file
document_path = os.path.join(data_dir, "document.html")
# Initialize an HTML document from the file
doc = ah.HTMLDocument(document_path)
# Create an instance of the HTML Renderer
renderer = rn.HtmlRenderer()
# Prepare path to save the converted PDF
save_path = os.path.join(output_dir, "convert-html-to-pdf-with-encryption.pdf")
# Create PDF rendering options and set password protection
options = rp.PdfRenderingOptions()
options.encryption = rpe.PdfEncryptionInfo(
user_password="user_pwd",
owner_password="owner_pwd",
permissions=rpe.PdfPermissions.PRINT_DOCUMENT,
encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
)
# Create the PDF device with options and output path
device = rp.PdfDevice(options, save_path)
# Render HTML to PDF
renderer.render(device, doc)
# Render HTML to PDF with tag structure using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.rendering.pdf as rp
# Prepare directories and paths
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
save_path = os.path.join(output_dir, "tagged-document.pdf")
# HTML code
code = """
<html>
<head><title>Tagged PDF Example</title></head>
<body>
<h1>Hello, World!</h1>
<p>This PDF is generated with tagging enabled.</p>
<ul>
<li>Accessibility</li>
<li>Logical structure</li>
<li>Better content extraction</li>
</ul>
</body>
</html>
"""
# Create HTML document
doc = ah.HTMLDocument(code, ".")
# Create rendering options for PDF format
pdf_options = rp.PdfRenderingOptions()
pdf_options.is_tagged_pdf = True
# Create the PDF device with options and output path
device = rp.PdfDevice(pdf_options, save_path)
# Renderг HTML в PDF
doc.render_to(device)
# Render HTML to PDF with custom page settings using Python
# Learn more: https://docs.aspose.com/html/python-net/renderers/
import os
import aspose.html as ah
import aspose.html.rendering as rn
import aspose.html.rendering.pdf as rp
import aspose.html.drawing as dr
import aspose.html.rendering.pdf.encryption as rpe
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare path to the source HTML file
document_path = os.path.join(data_dir, "document.html")
# Initialize an HTML document from the file
doc = ah.HTMLDocument(document_path)
# Create an instance of the HTML Renderer
renderer = rn.HtmlRenderer()
# Prepare path to save the converted PDF
save_path = os.path.join(output_dir, "render-html-with-options.pdf")
# Create PDF rendering options and set custom page size
options = rp.PdfRenderingOptions()
options.page_setup.any_page = dr.Page(dr.Size(600, 200))
# Setup PDF encryption
options.encryption = rpe.PdfEncryptionInfo(
user_password="user_pwd",
owner_password="owner_pwd",
permissions=rpe.PdfPermissions.PRINT_DOCUMENT,
encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
)
# Create the PDF device with options and output path
device = rp.PdfDevice(options, save_path)
# Render HTML to PDF
renderer.render(device, doc)
# Render HTML to PNG with resolution setting using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.rendering.image as ri
import aspose.html.drawing as dr
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare path to the source HTML file
document_path = os.path.join(data_dir, "drawing.html")
# Initialize an HTML document from the file
doc = ah.HTMLDocument(document_path)
# Prepare output paths for different resolutions
save_path1 = os.path.join(output_dir, "output_resolution_50.png")
save_path2 = os.path.join(output_dir, "output_resolution_300.png")
# Render at low resolution (50 DPI)
options1 = ri.ImageRenderingOptions(ri.ImageFormat.PNG)
options1.horizontal_resolution = dr.Resolution.from_dots_per_inch(50.0)
options1.vertical_resolution = dr.Resolution.from_dots_per_inch(50.0)
device1 = ri.ImageDevice(options1, save_path1)
doc.render_to(device1)
# Render at high resolution (300 DPI)
options2 = ri.ImageRenderingOptions(ri.ImageFormat.PNG)
options2.horizontal_resolution = dr.Resolution.from_dots_per_inch(300.0)
options2.vertical_resolution = dr.Resolution.from_dots_per_inch(300.0)
device2 = ri.ImageDevice(options2, save_path2)
doc.render_to(device2)
# Render HTML to PDF using Python
# Learn more: https://docs.aspose.com/html/python-net/fine-tuning-converters/
import aspose.html as ah
import aspose.html.rendering.pdf as rp
doc = ah.HTMLDocument("https://docs.aspose.com/html/files/document.html")
doc.render_to(rp.PdfDevice("output/document.pdf"))
# Render HTML to TIFF with custom settings using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.rendering.image as ri
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare paths for input HTML and output TIFF file
document_path = os.path.join(data_dir, "html_file.html")
save_path = os.path.join(output_dir, "document.tiff")
# Initialize an HTML document from the file
doc = ah.HTMLDocument(document_path)
# Create rendering options for image format (TIFF in this case)
image_options = ri.ImageRenderingOptions(ri.ImageFormat.TIFF)
image_options.compression = ri.Compression.NONE
# Create an ImageDevice and specify options and output file
device = ri.ImageDevice(image_options, save_path)
# Render HTML to TIFF
doc.render_to(device)
# Render HTML to XPS using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-device/
import os
import aspose.html as ah
import aspose.html.rendering.xps as rx
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare paths for input HTML and output XPS file
document_path = os.path.join(data_dir, "drawing.html")
save_path = os.path.join(output_dir, "drawing-output.xps")
# Initialize an HTML document from the file
doc = ah.HTMLDocument(document_path)
# Create rendering options for XPS format
xps_options = rx.XpsRenderingOptions()
# Create an XpsDevice and specify options and output file
device = rx.XpsDevice(xps_options, save_path)
# Render HTML to XPS
doc.render_to(device)
# Render HTML to XPS with custom page settings using Python
# Learn more: https://docs.aspose.com/html/python-net/rendering-options/
import os
import aspose.html as ah
import aspose.html.rendering.xps as rx
import aspose.html.drawing as dr
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare a path to the source HTML file
document_path = os.path.join(data_dir, "document.html")
# Initialize the HTML document from the file
doc = ah.HTMLDocument(document_path)
# Create an instance of XPS Rendering Options
options = rx.XpsRenderingOptions()
# Set custom page size (5 x 2 inches)
options.page_setup.any_page = dr.Page(dr.Size(dr.Length.from_inches(5.0), dr.Length.from_inches(2.0)))
# Prepare a path to save the converted file
save_path = os.path.join(output_dir, "document-options.xps")
# Create an XpsDevice with the specified options and output file
device = rx.XpsDevice(options, save_path)
# Render HTML to XPS
doc.render_to(device)
# Render MHTML to PDF with custom page settings using Python
# Learn more: https://docs.aspose.com/html/python-net/renderers/
import os
import aspose.html.rendering.pdf as rp
import aspose.html.rendering as rn
import aspose.html.drawing as dr
import aspose.pydrawing as pd
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare path to the source MHTML file
epub_path = os.path.join(data_dir, "document.mht")
save_path = os.path.join(output_dir, "render-mhtml-with-options.pdf")
# Open the MHTML file in binary mode
with open("document.mht", 'rb') as stream:
# Create an instance of MHTML Renderer
renderer = rn.MhtmlRenderer()
# Create PDF rendering options and set a custom page size and background color
options = rp.PdfRenderingOptions()
options.page_setup.any_page = dr.Page(dr.Size(800, 400))
options.background_color = pd.Color.bisque
# Create an instance of PdfDevice for output
device = rp.PdfDevice(options, save_path)
# Render MHTML to PDF
renderer.render(device, stream)
# Render SVG to PDF with custom page settings using Python
# Learn more: https://docs.aspose.com/html/python-net/renderers/
import os
import aspose.html as ah
import aspose.html.rendering.pdf as rp
import aspose.html.rendering as rn
import aspose.html.drawing as dr
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare path to the source SVG file
doc_path = os.path.join(data_dir, "flower.svg")
save_path = os.path.join(output_dir, "render-svg-with-options.pdf")
# Initialize an SVG document from the file
doc = ah.dom.svg.SVGDocument(doc_path)
# Create an instance of the SVG Renderer
renderer = rn.SvgRenderer()
# Create the instance of Rendering Options and set a custom page-size
options = rp.PdfRenderingOptions()
options.page_setup.any_page = dr.Page(dr.Size(600, 200))
# Create the PDF device with options and output path
device = rp.PdfDevice(options, save_path)
# Render SVG to PDF
renderer.render(device, doc)
# Render SVG to PNG with custom page settings using Python
# Learn more: https://docs.aspose.com/html/python-net/renderers/
import os
import aspose.html as ah
import aspose.html.rendering.image as ri
import aspose.html.rendering as rn
import aspose.html.drawing as dr
# Setup input and output directories
data_dir = "data/"
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Prepare path to the source SVG file
doc_path = os.path.join(data_dir, "flower.svg")
save_path = os.path.join(output_dir, "render-svg-with-options.png")
# Initialize an SVG document from the file
doc = ah.dom.svg.SVGDocument(doc_path)
# Create an instance of the SVG Renderer
renderer = rn.SvgRenderer()
# Create the instance of Rendering Options and set a custom page-size
options = ri.ImageRenderingOptions()
options.page_setup.any_page = dr.Page(dr.Size(400, 300))
# Create the PNG device with options and output path
device = ri.ImageDevice(options, save_path)
# Render SVG to PNG
renderer.render(device, doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment