使用Ghostscript + Ghost4j 压缩PDF时,文件输入和输出路径都包含中文字符,控制台说无法打开路径。
public static void compressPdf(String filePath,String destPath, String compression){
Ghostscript gs = Ghostscript.getInstance();
String[] gsArgs = new String[8];
//安静执行,执行过程尽肯能少输出日志信息
gsArgs[0] = "-dQUIET";
//每一页之间转换没有停顿
gsArgs[1] = "-dNOPAUSE";
//执行到最后一页退出
gsArgs[2] = "-dBATCH";
//通过命令行运行
gsArgs[3] = "-dNOSAFER";
//输出PDF格式
gsArgs[4] = "-sDEVICE=pdfwrite";
gsArgs[5] = "-dPDFSETTINGS=" + compression;
gsArgs[6] = "-sOutputFile=" + destPath;
gsArgs[7] = filePath;
try {
gs.initialize(gsArgs);
gs.exit();
} catch (GhostscriptException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
compressPdf("E:\\桌面\\toPdf\\pdf\\a.pdf","E:\\桌面\\toPdf\\pdf\\ab.pdf",“/ebook”);
}
下面是控制台信息:
GPL Ghostscript 9.52: **** Could not open the file E:\妗岄潰\toPdf\pdf\ab.pdf .
**** Unable to open the initial device, quitting.
org.ghost4j.GhostscriptException: Cannot initialize Ghostscript interpreter. Error code is -100
at org.ghost4j.Ghostscript.initialize(Ghostscript.java:365)
at com.rlsd.wei.utils.CompressUtil.compressPdf(CompressUtil.java:65)
at com.rlsd.wei.test.Test.main(Test.java:42)