JAVA实现WORD转PDF

x33g5p2x  于2021-08-23 转载在 Java  
字(2.9k)|赞(0)|评价(0)|浏览(637)

1、使用aspose-words,这是个收费的,需要购买获取license.xml中的秘钥,本文只介绍使用方法,请支持正版,apose官网。
2、笔者采坑记录:jacob也可以实现word转PDF且Jacob是不收费的,但是Jacob只支持windows,不支持linux。
3、参考博客:
Java利用aspose-words将word文档转换成pdf(破解 无水印)
使用com.aspose.words将word模板转为PDF乱码解决方案(window下正常)

一、jar包依赖

1、jar包获取地址
链接: jar包下载 提取码: mp59
2、jar包添加到本地maven仓库
jar包下载之后进入到命令行执行:

mvn install:install-file -Dfile=aspose-words-15.8.0-jdk16.jar -DgroupId=com.jcraft -DartifactId=aspose-words -Dversion=15.8.0-jdk16 -Dpackaging=jar

执行完毕之后可以看到本地maven仓库中生成了com.jcraft.aspose-words路径的文件
3、引用依赖

    <!-- word2pdf -->
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>aspose-words</artifactId>
        <version>15.8.0-jdk16</version>
    </dependency>

二、license.xml

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>---</SerialNumber>
    </Data>
    <Signature>---</Signature>
</License>

三、Java代码

/**
 * word 转 PDF
 *
 * @param wordPath word路径
 * @param pdfPath  pdf路径
 */
private void word2pdf(String wordPath, String pdfPath) {
    // 验证License 若不验证则转化出的pdf文档会有水印产生
    if (WordUtil.getLicense()) {
        try {
            long old = System.currentTimeMillis();
            //新建一个空白pdf文档
            File file = new File(pdfPath);
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(wordPath);
            //设置一个字体目录(必须设置,否则生成的pdf乱码)下面这行代码不加的话在windows系统下生成的pdf不存在乱码问题,但是在linux系统下会乱码,linux下乱码解决方案请看后面的解决方案
            //FontSettings.setFontsFolder("/usr/share/fonts/chinese", false);
            doc.save(os, SaveFormat.PDF);
            os.close();
            long now = System.currentTimeMillis();
            logger.info("word2pdf共耗时:{}秒", ((now - old) / 1000.0));
        } catch (Exception e) {
            logger.error("word2pdf异常", e);
            e.printStackTrace();
        }
    }
}
/**
 *  WordUtil.getLicense()
 * 判断是否有授权文件 如果没有则会认为是试用版,转换的文件会有水印
 */
public static boolean getLicense() {
    boolean result = false;
    try {
        InputStream is = WordUtil.class.getClassLoader().getResourceAsStream("license.xml");
        License aposeLic = new License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

四、Linux乱码解决(pdf中文被小方框代替了)

参考:使用com.aspose.words将word模板转为PDF乱码解决方案(window下正常)

1、问题详情

使用com.aspose.words将word模板转为PDF文件时,在开发平台window下转换没有问题,中文也不会出现乱码。但是将服务部署在正式服务器(Linux)上,转换出来的PDF中文就出现了乱码。

2、问题分析

在window下没有问题但是在linux下有问题,就说明不是代码或者输入输出流编码的问题,根本原因是两个平台环境的问题。出现乱码说明linux环境中没有相应的字体以供使用,所以就会导致乱码的出现。将转换无问题的windos主机中的字体拷贝到linux平台下进行安装,重启服务器后转换就不会出现乱码了。

3、解决方案

Windows下字体库的位置为C:\Windows\fonts
linux的字体库是 /usr/share/fonts
步骤:
a、将windows下的字体上传到linux上:/usr/share/fonts/chinese
b、执行以下命令

cd /usr/share/fonts/chinese/
chmod 755 /usr/share/fonts/chinese/*
yum install mkfontscale
mkfontscale
mkfontdir
fc-cache

c、将上面代码中//FontSettings.setFontsFolder("/usr/share/fonts/chinese", false);的注释放开

原文链接:https://blog.csdn.net/weixin_44605704/article/details/102572130

相关文章