前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >aspose win/linux WORD转PDF(及其解决乱码方式)

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

作者头像
默 语
发布2024-11-20 11:11:21
发布2024-11-20 11:11:21
25000
代码可运行
举报
文章被收录于专栏:JAVAJAVA
运行总次数:0
代码可运行
aspose win/linux WORD转PDF(及其解决乱码方式)

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

1.工具类

代码语言:javascript
代码运行次数:0
复制
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.控制台

代码语言:javascript
代码运行次数:0
复制
输入路径==>"  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目前的所有字体

代码语言:javascript
代码运行次数:0
复制
fc-list

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

代码语言:javascript
代码运行次数:0
复制
fc-list :lang=zh

拷贝到linux下的字体目录

代码语言:javascript
代码运行次数:0
复制
mkdir /usr/share/fonts/win
cp /local/src/fonts/* /usr/share/fonts/win

执行安装字体命令

代码语言:javascript
代码运行次数:0
复制
cd /usr/share/fonts
sudo mkfontscale
sudo mkfontdir 
sudo fc-cache -fv

执行命令让字体生效

代码语言:javascript
代码运行次数:0
复制
source /etc/profile

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

代码语言:javascript
代码运行次数:0
复制
chmod 755 *.ttf

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

4.JAR包

代码语言:javascript
代码运行次数:0
复制
		<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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-08-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • aspose win/linux WORD转PDF(及其解决乱码方式)
  • 1.工具类
  • 2.控制台
  • 3.解决乱码
  • 4.JAR包
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档