Domanda

I am running PdfToText via a Java process:

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

Which runs without a problem.

However, when I add an encoding parameter as specified here:

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

Then the process just hangs - i.e., the test I am running just runs and runs as if it were stuck in a loop.

The encoding definitely contains a value and when the generated command is copied and pasted into a command terminal then the pdftotext runs without a problem.

Can anybody point out where I am going wrong with this?

È stato utile?

Soluzione

Try this

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

There shouldn't be spaces and paths are system dependent.

Altri suggerimenti

Here is the code that is working for me:

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

Could you try this on your input and see how it goes? The problem may be that the process may write to stderr or stdout and you are not reading from it and it blocks.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top