如何用Java开源跨平台库实现Word转PDF且跨Windows/UNIX格式一致
问题:Windows创建的Word转PDF跨UNIX/Windows环境格式一致性问题
我曾使用aspose-words与document4j实现Word转PDF,但它们依赖渲染引擎生成布局,导致在UNIX与Windows环境下生成的PDF在字体、字号及格式上存在差异;尝试过Apache POI,但转换效果不佳。
已尝试的代码实现
使用document4j的实现
//using document4j InputStream docxInputStream = new FileInputStream("input.docx"); OutputStream outputStream = new FileOutputStream("output.pdf"); IConverter converter = LocalConverter.builder().build(); outputStream.close(); converter.convert(docxInputStream).as(DocumentType.DOCX) .to(outputStream).as(DocumentType.PDF).execute(); converter.shutdown()
使用Aspose.Words的实现
//using aspose word Document doc = new Document(new FileInputStream(filePath)); String outputFilePath = "output.pdf"; doc.save(outputFilePath, SaveFormat.PDF);
使用Apache POI的实现
//using apache poi InputStream doc = new FileInputStream(new File(docPath)); XWPFDocument document = new XWPFDocument(doc); PdfOptions options = PdfOptions.create(); OutputStream out = new FileOutputStream(new File(pdfPath)); PdfConverter.getInstance().convert(document, out, options);
需求
- 将Windows环境创建的Word文档(完整扩展名)转成PDF,代码运行在UNIX服务器上
- 借助外部开源库的内置方法实现
- 确保Windows与UNIX环境下生成的PDF字体、字号及页数完全一致
内容的提问来源于stack exchange,提问作者lemon chow
相关产品推荐
相关产品推荐

