Render HTML as a PNG Image
Aspose.HTML for Java
creates PNG images from HTML files, strings, and URLs. Load the page with HTMLDocument, select ImageFormat.Png in ImageSaveOptions, and invoke
Converter.convertHTML()
. The output reflects the rendered HTML, CSS, fonts, and linked images.
Java Code for Lossless PNG Output
The example loads a local HTML document, explicitly selects PNG, and saves the rendered page as output.png.
Convert HTML to PNG in Java
import com.aspose.html.HTMLDocument;
import com.aspose.html.converters.Converter;
import com.aspose.html.rendering.image.ImageFormat;
import com.aspose.html.saving.ImageSaveOptions;
// Load the HTML document
try (HTMLDocument document = new HTMLDocument("document.html")) {
// Select PNG output
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Convert HTML to PNG
Converter.convertHTML(document, options, "output.png");
}
Test HTML to PNG Rendering Online
Upload an HTML file or enter a webpage URL to review the PNG result before integrating the rendering workflow into Java code.
HTML to PNG API Steps
- Add Aspose.HTML for Java to the project.
- Load the HTML file or URL with
HTMLDocument. - Select
ImageFormat.PnginImageSaveOptions. - Pass the document, options, and output path to
Converter.convertHTML().
Control PNG Size and Transparency
ImageSaveOptions provides settings for resolution, page dimensions, margins, background color, smoothing, CSS processing, and text rendering. PNG supports transparency and preserves image data without lossy compression. A background defined by the page or its CSS can still render as opaque.
See
Convert HTML to Image in Java
and the
ImageSaveOptions API Reference
.
Install Aspose.HTML for Java
Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the
Aspose.HTML for Java releases
page, then reload or rebuild the project so the dependency is resolved.
Add the repository and dependency to the Maven project:
Maven – pom.xml
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html</artifactId>
<version>ASPOSE_HTML_VERSION</version>
</dependency>
</dependencies>Add the repository and dependency to the Gradle project:
Gradle – build.gradle
repositories {
maven {
url = uri('https://releases.aspose.com/java/repo/')
}
}
dependencies {
implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .
HTML to PNG Conversion FAQ
1. Can HTML be converted to PNG with a transparent background?
Yes. PNG supports transparency, and the default image background is transparent. Any background color or image defined by the HTML or CSS is still rendered.
2. Is PNG suitable for text-heavy HTML pages?
Yes. Lossless PNG output preserves sharp edges in text, icons, diagrams, and interface elements, although it can be larger than JPG for photographic content.
3. How do I control the PNG screenshot dimensions?
Set page size, margins, and horizontal and vertical resolution through ImageSaveOptions. These settings determine the rendered page area and pixel density.
4. Can I convert HTML to PNG online for free?
Yes. Use the free online HTML to PNG converter for a manual conversion, or use the Java API for automated rendering.
Related Java Conversions
Choose another output format for the same HTML source.