Aspose.HTML for Java FAQ

Aspose.HTML for Java is an on-premise API for loading, creating, editing, saving, and converting HTML and related web formats in Java applications. Use HTMLDocument for HTML DOM operations and the Converter class for supported format conversions.

This FAQ provides direct answers to common questions about Aspose.HTML for Java. Follow the links in each answer for complete Java examples, API options, and troubleshooting guidance.

Product, Installation, and Licensing

What is Aspose.HTML for Java?

Aspose.HTML for Java is a Java library for working with HTML documents without automating a web browser. It provides APIs for HTML parsing, DOM editing, CSS and JavaScript processing, data extraction, web accessibility checks, document saving, and conversion to formats such as PDF, DOCX, XPS, and raster images. See the Features List for an overview.

How do I install Aspose.HTML for Java?

Add the Aspose repository and Aspose.HTML dependency to a Maven or Gradle project, or download the JAR package manually. The Installation guide contains the current repository settings and dependency examples.

Can I use Aspose.HTML for Java for free?

Aspose.HTML for Java is a commercial library, but you can install and evaluate it without a license. Evaluation output has restrictions, including watermarks and a conversion page limit. Request a free temporary license to test the full API without evaluation restrictions. See Licensing for the current limitations and license setup.

Which Java versions and operating systems are supported?

Supported Java runtimes and operating systems can change as the product evolves. Check System Requirements for the maintained compatibility information before choosing a runtime or deployment image.

Where can I find complete Java examples?

Documentation pages contain focused code snippets, while the Aspose.HTML for Java GitHub repository provides complete examples and supporting data files. The How to Run the Examples article explains how to set up and run them.

HTML Conversion and Supported Formats

How do I convert HTML to PDF in Java?

Use Converter.convertHTML() with PdfSaveOptions. For a local HTML file:

  1. Specify the path to the source HTML file.
  2. Create PdfSaveOptions for the PDF output.
  3. Call Converter.convertHTML() with the input path, options, and output path.
1// Convert HTML to PDF
2
3Converter.convertHTML("document.html", new PdfSaveOptions(), "output.pdf");

See Convert HTML to PDF in Java for HTML strings, documents, custom page settings, and complete examples.

What input and output formats does Aspose.HTML for Java support?

Supported source formats include HTML, XHTML, MHTML, EPUB, SVG, and Markdown. Supported output workflows include HTML-related formats, PDF, XPS, DOCX, and raster images such as PNG, JPEG, TIFF, BMP, and GIF. Not every input format supports every output combination. Use the Supported File Formats table and the conversion guides to choose the correct workflow.

Can I convert a webpage URL to PDF?

Yes. Load the webpage URL with HTMLDocument, create PdfSaveOptions, and pass the document to Converter.convertHTML(). The application must be able to access the page and its linked resources while the document is loading and rendering. See Create and Load HTML Documents and Convert HTML to PDF.

Can I convert an HTML string without creating a temporary file?

Yes. Use a Converter.convertHTML() overload that accepts an HTML string, a base URI, save options, and an output path. Supply a valid base URI when the markup contains relative paths to images, stylesheets, scripts, fonts, or links.

Can I convert HTML to PNG or JPG in Java?

Yes. Use Converter.convertHTML() with ImageSaveOptions and select the required output format. Image conversion supports PNG, JPEG, TIFF, BMP, and GIF output. See Convert HTML to Image in Java for file, document, and custom rendering examples.

Can Aspose.HTML for Java convert PDF to HTML?

No. PDF is an output format in Aspose.HTML for Java, not a supported input format. Use Aspose.PDF for Java when a workflow must load or convert an existing PDF document.

How do I customize PDF, image, DOCX, or XPS output?

Create the save-options class for the target format and configure the settings supported by that class. Depending on the output, options can include page size, margins, resolution, background color, image format, or document-specific settings. Start with Fine-Tuning Converters and then open the guide for the required converter.

Can I load EPUB, MHTML, or SVG with HTMLDocument?

HTMLDocument is intended for HTML and XHTML documents. Use SVGDocument for SVG DOM operations. For EPUB and MHTML conversion, use the specialized Converter.convertEPUB() and Converter.convertMHTML() workflows described in the EPUB Converter and MHTML Converter guides.

