Search code examples
javafileprocessprocessbuilderpdftotext

Running PdfToText as a Java process with encoding parameter


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?


Solution

  • Try this

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

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