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

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

1.工具类

package com.ces.yun.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

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

点击阅读全文

更多推荐