Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Export pdf to OutputStream with wkhtmltopdf in java

I use wkhtmltopdf in my java project with ProccessBuilder and Process:

ProcessBuilder pb = new ProcessBuilder("wkhtmltopdf.exe", "input.html", "output.pdf"); Process process = pb.start();

This solution uses a html file as input and save output as pdf file on disk. But my html isn't a file, it's an OutputStream and I want to create pdf file on fly and don't want save it on disk.

is there a way for passing parameter with OutputStream and getting result as another OutputStream?

like image 722
parabol Avatar asked Feb 29 '12 05:02

parabol


1 Answers

If you pass "-" instead of "output.pdf" you should get the output as stream. Then you might be able to grab that stream, but make sure to test beforehand and bind into stderr and stdout as well and debug both their output as wkhtmltopdf can be a bit stubborn :)

In C# the forwarding is fairly easy and stdin/stderr/stdout are streams. You have to test with all three as wkhtml outputs to both, one for the pdf output and one for user interface output that you see when launching from command line. Sorry for the lack of Java or a working example, I haven't actually tried this as my processes are queued and they write to disk.

Process _process = new Process();
// Other stuff here
_process.StartInfo.RedirectStandardError = true;
StreamReader sr = _process.StandardError;
like image 176
Joel Peltonen Avatar answered Oct 11 '22 16:10

Joel Peltonen