Created
February 11, 2026 10:09
-
-
Save aspose-com-gists/a108eaeac9f9c638d24f540742c5daf3 to your computer and use it in GitHub Desktop.
How to Convert HTML to JPG with Python via .NET Using Aspose.HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import aspose.html as ah | |
| from aspose.html import HtmlDocument, ImageSaveOptions, ImageFormat, Color | |
| def convert_html_to_jpg(input_path: str, output_path: str, | |
| width: int = 1200, height: int = 800, | |
| dpi: int = 300, quality: int = 90) -> None: | |
| """ | |
| Converts an HTML file to a JPG image with the specified rendering options. | |
| """ | |
| try: | |
| # Load the HTML document from a file | |
| document = HtmlDocument(input_path) | |
| # Configure image save options | |
| options = ImageSaveOptions() | |
| options.image_format = ImageFormat.Jpeg | |
| options.width = width | |
| options.height = height | |
| options.dpi = dpi | |
| options.jpeg_quality = quality | |
| options.background_color = Color.white | |
| # Render and save the JPG file | |
| document.save(output_path, options) | |
| print(f"Conversion successful: '{output_path}'") | |
| except Exception as e: | |
| print(f"Error during conversion: {e}") | |
| finally: | |
| # Ensure resources are released | |
| if 'document' in locals(): | |
| document.dispose() | |
| # Example usage | |
| if __name__ == "__main__": | |
| html_file = "sample.html" | |
| jpg_file = "sample_output.jpg" | |
| convert_html_to_jpg(html_file, jpg_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment