aspose win/linux WORD转PDF(及其解决乱码方式)

简介: aspose win/linux WORD转PDF(及其解决乱码方式)

之前自己用的docm4j 本地进行转换是ok 在服务器中就异常了; 后来在网上查询之后 do4j无法支持liunx系统;

1.工具类

package com.aostar.ida.framework.util.excel;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
public class WordPdfUtil {
    /**
     * The constant LOG.
     *
     */
  private final static Logger LOGGER = Logger.getLogger(ExcelPdToWord.class);
    /**
     * 获取license
     *
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try {
            // 凭证
            String licenseStr =
                    "<License>\n" +
                            "  <Data>\n" +
                            "    <Products>\n" +
                            "      <Product>Aspose.Total for Java</Product>\n" +
                            "      <Product>Aspose.Words for Java</Product>\n" +
                            "    </Products>\n" +
                            "    <EditionType>Enterprise</EditionType>\n" +
                            "    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                            "    <LicenseExpiry>20991231</LicenseExpiry>\n" +
                            "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                            "  </Data>\n" +
                            "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
                            "</License>";
            InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
            License asposeLic = new License();
            asposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
          LOGGER.error("error:", e);
        }
        return result;
    }
    /**
     * Word 2 pdf.
     *  windos 测试
     * @param pdfFilePath   the pdf file path
     */
    public static void word2Pdf(String pdfFilePath,String wordFilePath) {
        FileOutputStream fileOS = null;
        // 验证License
        if (!getLicense()) {
          LOGGER.error("验证License失败!");
            return;
        }
        File inputWord = new File(wordFilePath);
        try {
            Document doc = new Document(new FileInputStream(inputWord));
            fileOS = new FileOutputStream(new File(pdfFilePath));
            // 保存转换的pdf文件
            doc.save(fileOS, SaveFormat.PDF);
        } catch (Exception e) {
          LOGGER.error("error:", e);
        } finally {
            try {
                if(fileOS != null){
                    fileOS.close();
                }
            } catch (IOException e) {
              LOGGER.error("error:", e);
            }
        }
    }
    /**
     * Word 2 pdf.
     * liunx 
     * @param pdfFilePath   the pdf file path
     */
    public static void word3Pdf(String pdfFilePath,String wordFilePath) {
        FileOutputStream fileOS = null;
        // 验证License
        if (!getLicense()) {
          LOGGER.error("验证License失败!");
            return;
        }
        File inputWord = new File(wordFilePath);
        try {
          //此处处理乱码和小方块
            //如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题
          //指定文件库内容路径
            FontSettings.getDefaultInstance().setFontsFolders(
                    new String[] {"/usr/share/fonts", "/usr/share/fonts/chinese"}
                    , true);
            Document doc = new Document(new FileInputStream(inputWord));
            fileOS = new FileOutputStream(new File(pdfFilePath));
            // 保存转换的pdf文件
            doc.save(fileOS, SaveFormat.PDF);
        } catch (Exception e) {
          LOGGER.error("error:", e);
        } finally {
            try {
                if(fileOS != null){
                    fileOS.close();
                }
            } catch (IOException e) {
              LOGGER.error("error:", e);
            }
        }
    }
    //*-----------------------------------------------------------------
    /**
     * Word 2 pdf.
     *
     * @param multipartFile the multipart file
     * @param pdfFilePath   the pdf file path
     */
    public static void word3Pdf(MultipartFile multipartFile, String pdfFilePath) {
        FileOutputStream fileOS = null;
        // 验证License
        if (!getLicense()) {
          LOGGER.error("验证License失败!");
            return;
        }
        try {
            Document doc = new Document(multipartFile.getInputStream());
            fileOS = new FileOutputStream(new File(pdfFilePath));
            // 保存转换的pdf文件
            doc.save(fileOS, SaveFormat.PDF);
        } catch (Exception e) {
          LOGGER.error("error:", e);
        } finally {
            try {
                if(fileOS != null){
                    fileOS.close();
                }
            } catch (IOException e) {
              LOGGER.error("error:", e);
            }
        }
    }
}

2.控制台

输入路径==>"  sourcePath  
输出路径==>" + targetPath
WORD转pdf
//本地调用
WordPdfUtil.word2Pdf(targetPath, sourcePath);
//服务器调用(需要安装字体库,否则乱码)
WordPdfUtil.word3Pdf(targetPath, sourcePath);

3.解决乱码

解决方案1:

环境解决安装字库,将win机器的c:\windows\fonts目录下的全部文件拷贝到生产服务器字体安装目录下。

将window中字体解压拷贝放到linux中,上传至/usr/shared/fonts/chinese或者/usr/share/fonts目录下,上面的liunx代码已经指定路径;fonts/和fonts/chinese 我这边是没有的自己创建即可;2个地址写放一个即可;

解决方案2:

环境解决安装字库,将win机器的c:\windows\fonts目录下的全部文件拷贝到生产服务器字体安装目录下,然后执行以下命令更新字体缓存。(此步和上面一样)

查看linux目前的所有字体

fc-list

查看Linux目前的所有中文字体

fc-list :lang=zh

拷贝到linux下的字体目录

mkdir /usr/share/fonts/win
cp /local/src/fonts/* /usr/share/fonts/win

执行安装字体命令

cd /usr/share/fonts
sudo mkfontscale
sudo mkfontdir 
sudo fc-cache -fv

执行命令让字体生效

source /etc/profile

如果安装失败,可以考虑修改字体权限

chmod 755 *.ttf

(此方法来源网上我这边并未尝试,我这边并无太高权限所以无法支撑此操作)

4.JAR包

<dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>16.8.0</version>
        </dependency>

https://download.csdn.net/download/qq_42055933/87269529

csdn的我这边是免费不知道他会不会收费(如收费大家需要可私信或者留言)

百度云:链接:https://pan.baidu.com/s/19clf3JPKMkr_O9uUFF8C0Q 密码:bbu0

记录下自己以后用的时候好抄;


相关文章
|
8天前
|
应用服务中间件
使用 Adobe Livecycle Enterprise service 将 word 文档转换成 PDF 格式
使用 Adobe Livecycle Enterprise service 将 word 文档转换成 PDF 格式
32 0
|
8天前
|
缓存 Linux Windows
Linux下安装宋体以及微软雅黑字体-转PDF后不显示中文,以及中文乱码
Linux下安装宋体以及微软雅黑字体-转PDF后不显示中文,以及中文乱码
108 0
|
8天前
|
Java Linux 数据安全/隐私保护
Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
136 0
|
8天前
|
Java 程序员 数据安全/隐私保护
分享一个word转pdf的工具类Aspose[java]
分享一个word转pdf的工具类Aspose[java]
58 0
|
8天前
|
数据安全/隐私保护 Python Windows
Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)
Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)
56 0
|
8天前
|
存储 Shell 数据安全/隐私保护
Python 自动化指南(繁琐工作自动化)第二版:十五、使用 PDF 和 WORD 文档
Python 自动化指南(繁琐工作自动化)第二版:十五、使用 PDF 和 WORD 文档
61 1
|
8天前
|
数据安全/隐私保护 Python
Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)(下)
Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)
41 1
|
Linux
Linux删除乱码文件
2015年2月28日 17:11:54 1.  ls -i  列出文件的inode号 674664 ??Φ-ͦ?+?-ˬͩ-????-??.doc 2.  find folder -inum -delete find . -inum 674664 -delete
633 0
|
Linux vr&ar
linux删除乱码文件
操作前请明确自己在做什么,一些小白不明所以自己乱操作造成数据丢失不去找自身原因却将责任推于本文。数据宝贵,谨慎操作! 一些乱码文件不可以通过普通的rm命令进行管理。 可以通过删除i节点的方式删除。
642 0

热门文章

最新文章