使用Aspose将Excel转PDF,解决linux乱码问题(中文全是小方格),windows环境正常

excel转pdf工具类

import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.FontSettings;
import lombok.extern.slf4j.Slf4j;
import java.io.*;

@Slf4j
public class ExcelToPdfUtils {
    private static boolean getLicense() {
  /*      boolean result = false;
        try {
            String filePath = "/License.xml";
            ClassPathResource classPathResource = new ClassPathResource(filePath);
            InputStream is =classPathResource.getInputStream();
           // InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream(""); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        boolean result = true;
        return result;
    }
    
    public static File excel2pdf(String inpath, String outPath) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return null;
        }
        FileOutputStream outputStream = null;
        File file = null;
        try {
            //获取当前java运行
            String os = System.getProperty("os.name");
            //当前环境:linux系统
            if (os != null && os.toLowerCase().startsWith("linux")) {
                //apose在linux服务器上生成pdf,内容乱码问题,解决代码:将windows的字体上传到linux,取linux上的字体列表
                FontSettings fontSettings = FontSettings.getDefaultInstance();
                fontSettings.setFontsFolder(File.separator + "usr"
                        + File.separator + "share" + File.separator + "fonts" + File.separator, true);
                log.info("excel2pdf_linux环境加载字体包");
            }
            long old = System.currentTimeMillis();
            file = new File(outPath); // 新建一个空白pdf文档
            outputStream = new FileOutputStream(file);
            Workbook wb = new Workbook(inpath);// 原始excel路径
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放,不分页。
            pdfSaveOptions.setOnePagePerSheet(true);
            wb.save(outputStream, pdfSaveOptions);
            long now = System.currentTimeMillis();
            log.info("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
        } catch (Exception e) {
            e.printStackTrace();
            log.error("excel2pdf error:" + e);
            return null;
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("关闭流失败:" + e);
                    return null;
                }
            }
        }
        return file;
    }

    /**
     * @Description
     * @Author chengweiping
     * @Date 2020/11/13 10:50
     */
    public static void excel2pdfNew(InputStream inputStream, OutputStream outputStream) {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
           /* FontSettings.setFontsFolder(File.separator + "usr"
                    + File.separator + "share" + File.separator + "fonts", true);*/
            long old = System.currentTimeMillis();
            Workbook wb = new Workbook(inputStream);// 原始excel路径
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            //缩放到一个页面(如果列太多 太长)
            pdfSaveOptions.setOnePagePerSheet(true);
            //重点,设置所有列放在一页里,会自动适应宽度
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
            wb.save(outputStream, pdfSaveOptions);
            long now = System.currentTimeMillis();
            log.info("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

使用的依赖

        <dependency>
            <groupId>com.luhuiguo</groupId>
            <artifactId>aspose-cells</artifactId>
            <version>22.10</version>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>20.6</version>
        </dependency>

linux环境excel转pdf乱码问题

需要向linux服务器上传字体包,存放目录是 /usr/share/fonts
Windows上的字体包在C:\Windows\Fonts 下
操作步骤:

  • 将你选择的字体文件,比如simsun.ttc(宋体),上传到你的程序中,我是放在和target同级的文件夹下
    在这里插入图片描述
  • 代码中指定你要用的字体包的路径
//apose在linux服务器上生成pdf,内容乱码问题,解决代码:将windows的字体上传到linux,取linux上的字体列表
FontSettings fontSettings = FontSettings.getDefaultInstance();
 fontSettings.setFontsFolder(File.separator + "usr"
         + File.separator + "share" + File.separator + "fonts" + File.separator, true);
  • 构建时 在docker file 中写入
FROM openjdk:8-sw66
COPY target/abc.jar /abc.jar
# 复制宋体字体文件到容器中的字体目录
COPY font/simsun.ttc /usr/share/fonts/simsun.ttc
# 安装字体配置工具
RUN  apt-get install -y fontconfig xfonts-utils
# 配置字体路径
RUN mkfontscale && mkfontdir && fc-cache -fv
CMD ["java", "-jar","-Duser.timezone=GMT+8","abc.jar"]

abc.jar是你的服务的jar包

到这里,问题就解决啦!!!撒花!!!

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值