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

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


相关实践学习
CentOS 7迁移Anolis OS 7
龙蜥操作系统Anolis OS的体验。Anolis OS 7生态上和依赖管理上保持跟CentOS 7.x兼容,一键式迁移脚本centos2anolis.py。本文为您介绍如何通过AOMS迁移工具实现CentOS 7.x到Anolis OS 7的迁移。
相关文章
|
1月前
|
Linux Python Windows
Python PDF文件转Word格式,只需要3秒(附打包)
Python PDF文件转Word格式,只需要3秒(附打包)
46 3
Python PDF文件转Word格式,只需要3秒(附打包)
|
29天前
|
Python
Python——将PPT和Word转为PDF文件
Python——将PPT和Word转为PDF文件
32 1
|
13天前
|
Python
还不会免费将PDF转为Word?你可以试试这3种工具!
还不会免费将PDF转为Word?你可以试试这3种工具!
|
2月前
|
JavaScript Java
Java 将Markdown文件转换为Word和PDF文档
【7月更文挑战第5天】Java中使用`Spire.Doc for Java`库可方便地将Markdown转换为Word或PDF。基本步骤包括导入模块,创建`Document`对象,加载Markdown文件,然后保存为目标格式(`.docx`或`.pdf`)。若遇到`Invalid UTF-8 stream`错误,需确保Markdown文件是UTF-8无BOM编码。页面设置可通过`PageSetup`类调整。注意,实际应用会依据具体需求和环境有所调整。
136 6
|
3月前
|
Java Linux PHP
【应急响应】后门攻击检测指南&Rookit&内存马&权限维持&WIN&Linux
【应急响应】后门攻击检测指南&Rookit&内存马&权限维持&WIN&Linux
|
3月前
|
数据可视化 网络协议 Linux
Linux 怎样通过win 远程桌面连接链接Linux后台服务器的可视化图形界面
Linux 怎样通过win 远程桌面连接链接Linux后台服务器的可视化图形界面
45 0
|
27天前
|
XML 缓存 JSON
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
96 0
|
14天前
|
移动开发 资源调度 JavaScript
Vue移动端网页(H5)预览pdf文件(pdfh5和vue-pdf)
这篇文章介绍了在Vue移动端网页中使用`pdfh5`和`vue-pdf`两个插件来实现PDF文件的预览,包括滚动查看、缩放、添加水印、分页加载、跳转指定页数等功能。
Vue移动端网页(H5)预览pdf文件(pdfh5和vue-pdf)
|
23天前
|
JSON JavaScript 数据格式
打印插件 hiprint 使用、回单打印PDF保存本地、将列表数据打印成pdf文件保存到本地
这篇文章介绍了如何使用hiprint打印插件将列表数据打印成PDF文件并保存到本地,包括插件的配置、依赖安装、项目代码案例以及如何预览和打印数据。
打印插件 hiprint 使用、回单打印PDF保存本地、将列表数据打印成pdf文件保存到本地
|
29天前
|
Python
Python——批量将PDF文件转为图片
Python——批量将PDF文件转为图片
26 2

热门文章

最新文章