HTML Output Streams in Java

HTML conversion results do not always need to be written directly to a file. An application may first keep the result in memory, inspect or transfer it, and then decide where to store it. The examples on this page show this workflow for HTML-to-JPEG conversion.

To keep HTML conversion output in memory, create a list of InputStream objects and pass it to Converter.convertHTML(). After conversion, read the required stream or copy it to a file. Image conversion can return multiple streams when the result contains multiple output pages.

Implement the MemoryStreamProvider Helper

MemoryStreamProvider is a custom helper class used by these examples; it is not an Aspose.HTML API class. It owns the java.util.List<java.io.InputStream> passed to the converter and implements java.io.Closeable so that all collected streams can be closed together.

Although the conversion result is output from Aspose.HTML, each collected item is exposed as an InputStream so that Java code can read the generated data. The list may contain more than one stream for an image conversion that produces several output images.

 1// Implement a memory-based stream provider for handling InputStreams during document rendering in Aspose.HTML for Java
 2
 3// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-Java
 4
 5public class MemoryStreamProvider implements java.io.Closeable {
 6    // List of InputStream objects created during the document rendering
 7    public java.util.List<java.io.InputStream> lStream = new java.util.ArrayList<java.io.InputStream>();
 8
 9    @Override
10    public void close() throws java.io.IOException {
11        for (java.io.InputStream stream : lStream) {
12            stream.close();
13        }
14    }
15}

Convert HTML to JPEG in Memory

The following Java example converts inline HTML to JPEG, receives the conversion result through the list owned by MemoryStreamProvider, and copies the first stream to a local file.

  1. Create a MemoryStreamProvider instance to own the list of result streams.
  2. Initialize an HTMLDocument from the HTML source.
  3. Create ImageSaveOptions for JPEG output.
  4. Pass the document, save options, and streamProvider.lStream to Converter.convertHTML().
  5. Retrieve the first InputStream, reset its position, and copy its data to the required output file.
 1// Convert HTML to JPEG in memory using Aspose.HTML for Java and save the result to a file
 2
 3// Create an instance of MemoryStreamProvider
 4MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 5
 6// Initialize an HTMLDocument instance
 7HTMLDocument document = new HTMLDocument("<span>Hello, World!!</span>", ".");
 8
 9// Convert HTML to JPG using the MemoryStreamProvider
10Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider.lStream);
11
12// Get access to the memory stream that contains the result data
13java.io.InputStream memory = streamProvider.lStream.get(0);
14memory.reset();
15
16// Flush the result data to the output file
17Path outputFile = new File("output.jpg").toPath();
18Files.copy(memory, outputFile, StandardCopyOption.REPLACE_EXISTING);

The example saves only lStream.get(0). If conversion produces several images, iterate through the complete lStream list and assign a separate output name to each stream.

When to Use In-Memory Conversion Output

In-memory output is useful when an application needs to return a generated image in an HTTP response, upload it to remote or cloud storage, or process it before creating a file. It also gives the application control over the names and destinations of multiple image results.

Close every collected stream after it is no longer needed. Because the helper implements java.io.Closeable, calling its close() method closes all streams stored in the list.

Related Aspose.HTML Articles

You can download the complete examples and data files from GitHub.