Convert HTML Files, Strings, and Webpages to JPEG
Aspose.HTML for Python via .NET
converts an HTML file, string, document, or URL to JPEG with
Converter.convert_html()
and
ImageSaveOptions
. Select ImageFormat.JPEG explicitly because the image save options use PNG by default.
JPEG is a lossy, opaque raster format. It is commonly suitable for photographs, gradients, and pages with complex color areas, but PNG is usually a better choice for transparent backgrounds, sharp interface graphics, or text that must avoid compression artifacts.
Test HTML to JPEG and View the Python Code
Add an HTML file or webpage URL, then keep JPEG selected to create a raster image. The widget displays Python code for the selected source and output format.
{{#if_output 'PDF' 'DOCX' 'XPS' 'MHTML' 'MD' }}
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP'}}
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'XHTML'}}
import aspose.html as ah
import aspose.html.saving as sav
{{/if_output}}
with ah.HTMLDocument("{{input lower}}") as document:
{{#if_output 'PDF'}}
options = sav.PdfSaveOptions()
{{/if_output}}
{{#if_output 'DOCX'}}
options = sav.DocSaveOptions()
{{/if_output}}
{{#if_output 'XPS'}}
options = sav.XpsSaveOptions()
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP'}}
options = sav.ImageSaveOptions(rim.ImageFormat.{{output param2 upper}})
{{/if_output}}
{{#if_output 'MHTML'}}
options = sav.MHTMLSaveOptions()
{{/if_output}}
{{#if_output 'MD'}}
options = sav.MarkdownSaveOptions.default
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP' 'PDF' 'MHTML' 'MD' 'XPS' 'DOCX'}}
conv.Converter.convert_html(document, options, "output.{{output lower}}")
{{/if_output}}
{{#if_output 'XHTML'}}
options = sav.HTMLSaveOptions()
options.document_type = sav.HTMLSaveOptions.XHTML
document.save("output.xhtml", options)
{{/if_output}}
The generated example selects ImageFormat.JPEG and writes output.jpeg. Choose the page background before conversion when transparent HTML regions must be composited against a specific color.
Python Code to Convert HTML to JPEG
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
with ah.HTMLDocument("document.html") as document:
options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
conv.Converter.convert_html(document, options, "output.jpeg")
How to Convert HTML to JPEG in Python
- Install the
aspose-html-netpackage. - Open an HTML file or webpage URL in
HTMLDocument. Provide a base URI when an HTML string contains relative resource references. - Create
ImageSaveOptionsand selectImageFormat.JPEG. - Set the page dimensions, resolution, CSS options, and opaque background required by the destination workflow.
- Call
Converter.convert_html()with the document, options, and.jpegoutput path.
Set JPEG Page Size, Resolution, and Output
Use ImageSaveOptions to control the raster canvas and the way HTML content is drawn:
- Page setup: Set dimensions and margins to control pagination and the size of each output image.
- Resolution: Horizontal and vertical resolution affect the pixel density of pages defined in physical units. Pixel-based page dimensions already define the raster size directly.
- Background: Set an opaque
background_colorwhen transparent HTML regions must be composited against a specific color. JPEG does not support transparency. - CSS processing: Select the media rules and style handling required for the target layout.
- Image processing: Adjust antialiasing and text hinting when the default treatment of edges or glyphs does not suit the output.
- Relative resources: Preserve the source location or provide a base URI for relative image, stylesheet, and font URLs.
- External assets and fonts: Keep remote photographs, styles, and web fonts accessible until conversion is complete, and install required local fonts to avoid substitution.
- Loaded document state: Convert the content available after loading. Later clicks, form interaction, and asynchronous browser actions are not reproduced.
See
Convert HTML to Image in Python
for the shared image workflow and the
ImageSaveOptions API Reference
for available properties.
Install Aspose.HTML for Python via .NET
Install the aspose-html-net package from
PyPI
.
Install Aspose.HTML for Python via .NET
pip install aspose-html-net
Review the installation guide and system requirements before deployment. Request a temporary license to test the API without evaluation limitations.
HTML to JPEG FAQ
1. Can ImageSaveOptions set JPEG quality during HTML to JPEG conversion?
No JPEG quality property is exposed by the current Aspose.HTML ImageSaveOptions API. Its compression property controls TIFF encoding only. If a workflow requires a specific JPEG quality level, create the JPEG first and apply that encoding requirement in a separate image-processing step.
2. Why did one HTML document produce several JPEG files?
Image conversion is page-based. When the converted HTML spans several pages, the converter writes separate numbered JPEG files. Adjust the page dimensions or source layout when the result must fit one image.
3. Is text selectable in an HTML to JPEG result?
No. JPEG is a raster image, so text, hyperlinks, and controls become pixels. Use PDF or DOCX when the destination requires selectable text or document structure.
4. Should the output file use the .jpg or .jpeg extension?
Both extensions identify JPEG files. Select ImageFormat.JPEG and use either .jpg or .jpeg consistently in the output path and downstream workflow.
JPEG and Raster Image Conversion Guides
Compare JPEG with other image outputs or return to the complete Python conversion catalog.