将 pdf 文件的内容打印到控制台

Print contents of pdf file to console

我正在重新实现 pdftk 的一个子集(pdftk 在更新版本的 pdf 上失败),其中一个功能是能够将交互式 pdf 文件输出到命令行(用于管道 purpos es)。我目前正在使用

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);

问题是当我将其通过管道传输到 out.pdf 时。然后打开它,只有表单字段在新的 pdf 字段中。我的第一个想法是第二行是错误的,但是 tmp.pdf 是完整的 pdf 文件,这表明问题出在我正在阅读 pdf 的那一行。有什么建议吗?

编辑: 我发现 mostly 使用 /dev/nul 或 CON(os 依赖)的不同方式。这种方式更好,因为它不会创建临时文件,但在 windows 上它无法正确传输。有什么办法让它变成管道吗?

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);

如评论中所述 - 您可以保存到 System.out:

而不是保存到临时文件
doc.save(System.out);

虽然我从来没有测试过 System.out 是否可以用于这样的目的并保持内容完整,所以我建议你做一些二进制测试来比较原始 PDF 和你得到的从管道中出来。