word转pdf

根据公司要求要求word转pdf

依赖

<!--    word 转 pdf-->
<dependency>
    <groupId>com.luhuiguo</groupId>
    <artifactId>aspose-words</artifactId>
    <version>23.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.2</version>
</dependency>
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.9.0-beta</version>
</dependency>

模版

代码工具类

Lincense文件

<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>
        sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
    </Signature>
</License>
/**
     * 填充word模板,获取填充后的word文件流:
     * @param datas: 填充数据 
     * @param fileStream: 文件模板流 -- 可从OSS/FastDFS上获取流,直接传入;
     * @return
     * @throws IOException
     */
    public static byte[] fillWord(Map<String,Object> datas, InputStream fileStream)  {
        XWPFTemplate template = null;
        ByteArrayOutputStream wordOut = new ByteArrayOutputStream(1024);
        try {
            //设置占位符:
            Configure config = Configure.builder()
                //SpringEl 表达式
                .useSpringEL()
                //若模板中占位符与填充属性不对应,则报错;
//                .setValidErrorHandler(new Configure.AbortHandler())
                //设置模板中表格的参数属性
                .bind("list", new HackLoopTableRenderPolicy())
                .build();
            //填充文本域:
            template = XWPFTemplate.compile(fileStream, config).render(datas);
            template.write(wordOut);
        }catch (RenderException ex){
            log.error("模板填充失败,请保证模板文本域与参数保持一致。错误信息::{}",ex.getMessage());
            throw new ServiceException(ex.getMessage());
        } catch (IOException ex){
            log.error("系统异常::{}",ex.getMessage());
            throw new ServiceException(ex.getMessage());
        }finally {
            try {
                if (template != null){
                    template.close();
                }
            } catch (IOException ex) {
                log.error("关闭文件流资源异常::{}",ex.getMessage());
            }
        }
        return wordOut.toByteArray();
    }

/**
     * word转pdf
     * @param wordInStream: word文件输入流;
     * @param pdfOutStream: pdf文件输出流;
     */
    public static void wordToPDF(FileInputStream wordInStream, FileOutputStream pdfOutStream) {
        try {
            InputStream licenseIn = WordUtil.class.getClassLoader().getResourceAsStream("License.xml");
            License aposeLic = new License();
            aposeLic.setLicense(licenseIn);
            //因linux环境没有语言包,当项目部署到linux环境后,转成的pdf中的中文是类似乱码的,所以需要去加载此目录下的语言包;
            OsInfo osInfo = SystemUtil.getOsInfo();
            Document doc = new Document(wordInStream);
            FontSettings settings = new FontSettings();
            URL fontFolderUrl = Thread.currentThread().getContextClassLoader().getResource("font/");
            File fontsDir = new File(fontFolderUrl.getFile());
            settings.setFontsFolder(fontsDir.getAbsolutePath(),true);
            settings.resetFontSources();
            doc.setFontSettings(settings);
//            if(osInfo.isLinux()){
//                //====================================== 注意 ============================================
//                //注意: 需要把项目中resource/fonts目录下的语言包全部放到linux中的此目录下,否则linux环境中处理是乱码;
//                //以下是我自己定义的目录,你可随意更改为你linux上的目录;
//                FontSettings.setFontsFolder("/usr/share/fonts/chinese", true);
//                //=======================================================================================
//            }
            //Address是将要被转化的word文档
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            doc.save(pdfOutStream, SaveFormat.PDF);
            pdfOutStream.close();
        } catch (Exception e) {
            log.error("word转pdf异常::{}",e);
            throw new RuntimeException("word转pdf异常");
        }
    }
  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值