使用aspose将docx(支持多种文档类型)转化为pdf,以及linux上转化后中文乱码问题

此代码示例展示了如何在Java中使用Aspose库将Docx文档流转换为PDF,同时处理Linux上的中文乱码问题。通过设置License避免转换后的PDF带有水印,并提供了在内存中转换为PDF流的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击下载对应jar包
使用aspose将docx(支持多种文档类型)转化为pdf,以下为docx流转化为pdf文件或pdf流(不产生中间文件)

/**
     * 验证License 若不验证则转化出的pdf文档会有水印产生
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        InputStream is = null;
        try {
            is = WordUtils.class.getClassLoader().getResourceAsStream("aspose/license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * @param inputStream 需要被转换的word的流
     * @param pdfPath 转换之后pdf的全路径带文件名
     */
    public static boolean docxToPdf(InputStream inputStream, String pdfPath) {
        FileOutputStream os = null;
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return false;
        }
        try {
            File outputFile = new File(pdfPath);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
             }
            os = new FileOutputStream(outputFile);
            Document docx = new Document(inputStream);
            // 支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB,XPS, SWF 相互转换
            docx.save(os, SaveFormat.PDF);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * @param inputStream 需要被转换的word的流
     * @return pdf流
     */
    public static InputStream docxToPdf(InputStream inputStream) {
        ByteArrayOutputStream os = null;
        ByteArrayInputStream is = null;
        // 验证License 若不验证则转化出的pdf文档会有水印产生
//        if (!getLicense()) {
//            throw new ResultException(500, "docx转pdf验证失败");
//        }
        //设置中文字体所在目录
        OsInfo osInfo = SystemUtil.getOsInfo();
        if(osInfo.isLinux()){
            FontSettings fontSettings = new FontSettings();
            fontSettings.setFontsFolder("/usr/share/fonts/chinese", true);
        }
        try {
            os = new ByteArrayOutputStream();
            Document docx = new Document(inputStream);
            // 支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB,XPS, SWF 相互转换
            docx.save(os, SaveFormat.PDF);
            is = new ByteArrayInputStream(os.toByteArray());
            return is;
        } catch (Exception e) {
            throw new ResultException(500, "合同生成失败");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

license.xml用来解决水印问题;linux上可能会中文乱码,因为linux没有对应的中文文件,上面代码设置了linux上中文文件所在的路径,只需将window上的中文文件拷贝到linux上即可。window上路径为C:\Windows\Fonts
在这里插入图片描述

参考1:https://blog.youkuaiyun.com/River_Frozen/article/details/105774825?spm=1001.2014.3001.5502
参考2:https://blog.youkuaiyun.com/Number_oneEngineer/article/details/122124096

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值