java使用 template模板ftl 含有图片的生成数据

发布时间 2024-01-11 15:55:30作者: HeavenTang

点击查看代码
 /**
     *  Base64编码.
     */
    public static String base64Encode(byte[] input) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(input);
    }
    @Override
    public void printStudentRxdjb(StudentRxdjInfoDto dto) {
        List<StudentRxdjVo> xsList = studentInfoMapper.getStudentRxdjData(dto);
        String downloadName = "招生入学登记模板导出.zip";
        String filePath = request.getSession().getServletContext().getRealPath("/");
        response.setContentType("application/zip;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + Encodes.urlEncode(downloadName));
        try (
                OutputStream outputStream = response.getOutputStream();
                ZipOutputStream zos = new ZipOutputStream(outputStream)
        ) {

            for (int i = 0; i < xsList.size(); i++) {
                if(null != xsList.get(i).getPhoto()){
                    String xwbase64 = base64Encode(xsList.get(i).getPhoto());
//                    xsList.get(i).setZp("data:image/jpg;base64,"+xwbase64);
                    xsList.get(i).setZp(xwbase64);
                }


                Configuration configuration = new Configuration(Configuration.getVersion());
                configuration.setDefaultEncoding("UTF-8");
                configuration.setClassForTemplateLoading(this.getClass(), "/word");
                String temName = "登记表2.ftl";
                if("pdf".equals(dto.getExportType())){
                    temName = "学登记表2.ftl";
                }
                Template template = configuration.getTemplate(temName);
                Map<String, Object> params = new HashMap<>();
                params.put("schoolName", "学");
                params.put("wordList", Arrays.asList(xsList.get(i)));

                {

//                    String fileName = "研究生新生入学登记表.doc";
                    String fileName = xsList.get(i).getXy()+"_硕士"+"_"+xsList.get(i).getXh()+".doc";
                    File outFile = new File(filePath + "/" + fileName);
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    ZipEntry zipEntry = new ZipEntry(fileName);
                    BufferedInputStream bufferedInputStream = null;
                    try (
                            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)))
                    ) {
                        template.process(params, out);
                        bufferedInputStream = new BufferedInputStream(new FileInputStream(outFile));

                        // 这里是导出word
                        if(!"pdf".equals(dto.getExportType())) {
                            zos.putNextEntry(zipEntry);
                            while ((len = bufferedInputStream.read(buffer)) != -1) {
                                zos.write(buffer, 0, len);
                            }
                        }
                        if("pdf".equals(dto.getExportType())){
                            // 这里是doc转pdf, 但转出来的在linux里面字体有问题
                            Consumer<Writer> consumer = writer1 -> {
                                try {
                                    writer1.write(Common.fileToCharArray(outFile));
                                } catch (IOException e) {
                                    log.error("转化为pdf失败:{}", e);
                                    throw new RuntimeException(e);
                                }
                            };
                            PdfUtil.wordToPdf(zos,"学生证明", consumer);
                        }

                    } catch (FileNotFoundException | TemplateException e) {
                        log.error("context", e);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (null != bufferedInputStream) {
                            bufferedInputStream.close();
                        }
                        if (outFile.exists()) {
                            if (!outFile.delete()) {
                                log.info("删除失败!");
                            }
                        }
                    }
                }
            }

        }catch (IOException e) {
            log.error("context",e);
        }

    }