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


#1

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.


#2

@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.


#3

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


#4

@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.


#5

@Farhan.Raza
Any updates after two weeks?


#6

@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.


#7

@Farhan.Raza the workaround works. thanks!