【发布时间】:2015-04-10 08:04:44
【问题描述】:
我正在尝试使用 Java 将 pdf 转换为 txt。我尝试过 Apache PDFBox,但由于某些奇怪的原因,它并没有转换整个文档。出于这个原因,我决定通过执行 Runtime.getRuntime().exec() 调用来使用 pdftotext。问题是,虽然我的终端 pdftotext 完美运行,但 exec() 调用给了我错误代码 1(有时甚至是 99)。 这是电话:
pdftotext "/home/www-data/CANEFS_TEST/Hello/ciao.pdf" "/tmp/ciao.pdf.txt"
这是代码
private static File callPDF2Text(File input,File output){
assert input.exists();
assert Utils.getExtension(input).equalsIgnoreCase("pdf");
assert Utils.getExtension(output).equalsIgnoreCase("txt") : output.getAbsoluteFile().toString();
Process p=null;
try {
System.out.println(String.format(
PDF2TXT_COMMAND,
input.getAbsolutePath(),
output.getAbsolutePath()));
p=Runtime.getRuntime().exec(String.format(
PDF2TXT_COMMAND,
input.getAbsolutePath(),
output.getAbsolutePath()));
p.waitFor();
if (p.exitValue()!=0){
throw new RuntimeException("exit value for pdftotext is "+p.exitValue());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return output;
}
这里是 PDF2TXT_COMMAND 字符串定义:
public static final String PDFTXT_COMMAND="pdftotext \"%s\" \"%s\"";
我知道这些错误通常是由权限设置引起的。所以,这里是 Hello 文件夹上 ls -l 命令的输出:
ls -l /home/www-data/CANEFS_TEST/Hello/
total 136
-rwxrwxr-- 1 www-data www-data 136041 mar 27 16:31 ciao.pdf
另外,请注意创建进程的用户是 koldar,它位于组 www-data 本身中。 感谢您的时间和耐心!
【问题讨论】:
标签: java linux runtime.exec pdftotext