How to export pdf to OutputStream using 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 an html file to input and save the output as a pdf file on disk. But my html is not a file, it is an OutputStream, and I want to create a pdf file on the fly and do not want to save it to disk.

Is there a way to pass a parameter with an OutputStream and get the result as another OutputStream?

+6
source share
1 answer

If you pass "-" instead of "output.pdf", you should get the output as a stream. Then you can capture this stream, but do not forget to check it in advance and associate with stderr and stdout, as well as debug both outputs, since wkhtmltopdf can be a bit stubborn :)

In C #, forwarding is pretty straightforward, while stdin / stderr / stdout is streams. You should test all three as wkhtml output for both: one for pdf output and one for the user interface output that you see when you start from the command line. Sorry for the lack of Java or a working example, I have not really tried this, since my processes are queued and they are written to disk.

 Process _process = new Process(); // Other stuff here _process.StartInfo.RedirectStandardError = true; StreamReader sr = _process.StandardError; 
+2
source

Source: https://habr.com/ru/post/909602/


All Articles