Why are CSS, images, or fonts missing from converted output?

Relative resource URLs are resolved against the document URL or the base URI supplied for in-memory HTML. Use a correct base URI, confirm that local or remote resources are accessible, and make the required fonts available to the rendering environment. For configuration examples, see Create and Load HTML Documents and Environment Configuration.

Can I keep conversion output in memory instead of writing directly to disk?

Yes. Pass a list of output streams to a supported converter overload and manage the resulting streams in application memory. Image conversion can create more than one stream when the result has multiple output pages. See HTML Output Streams in Java for the MemoryStreamProvider helper and a complete example.

Create, Edit, Save, and Extract HTML

Can I create and edit HTML documents in Java?

Yes. Create an empty document or load HTML from a file, URL, string, or stream. You can then use DOM APIs to add or remove nodes, change elements and attributes, update text, and modify CSS. Start with Create and Load HTML Documents and Edit HTML Documents.

How do I save an HTML document with its linked resources?

Call HTMLDocument.save() and use HTMLSaveOptions when you need to control how resources are handled. The save options can determine which linked resources are written and how their URLs are processed. See Save HTML Documents in Java.

Can Aspose.HTML save linked HTML pages together?

Yes, when saving a document that links to other HTML pages. Configure ResourceHandlingOptions.setMaxHandlingDepth() to control how deeply the save operation follows linked pages. This is a linked-resource traversal setting, not a general batch API for unrelated HTML files. See Save HTML Documents for depth values and resource-handling examples.

Can I extract text, links, images, or SVG from a website?

Yes. Load the document, select the required DOM nodes, read their content or attributes, resolve relative URLs, and save or process the results. See Data Extraction in Java, Extract Images from a Website, and Extract SVG from a Website.

Does Aspose.HTML for Java support CSS selectors and XPath?

Yes. Use CSS selectors to find elements with querySelector() or querySelectorAll(), and use XPath expressions when a query requires XPath navigation. The HTML DOM Navigation guide includes examples of CSS selectors, XPath, TreeWalker, and NodeFilter.

JavaScript, Network Resources, and Styles

Can I disable JavaScript for an HTML document?

Yes. Create a Configuration, add the Sandbox.Scripts flag, and load the document with that configuration. Sandboxing can also restrict other document capabilities. See Environment Configuration for the Java example.

Can I limit JavaScript execution time?

Yes. Use the runtime service from Configuration and set a JavaScript timeout before loading or converting the document. This prevents a long-running script from blocking processing indefinitely. See JavaScript Runtime Service.

How can I inspect or control resources loaded with an HTML document?

Use the network service and message-handler chain to inspect requests and responses. Custom handlers can log failed requests or modify request and response processing before the next handler runs. See Network Service.

How do I override CSS without editing the source HTML?

Configure a user stylesheet through the user agent service and pass that Configuration to the document. This lets application CSS participate in rendering without changing the source file. See User Style Sheet.

Accessibility, Deployment, and Troubleshooting

Can Aspose.HTML for Java check web accessibility?

Yes. The Web Accessibility API validates HTML against supported WCAG 2.0 rules and returns results that identify failed criteria, techniques, and affected elements. Start with Web Accessibility in Java and Accessibility Validator.

Can I use Aspose.HTML for Java in Docker or a headless environment?

Yes. The application can run in Docker and headless environments when the container includes the required Java runtime, library dependencies, and fonts needed by the documents. See Using Aspose.HTML for Java in Docker and Using Headless Mode.

Why do I see incorrect characters or black squares in the output?

Check both character encoding and font availability. If the source uses a non-UTF-8 encoding, declare or configure the correct character set. If characters parse correctly but render as boxes, the selected font may not contain the required glyphs or may be unavailable in the environment. See Environment Configuration for charset and custom-font settings.

How do I add page numbers, headers, or footers to rendered output?

Use paged-media CSS and the supported Aspose.HTML CSS extensions to place generated content in page margin areas. See CSS Extensions in Java for page-number and margin-box examples.

Why can rendering look different on another machine?

Rendering can change when machines use different fonts, runtime environments, locale settings, resource access, or Aspose.HTML versions. For reproducible output, keep these inputs and settings consistent and make external resources available during processing.

Related Aspose.HTML for Java Resources