Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
The basic workflow uses a local HTML file and the default PNG save settings:
ImageSaveOptions object. PNG is the default image format.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.
Use a Url object to render an online HTML page as PNG:
Url object for the webpage.ImageSaveOptions and specify an output path.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.
Use ImageSaveOptions to select PNG output and configure the page layout, resolution, and antialiasing:
HTMLDocument.ImageSaveOptions for PNG. 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:

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.
page_setup.any_page.size in pixels when exact PNG dimensions are required.background_color when the PNG requires a solid page background.For shared page, resolution, CSS, and rasterization settings, see Configure Image Output with ImageSaveOptions.
| Problem | Check or fix |
|---|---|
| Transparency is not visible | Check 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 blurred | Increase the actual pixel dimensions and use sufficiently detailed source images. PNG compression itself is lossless. |
| The PNG is larger than expected | Reduce 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.
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.
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.
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.
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.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.