使用Aspose.Words将word转换为pdf时,在windows系统使用时正常的,但在linux系统中出现docx可以正常转换,doc转换失败(Unknown file format)。
public static void wordToPdf(String wordPath,String pdfPath) {
FileOutputStream os = null;
try {
getLicense();
File file = new File(pdfPath);
os = new FileOutputStream(file);
Document doc = new Document(wordPath);
doc.save(os, SaveFormat.PDF);
os.close();
} catch (Exception e) {
logger.error("word转pdf失败!");
}
}
原因已找到,因为使用的电脑都带有加密功能,导致未加密的文件另存为doc格式文件时自动加密,从而导致外网测试失败
别用FileOutputStream,改用ByteArrayOutputStream避免文件路径的问题,然后它可以直接转byte数组,整个转换过程中不需要生成本地文件,最终byte数组你想怎么玩就怎么玩
可以试试其他API转换: Java 将 Word 转为 PDF