根据word模板生成word并转成pdf

1. 使用poi-ti填充word模板

  1. 依赖
    <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.5.1</version>
       </dependency>
  1. 实现

    1. 填充实体类
    
            UserContract contract = new UserContract();
            String contractNo = order.getOrderNo();
            contract.setOrderNo(contractNo);
            // 添加门店的信息
            contract.setStoreName(store.getStoreName());
            contract.setStoreAddress(store.getDeliveryAddress());
            contract.setStorePhone(store.getStoreMobile());
            contract.setStoreHead(store.getStoreHead());
            contract.setStoreCourt(store.getStoreCourt());
            // 添加用户的信息
            contract.setUserName(user.getName());
            contract.setUserIdCard(user.getIdcard());
            contract.setUserPhone(user.getMobile());
            contract.setUserAddress(user.getDeliveryAddress());
            // 添加商品信息
            contract.setPhoneType(order.getPhoneModel() + "_" + order.getPhoneSpecification());
            contract.setPhoneNum(order.getPhoneCount());
            contract.setPhoneSp(order.getPhoneSpecification());
            contract.setPhonePrice(String.valueOf(order.getOrderAmountTotal().subtract(BigDecimal.valueOf(1000))));
            contract.setAllPrice(String.valueOf(order.getOrderAmountTotal().subtract(BigDecimal.valueOf(1000))));
            contract.setAllAmount(String.valueOf(order.getOrderAmountTotal()));
            // 添加分期数最后一期
          contract.setLastPayTime(lastPayTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
            contract.setFirstPayDay(order.getPayOnceTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            contract.setFirstAmount(String.valueOf(order.getPayOnceAmount()));
            contract.setResidualAmount(String.valueOf(order.getInstallmentAmountTotal()));
            contract.setPhasesNumber(order.getInstallmentNum());
            contract.setFixedDay(order.getInstallmentPayDay());
            List<RowRenderData> listRowRenderData = getListRowRenderData(order.getInstallmentNum(), order.getInstallmentAmount());
            MiniTableRenderData tableRenderData = new MiniTableRenderData(listRowRenderData);
            // 添加表格数据
            contract.setBillTable(tableRenderData);
            // 设置月份
            contract.setFixedMonth(order.getInstallmentMonthType());
    
            return contract;
    

​ 2.获取文件并填充输出到response流中

File file = new File(template_contract);

  // 设置response输入流
        OutputStream outputStream = response.getOutputStream();
        // word
        response.setContentType("application/octet-stream");

        response.setHeader("Content-disposition", "attachment;filename=\"" + URLEncoder.encode(filename, "UTF-8") + ".docx" + "\"");
 // 开始生成新模板
        XWPFTemplate template = XWPFTemplate.compile(file);
        template.render(contract);
        template.write(outputStream);

2. 使用poi将word转成pdf

  1. 依赖

     <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>ooxml-schemas</artifactId>
                <version>1.3</version>
            </dependency>
            
            
    
  2. 实现

            File file = new File(filename);
            if (!file.exists()) {
                throw new IOException("word版本合同丢失");
            }
            FileInputStream fileInputStream = new FileInputStream(file);
    
            ByteArrayOutputStream outPDF = new ByteArrayOutputStream();
            XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
            PdfOptions options = PdfOptions.create();
            PdfConverter.getInstance().convert(xwpfDocument, outPDF, options);
            FileOutputStream fileOutputStream = new FileOutputStream(filename.replace("docx", "pdf"));
            fileOutputStream.write(outPDF.toByteArray());
            outPDF.close();
            fileOutputStream.close();
            fileInputStream.close();
            return filename.replace("docx", "pdf");
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值