Render XHTML as a JPG Image in Java

Aspose.HTML for Java loads XHTML into an HTMLDocument and renders it with Converter.convertHTML() . Select ImageFormat.Jpeg in ImageSaveOptions to create a JPG file and configure resolution, page dimensions, background, CSS processing, and text rendering.


Create a JPG Image from XHTML

The example loads input.xhtml, selects JPEG output, and writes output.jpg. Try-with-resources guarantees cleanup of the source document.

Convert XHTML to JPG 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 XHTML document
try (HTMLDocument document = new HTMLDocument("input.xhtml")) {
    // Select JPEG output
    ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);

    // Convert XHTML to JPG
    Converter.convertHTML(document, options, "output.jpg");
}

Test XHTML to JPG Conversion Online

Upload an XHTML file or provide its URL to inspect the JPG result before adding the conversion workflow to a Java application.

XHTML to JPG API Steps

  1. Add Aspose.HTML for Java to the project.
  2. Open the source with HTMLDocument .
  3. Create ImageSaveOptions with ImageFormat.Jpeg.
  4. Pass the document, options, and output path to Converter.convertHTML() .
  5. Close the source document with try-with-resources.

Configure JPG Resolution and Page Size

Use ImageSaveOptions to control horizontal and vertical resolution, page dimensions, background color, CSS processing, and text rendering. JPG uses lossy compression and does not preserve transparency, so PNG is usually a better target for graphics that require transparent areas or lossless edges.

See the HTML Converter guide 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 .


XHTML to JPG Conversion FAQ

1. How do I convert XHTML to JPG in Java?

Load the XHTML file into an HTMLDocument, create ImageSaveOptions with ImageFormat.Jpeg, and call Converter.convertHTML() with the output path.

2. Are JPG and JPEG different output formats?

No. JPG and JPEG refer to the same image encoding. ImageFormat.Jpeg creates JPEG data, and the output commonly uses the .jpg or .jpeg extension.

3. How can I increase the resolution of an XHTML image?

Set the horizontal and vertical resolution in ImageSaveOptions before conversion. Higher resolution produces more pixels and usually increases the file size.

4. Can I convert XHTML to JPG online for free?

Yes. Use the free online XHTML to JPG converter for a manual conversion. Use Aspose.HTML for Java for automated workflows.

Related Java Conversions

Choose another XHTML output or a related JPG workflow.