How to convert html to pdf using javax.servlet.ServletOutputStream or java.io.OutputStream?

I have downloaded the evaluation version of Aspose.Html for java to see whether it’s suitable.

The online example only shows how to save a pdf on the file system, but i want to write the pdf to a javax.servlet.ServletOutputStream. Is it possible?

Here’s the code snippet:

HTMLDocument html = new HTMLDocument(is, "");
HtmlRenderer renderer = new HtmlRenderer();
renderer.render(new PdfDevice(new PdfRenderingOptions(), "newdoc.pdf"), html);

PdfDevice constructor only accepts:
A. file name
B. com.aspose.html.internal.ms.System.IO.Stream
C. com.aspose.html.io.ICreateStreamProvider

but it’s not clear how write the pdf to a java OutputStream or a ServletOutputStream using option B or C.

@huangfang

Thank you for contacting support.

We have logged a ticket with ID HTMLJAVA-139 in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

Hey Farhan. Just want to follow up on this: is java.io.OutputStream supported as a destination when rendering HTML to PDF?

@huangfang

Thank you for getting back to us.

We are afraid that logged ticket is pending for investigations at the moment, owing to previously logged tickets. We will update you as soon as some significant progress will be made. We appreciate your patience and comprehension in this regard.

@Farhan.Raza
Any updates after two weeks?

@huangfang

We would like to update you that the issue HTMLJAVA-139 has been resolved and the constructor PdfDevice(java.io.OutputStream stream) will be supported in the upcoming release. However, you can use a workaround approach for now, as in the lines of code below:

java.io.OutputStream fos = new FileOutputStream("newdoc.pdf");
OutputStreamAdapter outputStreamAdapter = new OutputStreamAdapter(fos);
renderer.render(new PdfDevice(new PdfRenderingOptions(), outputStreamAdapter), html);

Please download attached java class for your kind reference. OutputStreamAdapter.java

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

@Farhan.Raza the workaround works. thanks!

@Farhan.Raza, could you also provide us the workaround OutputStreamAdapter.java?
Kind regards!

@dvtdaten

Thank you for posting.

You may download the file from this public access link of Google Drive. Feel free to contact us if you need any further assistance.

Hi, i am working with Aspose.html v 23.8, but i can not find the constructor PdfDevice(java.io.OutputStream stream) ?

@huangfang

We have opened the following new ticket(s) in our internal issue tracking system to investigate this issue. We will look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

Issue ID(s): HTMLJAVA-1583