Oscar Smith Oscar Smith - 1 year ago 93
Java Question

Print contents of pdf file to console

I am re-implementing a subset of pdftk (pdftk fails with newer versions of pdf) and one of it's features it the ability to output an interactive pdf file to the command line (for piping purposes). I currently am doing that with

File tmp=new File("tmp.pdf");
output= new String(Files.readAllBytes(Paths.get("tmp.pdf")), "UTF-8");

The problem is when I pipe this to out.pdf. and open it, only the form fields are in the new pdf field. My first thought would be that the second line would be the faulty one, but tmp.pdf is the full pdf file, suggesting that the problem is in the line where I am reading the pdf. Any suggestions?

I found a different way that mostly works using /dev/nul or CON (os dependent). This way is better as it doesn't create temp files, but on windows it doesn't pipe correctly. Any ways to make it pipe?

doc.save(new File("CON"));
doc.save(new File("/dev/stdout"));

Answer Source

As discussed in the comments - instead of saving to a temp file, you can save to System.out:


Although I've never tested whether System.out can be used for such a purpose and keep the content intact, so I'd recommend that you do some binary test to compare the original PDF and what you get out of the pipe.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download