Convert HTML to PNG in Python

To convert HTML to PNG in Python, call Converter.convert_html() with an HTML source, ImageSaveOptions, and an output path. The source can be an HTML file, an HTMLDocument, inline HTML with a base URI, or a webpage URL supported by the selected overload.

PNG uses lossless compression and supports transparency. It avoids the compression artifacts associated with lossy formats, making it suitable for webpage captures, diagrams, interface previews, text, and line graphics. Aspose.HTML for Python via .NET renders the HTML and CSS before writing the result as one or more PNG images.

Convert HTML to PNG – Python Example

The basic workflow uses a local HTML file and the default PNG save settings:

  1. Specify the source HTML file.
  2. Create an ImageSaveOptions object. PNG is the default image format.
  3. Call Converter.convert_html() with the source, options, and output PNG path.
1# Convert HTML to PNG in Python
2
3import aspose.html.converters as conv
4import aspose.html.saving as sav
5
6# Convert HTML to PNG
7conv.Converter.convert_html("document.html", sav.ImageSaveOptions(), "output.png")

This overload reads document.html, applies the default image settings, and writes output.png.

Convert a Webpage URL to PNG in Python

Use a Url object to render an online HTML page as PNG:

  1. Create a Url object for the webpage.
  2. Create ImageSaveOptions and specify an output path.
  3. Pass the URL, options, and output path to Converter.convert_html().
 1import os
 2import aspose.html as ah
 3import aspose.html.converters as conv
 4import aspose.html.saving as sav
 5
 6output_dir = "output"
 7os.makedirs(output_dir, exist_ok=True)
 8
 9url = ah.Url("https://docs.aspose.com/html/files/aspose.html")
10output_path = os.path.join(output_dir, "aspose-from-url.png")
11
12options = sav.ImageSaveOptions()
13conv.Converter.convert_html(url, options, output_path)

The application must have network access to the page and its linked resources. This example uses a static sample HTML file; a complex website can take longer to process while external resources are loaded.

Convert HTML to PNG Using ImageSaveOptions

Use ImageSaveOptions to select PNG output and configure the page layout, resolution, and antialiasing:

  1. Load the HTML source into an HTMLDocument.
  2. Create ImageSaveOptions for PNG.
  3. Configure the required page and rendering settings.
  4. Convert the document and save the PNG output.
 1import os
 2import aspose.html as ah
 3import aspose.html.converters as conv
 4import aspose.html.drawing as dr
 5import aspose.html.rendering.image as rim
 6import aspose.html.saving as sav
 7
 8input_dir = "data"
 9output_dir = "output"
10os.makedirs(output_dir, exist_ok=True)
11
12input_path = os.path.join(input_dir, "banner.html")
13output_path = os.path.join(output_dir, "banner-options-resolution.png")
14
15with ah.HTMLDocument(input_path) as document:
16    options = sav.ImageSaveOptions(rim.ImageFormat.PNG)
17    options.page_setup.any_page.size = dr.Size(
18        dr.Length.from_millimeters(210.0),
19        dr.Length.from_millimeters(297.0)
20    )
21    options.horizontal_resolution = dr.Resolution.from_dots_per_inch(200.0)
22    options.vertical_resolution = dr.Resolution.from_dots_per_inch(200.0)
23    options.use_antialiasing = True
24    conv.Converter.convert_html(document, options, output_path)

The example converts banner.html, explicitly selects PNG, sets an A4 page and 200 dpi resolution, and enables antialiasing. The source HTML uses a viewport-dependent flex layout with height: 100vh. When the output page is set to A4, the layout is recalculated for the A4 page instead of the browser window. As a result, the banner width and text wrapping differ from the browser view. The 200 dpi setting determines the raster dimensions and produces a 1654 × 2339 pixel PNG. The illustration below shows a cropped preview of the rendered A4 page:

HTML banner rendered to an A4 PNG page at 200 dpi

Control PNG Dimensions, Transparency, and Sharpness

PNG uses lossless compression, so it preserves text, line graphics, and interface details without JPEG-style compression artifacts. The visible result still depends on the rendered pixel dimensions, fonts, CSS layout, and source images.

For shared page, resolution, CSS, and rasterization settings, see Configure Image Output with ImageSaveOptions.

Common HTML to PNG Conversion Issues

ProblemCheck or fix
Transparency is not visibleCheck whether the HTML or CSS defines a background color. background_color is transparent by default, but a document background is still rendered.
Text or images look blurredIncrease the actual pixel dimensions and use sufficiently detailed source images. PNG compression itself is lossless.
The PNG is larger than expectedReduce the page dimensions or simplify large raster resources. Lossless PNG output can be larger than JPEG for photographic content.

For pagination, page sizing, missing resources, and URL-loading problems, see Common HTML to Image Conversion Issues.

Related HTML Conversion Guides

Other Platforms

FAQ

Can I convert an HTML string to PNG?

Yes. Use the Converter.convert_html() overload that accepts the HTML string, a base URI, ImageSaveOptions, and an output path. The base URI is required when the string contains relative resource URLs.

Does HTML to PNG conversion preserve transparency?

PNG supports transparency, and the default background_color is transparent. A background defined by the HTML or CSS is still rendered. Set background_color explicitly when the output requires a solid page background.

Can I convert HTML to PNG for free?

Use the free online HTML to PNG Converter below for occasional manual conversions. For programmatic conversion, you can evaluate the library without a license or follow the licensing guide to request a temporary license.

Try Online HTML to PNG Conversion

                
            

Use the free online HTML to PNG Converter for quick manual conversion without writing code. Use Aspose.HTML for Python via .NET when HTML-to-PNG conversion must run in a Python application, service, or document-processing workflow.

Free Online HTML to PNG Converter