word转pdf

206 阅读1分钟

1、导入依赖

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>15.8.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/aspose-words-21.11-jdk17.jar</systemPath>
</dependency>

2、resources下新建License.xml

<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>
        sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
    </Signature>
</License>

3、在项目下新建lib导入依赖包

image.png

(链接: pan.baidu.com/s/1WAKFScq7… 提取码: yqji 复制这段内容后打开百度网盘手机App,操作更方便哦)

4、工具类

public static void wordToPdf(InputStream inputStream, HttpServletResponse httpResponse) throws Exception {
    try (InputStream is = TableUtils.class.getClassLoader().getResourceAsStream("License.xml")) {
        License license = new License();
        license.setLicense(is);
        OsInfo osInfo = SystemUtil.getOsInfo();
        // linux设置字体
        if (osInfo.isLinux()){
            FontSettings defaultInstance = FontSettings.getDefaultInstance();
            defaultInstance.setFontsFolder(localFontsPath, true);
        }
    }
    com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
    doc.save(httpResponse.getOutputStream(), SaveFormat.PDF);
    inputStream.close();
}