【问题标题】:Running PdfToText as a Java process with encoding parameter使用编码参数将 PdfToText 作为 Java 进程运行
【发布时间】:2012-08-18 15:32:00
【问题描述】:

我正在通过 Java 进程运行 PdfToText:

File pdf = new File( "/path/to/test.pdf" );
File output = new File( "/path/to/output.txt" );

String[] cmd = { "pdftotext",
                 pdf.getPath(), 
                 output.getPath()
               };

ProcessBuilder pb = new ProcessBuilder( cmd );
Process pr = pb.start() ;
int exit = pr.waitFor();

运行没有问题。

但是,当我添加指定的编码参数here:

String[] cmd = { "pdftotext", 
                 "-enc " + encoding, 
                 pdf.getPath(), 
                 output.getPath()
               };

然后进程就挂起 - 即,我正在运行的测试只是运行并运行,就好像它被卡在一个循环中一样。

编码肯定包含一个值,当生成的命令被复制并粘贴到命令终端时,pdftotext 运行没有问题。

谁能指出我哪里出了问题?

【问题讨论】:

    标签: java file process processbuilder pdftotext


    【解决方案1】:

    试试这个

    String[] cmd = { "pdftotext", 
      "-enc", encoding, 
      pdf.getAbsolutePath(), 
      output.getAbsolutePath()
    };
    

    不应有空格,路径取决于系统。

    【讨论】:

      【解决方案2】:

      这是对我有用的代码:

       public class ToDelete {
      public static void main(String[] args) throws Exception {
          File file = new File("/Users/eugenrabii/Desktop/MyPDF.pdf");
          File output = new File("/Users/eugenrabii/Desktop/Output.txt");
          List<String> commands = new ArrayList<String>();
      
          commands.add("/opt/local/bin/pdftotext");
          commands.add("-enc");
          commands.add("UTF-8");
          commands.add(file.getPath());
          commands.add(output.getPath());
      
          ProcessBuilder processBuilder = new ProcessBuilder(commands);
          Process process = processBuilder.start();
      
          int result = process.waitFor();
          if(result!=0){
              printResult(process.getErrorStream());
          } else {
              printResult(process.getInputStream());
          }
      }
      
      private static void printResult(InputStream inputStream) throws IOException {
          byte [] byte_ = new byte[inputStream.available()];
          inputStream.read(byte_);
          System.out.println(new String(byte_));
      }
      }
      

      你能在你的输入上试试这个,看看效果如何?问题可能是该进程可能会写入 stderr 或 stdout 并且您没有从中读取并且它会阻塞。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多