springboot 根据模板生成word导出pdf

部分主要代码: 

@GetMapping("/export")
    public void exporttopdf(@Param("lunwenid") Integer lunwenid,HttpServletResponse response)
            throws IOException, InvocationTargetException, IllegalAccessException {
        QueryWrapper<LunwenDetailEntity> wrapper=new QueryWrapper();
        wrapper.lambda().eq(LunwenDetailEntity::getLunwenid,lunwenid);
        List<LunwenDetailEntity> lunwenDetail = lunwenDetailService.list(wrapper);
        LunwenEntity lunwenEntity=lunwenService.getById(lunwenid);
        WordParam wordParam = new WordParam();
        wordParam.setLunwenname(lunwenEntity.getLunwenname());
        wordParam.setName(lunwenEntity.getName());
        wordParam.setNumber(lunwenEntity.getNumber());
        wordParam.setZy(lunwenEntity.getZy());
        wordParam.setWcdate(lunwenEntity.getWcdate());
        wordParam.setTeacher(lunwenEntity.getTeacher());
        wordParam.setXy(lunwenEntity.getXy());

        lunwenDetail.forEach(LunwenEntity->{
            if(LunwenEntity.getTabtitleid().equals("1402871389312622592")){
                wordParam.setZhaiyao(LunwenEntity.getContent());
            }else if(LunwenEntity.getTabtitleid().equals("1402871389312622593")){
                wordParam.setGjc(LunwenEntity.getContent());
            }else if(LunwenEntity.getTabtitleid().equals("1402871389312622594")){
                wordParam.setEnglishzy(LunwenEntity.getContent());
            }else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
                wordParam.setEnglishgjz(LunwenEntity.getContent());
            }
            else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
                wordParam.setEnglishgjz(LunwenEntity.getContent());
            }
            else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
                wordParam.setEnglishgjz(LunwenEntity.getContent());
            }
            else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
                wordParam.setEnglishgjz(LunwenEntity.getContent());
            }else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
                wordParam.setEnglishgjz(LunwenEntity.getContent());
            }else if(LunwenEntity.getTabtitleid().equals("1480445351848153088")){//参考文献
                wordParam.setWx(LunwenEntity.getContent());
            }
        });
        // 模版位置
        File file = new File(fileSpace+"\\test.docx");
        InputStream inputStream = new FileInputStream(file);
        XWPFDocument document = new XWPFDocument(inputStream);
        // 获取整个文本对象
        List<XWPFParagraph> paragraphs = document.getParagraphs();
        // 存放匹配字段的值
        Map map=new HashMap();
        map.put("${lunwenname}","Lunwenname");
        map.put("${name}","Name");
        map.put("${number}","Number");
        map.put("${teacher}","Teacher");
        map.put("${xy}","Xy");
        map.put("${zy}","Zy");
        map.put("${wcdate}","Wcdate");
        map.put("${zhaiyao}","Zhaiyao");
        map.put("${gjc}","Gjc");
        map.put("${englishzy}","Englishzy");
        map.put("${englishgjz}","Englishgjz");
       .......省略
        // for (XWPFParagraph paragraph : paragraphs) {
        for (int par=0;par<paragraphs.size();par++) {
            XWPFParagraph paragraph=paragraphs.get(par);
            List<XWPFRun> runs = paragraph.getRuns();
            for (int i=0;i<runs.size();i++) {
                XWPFRun run=runs.get(i);
                // 获取word中每一个段落的内容
                String runString = run.toString();
                // 正则匹配word要替换的内容
                Matcher m = Pattern.compile("\\$\\{(.*?)}").matcher(runString);
                if (m.find()) {
                    for (Object key : map.keySet()) {
                        if (runString.equals(key)) {
                            Object param = map.get(key);
                            String getParam = "get" + param.toString();
                            Method method = ReflectUtil.getMethod(WordParam.class, getParam);
                            Object o = method.invoke(wordParam);
                            run.setText(o.toString(), 0);
                           
                        }
                    }
                }
            }
        }
      
        FileOutputStream fileOutputStream = new FileOutputStream(fileSpace+"\\"+wordParam.getLunwenname()+".docx");
        document.write(fileOutputStream);
        document.close();
        try {
            WordprocessingMLPackage pkg = Docx4J.load(new File(fileSpace+"\\"+wordParam.getLunwenname()+".docx"));
            Mapper fontMapper = new IdentityPlusMapper();
            fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
            fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
            fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
            fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
            fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
            fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
            fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
            fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
            fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
            fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
            fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
            fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
            fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
            fontMapper.put("等线", PhysicalFonts.get("SimSun"));
            fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
            fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
            fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
            fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
            fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
            fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
            fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
            fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
            fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
            fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
            fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
            pkg.setFontMapper(fontMapper);
            Docx4J.toPDF(pkg, new FileOutputStream(fileSpace+"\\"+wordParam.getLunwenname()+".pdf"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Docx4JException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            byte[] bytes = fileHolder.findByKey(wordParam.getLunwenname()+".pdf",fileSpace);
            MvcUtil.viewData(response, bytes,wordParam.getLunwenname()+".pdf");
        }

    }

部分运行效果图:

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件编程工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值