Render html to image stream


#1

Hi,

I used to render html to png in aspose.HTML 18.11 as follows:

java.io.InputStream content = ...;

// load the document to be rendered
html = new HTMLDocument( content, "" );

// Create output stream
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();

// render to image
HtmlRenderer renderer = new HtmlRenderer();
		
ImageRenderingOptions imageRenderingOptions = new ImageRenderingOptions( getAsposeHtmlImageRenderingFormat( previewFormat ) );

// Set resolution ...

ImageDevice imageDevice = new ImageDevice( imageRenderingOptions, imageStream );

renderer.render( imageDevice, html );

Since version 19.3 the following constructor is missing:

public ImageDevice(ImageRenderingOptions options, java.io.OutputStream outputStream)

It seems to be replaced with the following:

public ImageDevice(ImageRenderingOptions options, com.aspose.html.internal.ms.System.IO.Stream stream)

My questions are:

  1. Is there still a way to render html to png using OutputStream in version 19.3 / 19.5? I would like to avoid saving to disk first.

  2. What is the prefered way to render html to png? Searching the forum I stumbled upon this: HTML to Image Conversion Although Aspose.HTML for Java is auto-ported version of Aspose.HTML for .NET I don’t see how to use this approach in Java.

Regards,
Michae


#2

@mstrucken

Thank you for contacting support.

We have been able to notice the missing method and have logged a ticket with ID HTMLJAVA-255 in our issue management system for further investigations. We will let you know as soon as any significant update will be available in this regard.

Moreover, we are in the process of updating our examples project and new ported examples will be available in Java in Documentation as well. For now, you may convert HTML to PNG using below code snippet:

HTMLDocument  document = new HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", "c:\\work\\");
try
{
    ImageDevice device = new ImageDevice(StringExtensions.concat(dataDir,  "document_out.png"));
    try
    {
        document.renderTo(device);
    }
    finally { if (device != null) device.dispose(); }
}
finally { if (document != null) document.dispose(); }

#3

@mstrucken

We are glad to inform you that HTMLJAVA-255 has been resolved in Aspose.HTML for Java 19.6. Please feel free to contact us if you need any further assistance.