word文档docx转为pdf文件,在Linux操作系统上也能正常显示中文

本文介绍了在Linux系统上遇到word转pdf过程中汉字不显示的问题及其解决方案。问题根源在于系统缺少中文字体库。解决方法是将Windows的字体文件复制到Linux的/usr/share/fonts/wfonts目录下,并确保已安装Linux应用桌面。同时提供了Java代码示例,用于实现docx到pdf的转换。
摘要由CSDN通过智能技术生成

 

 在我的项目中出现了问题 生产(Linux)上就是word转pdf没有汉字显示,只有字母和数字。

因为liunx系统上,没有安装有中文字体库;

把win的上C:\Windows\Fonts文件夹,

上传到linux系统上的/usr/share/fonts路径下,新建一个wfonts文件夹存放;

若linux上不存在/usr/share/fonts路径,是因为没安装linux版应用桌面,所以要先安装一下应用。

解决方案详情见我的另一篇文章:https://blog.csdn.net/candy2011_2011/article/details/107836494

 

下面是实现word文档docx转为pdf文件的代码。

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell; 
import org.apache.poi.xwpf.usermodel.XWPFParagraph;  
import org.apache.poi.xwpf.usermodel.XWPFRun;   
import org.apache.poi.xwpf.usermodel.XWPFTableRow; 
import org.springframework.util.ClassUtils;
import org.springframework.web.util.HtmlUtils;

import org.apache.commons.collections.MapUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

/**
 * 创建word文档
 * @title: WordUtil.java
 * @description: TODO
 * @author: mbl
 * @date: 2020年7月20日 上午10:32:35
 * @version: V1.0
 */
@Slf4j
public class WordUtil {
    private WordUtil() {
    }

    /**
     * 创建2017版word的xml文件
     * 
     * @title: createWord2017Xml
     * @description: TODO
     * @author: mbl
     * @date: 2020年7月21日 下午5:57:41
     * @param dataMap
     * @param templateXmlName
     * @param filePath
     * @param xmlFileName
     * @throws:
     */
    public static void createWord2017Xml(Map<String, Object> dataMap, String templateXmlName, String templatePath,
            String filePath, String xmlFileName) {
        if(!StringUtils.isEmpty(templatePath)&&!templatePath.endsWith("/")) { 
            templatePath=templatePath+'/'; 
            }
        try {
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
            // 设置编码
            configuration.setDefaultEncoding("UTF-8");
            /** 加载文件 **/
            // 通过templates内部引用zip,编译生成tar

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值