How to Export pdf to OutputStream with wkhtmltopdf in java
Asked Answered
R

1

6

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?

Rabblerousing answered 29/2, 2012 at 5:33 Comment(0)
D
4

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;
Daliadalila answered 12/7, 2012 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.