package com.zcloud.util.archivesUtil; import com.zcloud.service.eduArchives.ArchivesService; import com.zcloud.service.eduStudy.ClassService; import com.zcloud.entity.PageData; import com.zcloud.entity.TrainingPlanDao; import com.zcloud.util.Const; import com.zcloud.util.Tools; import com.zcloud.util.WordToPdfUtil; import fr.opensagres.xdocreport.document.IXDocReport; import fr.opensagres.xdocreport.document.images.ByteArrayImageProvider; import fr.opensagres.xdocreport.document.registry.XDocReportRegistry; import fr.opensagres.xdocreport.template.IContext; import fr.opensagres.xdocreport.template.TemplateEngineKind; import fr.opensagres.xdocreport.template.formatter.FieldsMetadata; import fr.opensagres.xdocreport.template.formatter.NullImageBehaviour; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * 说明:培训影像资料 107 * 作者:wangxuan * 官网:www.zcloudchina.com */ @Component public class ImageDataAbsHandler extends AbsHandlerBatchExport { /* * 同一前缀 archives101 * 同一前缀 archives102 * varList: [{ NAME: '安全培训教材会审表', TYPE: 100 }, { NAME: '培训日程安排通知/也可称为培训计划', TYPE: 101 }, { NAME: '安全培训教材或课程讲义', TYPE: 106 }, { NAME: '安全培训教育记录及签字表', TYPE: 102 }, { NAME: '培训影像资料', TYPE: 107 }, { NAME: '培训考核试卷', TYPE: 103 }, { NAME: '学员考核成绩统计表', TYPE: 104 }, { NAME: '本期综合考评报告', TYPE: 105 }, { NAME: '与培训机构签订的服务合同或者协议复印件', TYPE: 6 }], * */ @Autowired private ArchivesService archivesService; @Autowired private ClassService classService; @Override public File batchExport(PageData pd, String now) throws Exception { String projectPath = pd.getString("projectPath"); String className = pd.getString("CLASS_NAME"); PageData clazz = classService.findById(pd); List studentList = archivesService.getStudentsFace(pd); File file = new File(projectPath + Const.FILEPATHWORDTEMPLATE + "imagedata.docx"); InputStream mbwj = new FileInputStream(file); //注册xdocreport实例并加载FreeMarker模板引擎 IXDocReport report = XDocReportRegistry.getRegistry().loadReport(mbwj, TemplateEngineKind.Freemarker); //创建xdocreport上下文对象 IContext context = report.createContext(); context.put("value1", clazz.getString("CODE")); //班级编码 context.put("value2", clazz.getString("NAME")); //班级名称 List list1 = new ArrayList<>(); //一共多少航 for (int i = 0; i < studentList.size(); i++) { TrainingPlanDao trainingPlanDao = new TrainingPlanDao(); //每行的内容实体类 trainingPlanDao.setValue1(String.valueOf(i + 1)); //序号 trainingPlanDao.setValue2(studentList.get(i).getString("NAME")); //姓名 trainingPlanDao.setValue3(studentList.get(i).getString("USER_ID_CARD")); //身份证ID trainingPlanDao.setValue4(studentList.get(i).getString("PHONE")); //手机号 if (Tools.notEmpty(studentList.get(i).getString("PORTRAIT"))) { String path = Const.HTTPFILEURL + (String) studentList.get(i).getString("PORTRAIT"); URL url = new URL(path); trainingPlanDao.setPic(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 } else { } String[] arr = new String[0]; if (Tools.notEmpty(studentList.get(i).getString("FACES"))) { arr = studentList.get(i).getString("FACES").split(","); if (arr.length == 1) { String path = Const.HTTPFILEURL + arr[0]; URL url = new URL(path); trainingPlanDao.setPic2(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 } else if (arr.length == 2) { String path = Const.HTTPFILEURL + arr[0]; URL url = new URL(path); trainingPlanDao.setPic2(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[1]; url = new URL(path); trainingPlanDao.setPic3(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 } else if (arr.length == 3) { String path = Const.HTTPFILEURL + arr[0]; URL url = new URL(path); trainingPlanDao.setPic2(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[1]; url = new URL(path); trainingPlanDao.setPic3(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[2]; url = new URL(path); trainingPlanDao.setPic4(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 } else if (arr.length == 4) { String path = Const.HTTPFILEURL + arr[0]; URL url = new URL(path); trainingPlanDao.setPic2(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[1]; url = new URL(path); trainingPlanDao.setPic3(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[2]; url = new URL(path); trainingPlanDao.setPic4(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[3]; url = new URL(path); trainingPlanDao.setPic5(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 } else if (arr.length >= 5) { String path = Const.HTTPFILEURL + arr[0]; URL url = new URL(path); trainingPlanDao.setPic2(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[1]; url = new URL(path); trainingPlanDao.setPic3(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[2]; url = new URL(path); trainingPlanDao.setPic4(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[3]; url = new URL(path); trainingPlanDao.setPic5(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 path = Const.HTTPFILEURL + arr[4]; url = new URL(path); trainingPlanDao.setPic6(new ByteArrayImageProvider(url.openStream())); //照片 读取网络路径 } } list1.add(trainingPlanDao); } context.put("list1", list1); FieldsMetadata fm = report.createFieldsMetadata(); fm.addFieldAsImage("pic", "item.pic", NullImageBehaviour.RemoveImageTemplate); fm.addFieldAsImage("pic2", "item.pic2", NullImageBehaviour.RemoveImageTemplate); fm.addFieldAsImage("pic3", "item.pic3", NullImageBehaviour.RemoveImageTemplate); fm.addFieldAsImage("pic4", "item.pic4", NullImageBehaviour.RemoveImageTemplate); fm.addFieldAsImage("pic5", "item.pic5", NullImageBehaviour.RemoveImageTemplate); fm.addFieldAsImage("pic6", "item.pic6", NullImageBehaviour.RemoveImageTemplate); fm.load("list1", TrainingPlanDao.class, true); FileOutputStream out = new FileOutputStream(projectPath + Const.TEMPZIPFILES + "培训影像资料-" + now + ".docx"); report.process(context, out); out.close(); File word = new File(projectPath + Const.TEMPZIPFILES + "培训影像资料-" + now + ".docx"); //输出到本地目录 WordToPdfUtil.docToPdf(projectPath + Const.TEMPZIPFILES+ "培训影像资料-" + now + ".docx", projectPath + Const.TEMPZIPFILES + "培训影像资料-" + now + ".pdf"); word.delete(); File pdf = new File(projectPath + Const.TEMPZIPFILES + "培训影像资料-" + now + ".pdf"); return pdf; } @Override public void afterPropertiesSet() throws Exception { BatchExportFactory.register("archives107", this); } }