java使用 template模板ftl 含有图片的生成数据
点击查看代码
/**
* 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);
}
}
posted on 2024-01-11 15:54 HeavenTang 阅读(301) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix