Oscar Smith Oscar Smith - 5 months ago 16
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

if("".equals(output)){
File tmp=new File("tmp.pdf");
doc.save(tmp);
output= new String(Files.readAllBytes(Paths.get("tmp.pdf")), "UTF-8");
tmp.delete();
}
System.out.println(output);


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?

Edit:
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?

if("".equals(output)){
if("W".equals(System.getProperty("os.name").substring(0,1)))
doc.save(new File("CON"));
else
doc.save(new File("/dev/stdout"));
System.out.println(output);

Answer

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

doc.save(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.