可以将Word文档doc(x)转换为pdf的库
我正在开发一个软件,应该能够自动将Word文档转换为pdf。有没有最好支持Java的库可以实现这一点?它可以是免费的,也可以是商业的,这并不重要。
谢谢
I'm working on a software that should be able to automatically convert Word documents to pdfs. Is there any library that supports Java preferably that can achieve this? It can be either free or commercial, it doesn't matter.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
docx2pdf
是一个命令行工具和 python 库,可以在 macOS 和 Windows 上实现此目的。在 Java 中,您应该能够使用命令行调用该工具。在内部,这个 python 库从命令行调用 AppleScript,以使其在 macOS 上运行。https://github.com/AlJohri/docx2pdf/
这种方法的好处是它使用Word 自己的 API 可创建像素完美转换。
免责声明:我编写了这个工具。
docx2pdf
is a command line tool and python library that can achieve this on macOS and Windows. From Java, you should be able to call the tool using the command line. Internally, this python library calls an AppleScript from the command line in order to get it to work on macOS.https://github.com/AlJohri/docx2pdf/
The benefit to this approach is that it uses Word's own APIs to create a pixel perfect conversion.
Disclaimer: I wrote this tool.
我没用过,不过你可以试试JODConverter
I haven't used it, but you could try JODConverter
StackOverflow 上的大多数人推荐 Apache POI,它可以将大多数 Microsoft Office 格式转换为 PDF,反之亦然。它是开源的。
Most people on StackOverflow recommend Apache POI which can convert most Microsoft Office formats into PDF and vice-versa. It's open-source.
@BoffinbraiN,POI 并不能帮助您将 Office 文档转换为 PDF。
Apache FOP 可用于执行此操作,但如果要使用 FOP,首先需要将 Office 文档转换为 XSL-FO。
在这里,docx4j 可以提供帮助,因为它生成 XSL-FO,并使用 FOP 1.0 创建 PDF。
假设 OutputStream 操作系统和已加载的 WordprocessingMLPackage wordMLPackage:
请进一步查看 docx4j 示例 CreatePdf.java。
@BoffinbraiN, POI doesn't do much to help you to convert Office documents to PDF.
Apache FOP can be used to do this, but if you want to use FOP, first you need to convert the Office document to XSL-FO.
Here, docx4j can help, as it generates the XSL-FO, and uses FOP 1.0 to create the PDF.
Assuming OutputStream os and a loaded WordprocessingMLPackage wordMLPackage:
See further the docx4j sample CreatePdf.java.
有几个可用的开源解决方案,但没有一个能够完全保留源文档的格式。
看看 Muhimbi PDF 转换器服务。它作为可扩展且强大的 Windows 服务安装在您的环境中,并且专门设计用于在基于服务器的应用程序(例如 Java 和 ASP.NET)中使用。
它带有一个基于友好网络服务的界面 允许在大多数现代环境(例如 Java 和 .NET)中使用它。它支持所有常见以及一些不常见的文件格式。还包括水印和PDF安全性。
免责声明,我曾参与过该产品的开发,因此通常的免责声明适用。话虽如此,效果很好。
There are a couple of open source solutions available, but none keep the formatting of the source document completely.
Give the Muhimbi PDF Converter Services a look. It installs in your environment as a scalable and robust Windows Service and has specifically been designed for use from server based applications such as Java and ASP.NET.
It comes with a friendly web services based interface that allows it to be used from most modern environments such as Java and .NET. It supports all common as well as some not so common file formats. Watermarking and PDF Security is included as well.
Disclaimer, I have worked on this product so the usual disclaimers apply. Having said that, it works great.
您可以将 apache-poi 与 opensagres-xdocreport。
pom.xml
代码示例 - Java
作为 Azure 函数的完整代码
我会将完整的工作代码放在 github 上 https://github.com/donniexyz/ms-donniexyz-poi-azure-function
You can use apache-poi in tandem with opensagres-xdocreport.
pom.xml
Code example - Java
Complete Code as Azure Function
I will put a complete working code on github https://github.com/donniexyz/ms-donniexyz-poi-azure-function