可以将Word文档doc(x)转换为pdf的库

发布于 2024-11-05 11:31:12 字数 91 浏览 5 评论 0原文

我正在开发一个软件,应该能够自动将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

把人绕傻吧 2024-11-12 11:31:12

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.

已下线请稍等 2024-11-12 11:31:12

我没用过,不过你可以试试JODConverter

I haven't used it, but you could try JODConverter

等风来 2024-11-12 11:31:12

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.

追风人 2024-11-12 11:31:12

@BoffinbraiN,POI 并不能帮助您将 Office 文档转换为 PDF。

Apache FOP 可用于执行此操作,但如果要使用 FOP,首先需要将 Office 文档转换为 XSL-FO。

在这里,docx4j 可以提供帮助,因为它生成 XSL-FO,并使用 FOP 1.0 创建 PDF。

假设 OutputStream 操作系统和已加载的 WordprocessingMLPackage wordMLPackage:

PdfConversion c = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
c.output(os);

请进一步查看 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:

PdfConversion c = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
c.output(os);

See further the docx4j sample CreatePdf.java.

一片旧的回忆 2024-11-12 11:31:12

有几个可用的开源解决方案,但没有一个能够完全保留源文档的格式。

看看 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.

泪意 2024-11-12 11:31:12

您可以将 apache-poiopensagres-xdocreport

pom.xml

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
    <version>2.0.4</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document</artifactId>
    <version>2.0.4</version>
</dependency>

代码示例 - Java

try (XWPFDocument mailMergedXwpfDocument = PoiMailMerge.perform(templatePath, bodyMap);
 ByteArrayOutputStream out = new ByteArrayOutputStream()) {

if (null == toFormat || "".equals(toFormat)) {
    mailMergedXwpfDocument.write(out);
} else if ("pdf".equalsIgnoreCase(toFormat)){
    try {
        PdfOptions pdfOptions = PdfOptions.create();
        // HERE convert docx as xwpf document into PDF
        PdfConverter.getInstance().convert(mailMergedXwpfDocument, out, pdfOptions);
        resultFileName = FilenameUtils.removeExtension(filename) + ".pdf";
    } catch (IOException e) {
        // ...
    } catch (XWPFConverterException e) {
        // ...
    }
}
// here is the PDF as byte array
byte[] bytes = out.toByteArray();

作为 Azure 函数的完整代码

我会将完整的工作代码放在 github 上 https://github.com/donniexyz/ms-donniexyz-poi-azure-function

You can use apache-poi in tandem with opensagres-xdocreport.

pom.xml

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
    <version>2.0.4</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document</artifactId>
    <version>2.0.4</version>
</dependency>

Code example - Java

try (XWPFDocument mailMergedXwpfDocument = PoiMailMerge.perform(templatePath, bodyMap);
 ByteArrayOutputStream out = new ByteArrayOutputStream()) {

if (null == toFormat || "".equals(toFormat)) {
    mailMergedXwpfDocument.write(out);
} else if ("pdf".equalsIgnoreCase(toFormat)){
    try {
        PdfOptions pdfOptions = PdfOptions.create();
        // HERE convert docx as xwpf document into PDF
        PdfConverter.getInstance().convert(mailMergedXwpfDocument, out, pdfOptions);
        resultFileName = FilenameUtils.removeExtension(filename) + ".pdf";
    } catch (IOException e) {
        // ...
    } catch (XWPFConverterException e) {
        // ...
    }
}
// here is the PDF as byte array
byte[] bytes = out.toByteArray();

Complete Code as Azure Function

I will put a complete working code on github https://github.com/donniexyz/ms-donniexyz-poi-azure-function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文