package com.ruoyi.web.controller.archive; import com.itextpdf.text.*; import com.itextpdf.text.Font; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.common.utils.poi.ExcelExp; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.utils.poi.ExcelUtilManySheet; import com.ruoyi.common.utils.poi.ExcelUtilManySheetSecond; import com.ruoyi.domain.DocumentMaterials; import com.ruoyi.domain.vo.ArchiveInfoVo; import com.ruoyi.domain.vo.DocumentMaterialsVo; import com.ruoyi.domain.vo.DocumentMaterialsVoLarge; import com.ruoyi.domain.vo.DocumentMaterialsVoSmall; import com.ruoyi.framework.web.domain.server.Sys; import com.ruoyi.service.IArchiveRecordsService; import com.ruoyi.service.IDocumentMaterialsService; import com.ruoyi.service.impl.BarcodeService; import com.ruoyi.service.impl.pdfGenerateService; import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellUtil; import org.apache.poi.xssf.usermodel.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.lang.reflect.Field; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.*; import java.util.List; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import static org.apache.poi.hssf.usermodel.HSSFCell.*; @RestController @RequestMapping("/system/archiveAllExport") public class archiveAllExportController { @Autowired private pdfGenerateService pdfGenerateService; @Autowired private BarcodeService barcodeService; @Autowired private IDocumentMaterialsService documentMaterialsService; @Autowired private IArchiveRecordsService iArchiveRecordsService; @PostMapping("/importTemplate") public void importTemplate(HttpServletResponse response) throws IOException { //导出卷面封面代码 ArchiveInfoVo aIV = iArchiveRecordsService.selectByRecordId(55L); List arsi = new ArrayList<>(); arsi.add(aIV); String recordId = aIV.getRecordId(); byte[] imgr = barcodeService.generateBarcodeImage(recordId); byte[] sedcode = pdfGenerateService.createQrCodeN(recordId, 30, 30); ExcelExp e1 = new ExcelExp("案卷封面数据",arsi, ArchiveInfoVo.class); ExcelExp e2 = new ExcelExp("案卷封面", arsi, recordId, imgr,sedcode, ArchiveInfoVo.class); List mysheet = new ArrayList(); mysheet.add(e1); mysheet.add(e2); ByteOutputStream bos1 = new ByteOutputStream(); ExcelUtilManySheetSecond> util2 = new ExcelUtilManySheetSecond>(mysheet); // util2.exportExcelManySheet(response, mysheet); //导出卷面目录代码 // DocumentMaterials documentMaterials = new DocumentMaterials(); // documentMaterials.setRecordId(55L); // // List docs = documentMaterialsService.selectDocumentMaterialsList(documentMaterials); // List dsvs = documentMaterialsService.findArchMInfo("55"); // // dsvs.get(0).setUrl("/profile/upload/2025/08/14/30_20250814212128A031.jpg"); // // // // dsvs.stream().map() // List list2 = dsvs.stream().map(res -> new DocumentMaterialsVoSmall(res.getNum(), res.getDocumentNumber(),res.getCreator(), // res.getTitle(), res.getDate(), res.getPageNumber(), res.getRemarks())).collect(Collectors.toList()); // // String recordId = dsvs.get(0).getRecordId(); // byte[] imgr = barcodeService.generateBarcodeImage(recordId); // ExcelExp e1 = new ExcelExp("卷内目录数据",dsvs, DocumentMaterialsVo.class); // ExcelExp e2 = new ExcelExp("卷内数据", list2, recordId, imgr, DocumentMaterialsVoSmall.class); // List mysheet = new ArrayList(); // mysheet.add(e1); // mysheet.add(e2); // ExcelUtilManySheet> util2 = new ExcelUtilManySheet>(mysheet); // util2.exportExcelManySheet(response, mysheet); } /** * 打包下载 * @param response * @param */ @PostMapping("/export") public void packDownload(HttpServletResponse response, Long id) throws Exception { // 获取文件的保存位置,读取数据库, DocumentMaterials documentMaterials = new DocumentMaterials(); documentMaterials.setRecordId(id); List docs = documentMaterialsService.selectDocumentMaterialsAllByRecordId(id); System.out.println(docs.size()+"----009"); //.selectDocumentMaterialsList(documentMaterials); List dsvs = documentMaterialsService.findArchMInfo(id.toString()); ArchiveInfoVo aIV = iArchiveRecordsService.selectByRecordId(id); List paths = new ArrayList<>(); // System.out.println(docs); if (docs.size() == 1) { // 直接下载 String filePath = paths.get(0); filePath = filePath.replace("/profile/", RuoYiConfig.getProfile() + "/"); File file = new File(filePath); if (!file.exists()) { throw new Exception("文件不存在"); } String suffix = filePath.substring(filePath.lastIndexOf(".")); // 输出文件流 writeFileToRes(response, "user" + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + suffix, file); } else { // 压缩之后在进行下载 //压缩包名称(会拼上当前时间) System.out.println(90); String datumName = "user"; //压缩文件 List filePathList = paths; File file = compressedFileToZip(docs, dsvs, aIV, id); System.out.println(file.getName()); String fileName =aIV.getRecordId()+".zip"; //输出文件流 writeFileToRes(response, file.getName(), file); //删除压缩包 if(file.exists()){ file.delete(); } } } // 输出文件流到response private void writeFileToRes(HttpServletResponse response, String fileName, File file) throws IOException { String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); String filePath = RuoYiConfig.getDownloadPath() + fileName; response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, realFileName); FileUtils.writeBytes(filePath, response.getOutputStream()); } public String frontCompWithZore(int formatLength,int formatNumber){ /** * 0 指前面补充零 * formatLength 字符总长度为 formatLength * inputNumber 格式化数字 * d 代表为正数。 */ String newString = String.format("%0"+formatLength+"d", formatNumber); return newString; } private PdfPCell ImageSet(int high) throws Exception { // byte[] fileByte = toByteArray("D:\\project\\archive\\download\\12.jpg"); // // // FileInputStream fis = new FileInputStream("D:\\project\\archive\\download\\56.png"); // byte[] imageBytes = new byte[fis.available()]; // fis.read(imageBytes); // fis.close(); // Jpeg jpeg = new Jpeg(imageBytes); // jpeg.scaleAbsolute(70, 50); byte[] imageBytes = barcodeService.generateBarcodeImage("D3.4.1-05-2024-0002"); // 加载PNG图片 Image img = Image.getInstance(imageBytes); // 设置图片在PDF中的位置(可选) img.setAbsolutePosition(100, 700); // 将图片添加到PDF文档中 PdfPCell pdfPCell = new PdfPCell(img); pdfPCell.setMinimumHeight(high); pdfPCell.setUseAscender(true); // 设置可以居中 pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中 pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中 return pdfPCell; } // 压缩文件 private File compressedFileToZip(List docs, List dsvs, ArchiveInfoVo aIV, Long id) throws Exception { //压缩包具体名称(拼接时间戳防止重名) String datumName = ""; String zipFileName =dsvs.get(0).getDocumentNumber()+aIV.getRecordId()+ ".zip"; //生成压缩包存储地址(最后会删掉) String fileZip = RuoYiConfig.getProfile() + "/download/" + zipFileName; OutputStream os=null; ZipOutputStream zos = null ; System.out.println("==============_______________"); File file = new File(fileZip); try { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } os=new FileOutputStream(file); //压缩文件 zos = new ZipOutputStream(os); //pdf目录封面 String pdfPathF = "07-卷面封面.pdf"; pdfGenerateService.generatePdf(pdfPathF); // 2. 压缩PDF到ZIP文件 // 添加PDF文件到ZIP ZipEntry zipEntry = new ZipEntry(pdfPathF); zos.putNextEntry(zipEntry); // 读取PDF文件内容并写入ZIP try (FileInputStream fis = new FileInputStream(pdfPathF)) { byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } } //08-卷内卷内目录的pdf String pdf08Path= "08-卷内目录.pdf"; List list3 = dsvs; pdfGenerateService.generateFileDirectoryPdf(pdf08Path, list3); ZipEntry zipEntry2 = new ZipEntry(pdf08Path); zos.putNextEntry(zipEntry2); // 读取PDF文件内容并写入ZIP try (FileInputStream fis = new FileInputStream(pdf08Path)) { byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } } //09-备考表.pdf String pdf09Path = "09-备考表.pdf"; pdfGenerateService.generateFileStyleInfo(pdf09Path, aIV.getRecordId(), id); // 2. 压缩PDF到ZIP文件 // 添加PDF文件到ZIP ZipEntry zipEntry1 = new ZipEntry(pdf09Path); zos.putNextEntry(zipEntry1); // 读取PDF文件内容并写入ZIP try (FileInputStream fis = new FileInputStream(pdf09Path)) { byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } } //excel 卷内封面导出zip ZipEntry entryiv = new ZipEntry("卷内封面" + ".xls"); List arsi = new ArrayList<>(); arsi.add(aIV); String recordId1 = aIV.getRecordId(); byte[] imgr1 = barcodeService.generateBarcodeImage(recordId1); byte[] sedcode = pdfGenerateService.createQrCodeN(recordId1, 30, 30); ExcelExp e3 = new ExcelExp("案卷封面数据",arsi, ArchiveInfoVo.class); ExcelExp e4 = new ExcelExp("案卷封面", arsi, recordId1, imgr1,sedcode, ArchiveInfoVo.class); List mysheet1 = new ArrayList(); mysheet1.add(e3); mysheet1.add(e4); ByteOutputStream bos2 = new ByteOutputStream(); ExcelUtilManySheetSecond> util3 = new ExcelUtilManySheetSecond>(mysheet1); util3.exportExcelManySheet(bos2, mysheet1); // System.out.println(bos2); zos.putNextEntry(entryiv); // ExcelUtil utilsv = new ExcelUtil(ArchiveInfoVo.class); // // // ByteOutputStream boss = new ByteOutputStream(); // List aivs = new ArrayList<>(); // aivs.add(aIV); // utilsv.byteOutputStreamExcel(boss, aivs,"Date List", ""); bos2.writeTo(zos); //写入电子目录 xsxl ZipEntry entry = new ZipEntry("电子文件目录" + ".xls"); ExcelUtil util = new ExcelUtil(DocumentMaterialsVoLarge.class); zos.putNextEntry(entry); ByteOutputStream bos = new ByteOutputStream(); util.byteOutputStreamExcel(bos, docs,"电子文件目录", ""); bos.writeTo(zos); //拿到卷内目录的excel List list2 = dsvs.stream().map(res -> new DocumentMaterialsVoSmall(res.getNum(), res.getDocumentNumber(),res.getCreator(), res.getTitle(), res.getDate(), res.getPageNumber(), res.getRemarks())).collect(Collectors.toList()); String recordId = dsvs.get(0).getRecordId(); byte[] imgr = barcodeService.generateBarcodeImage(recordId); ExcelExp e1 = new ExcelExp("卷内目录数据",dsvs, DocumentMaterialsVo.class); ExcelExp e2 = new ExcelExp("卷内数据", list2, recordId, imgr, DocumentMaterialsVoSmall.class); List mysheet = new ArrayList(); mysheet.add(e1); mysheet.add(e2); ExcelUtilManySheet> util2 = new ExcelUtilManySheet>(mysheet); ZipEntry entr = new ZipEntry("卷内目录" + ".xls"); ExcelUtil util1 = new ExcelUtil(DocumentMaterialsVo.class); System.out.println(dsvs); zos.putNextEntry(entr); ByteOutputStream bos1 = new ByteOutputStream(); util2.exportExcelManySheet(bos1, mysheet); // util1.byteOutputStreamExcel(bos1, dsvs,"Date List", ""); bos1.writeTo(zos); //把excel转为pdf boolean res = true; byte[] buf = new byte[1024]; for (DocumentMaterialsVoLarge dc : docs) { String filePath = dc.getUrl(); filePath = filePath.replace("/profile/", RuoYiConfig.getProfile() + "/"); System.out.println(filePath); File tempFile = new File(filePath); //在压缩包中添加文件夹 if(res) { zos.putNextEntry(new ZipEntry("01-申请材料/")); zos.putNextEntry(new ZipEntry("02-办案过程材料/")); zos.putNextEntry(new ZipEntry("03-结论性文件/")); zos.putNextEntry(new ZipEntry("04-其他材料/")); zos.putNextEntry(new ZipEntry("05-档案变更材料/")); zos.putNextEntry(new ZipEntry("06-业务数据/")); res = false; } //得到文件名frontCompWithZore(4, dc.get)+ String fname = frontCompWithZore(4, dc.getFileNumber().intValue())+"-"+dc.getTitle()+"-"+frontCompWithZore(4,dc.getPageNumber().intValue())+"." +dc.getUrl().split("\\.")[1]; if(dc.getStage().equals("01-申请材料")) zos.putNextEntry(new ZipEntry("01-申请材料/"+fname)); else if(dc.getStage().equals("02-办案过程材料")) zos.putNextEntry(new ZipEntry("02-办案过程材料/"+fname)); else if(dc.getStage().equals("03-结论性文件")) zos.putNextEntry(new ZipEntry("03-结论性文件/"+fname)); else if(dc.getStage().equals("04-其他材料")) { zos.putNextEntry(new ZipEntry("04-其他材料/" + fname)); } else if(dc.getStage().equals("05-档案变更材料")) zos.putNextEntry(new ZipEntry("05-档案变更材料/"+fname)); else if(dc.getStage().equals("06-业务数据")) zos.putNextEntry(new ZipEntry("06-业务数据/"+fname)); else zos.putNextEntry(new ZipEntry(fname)); int len; FileInputStream in = new FileInputStream(tempFile); while ((len = in.read(buf)) != -1){ zos.write(buf, 0, len); } // zos.putNextEntry(new ZipEntry("04-其他材料")); zos.closeEntry(); in.close(); } } catch (Exception e) { e.printStackTrace(); System.out.println(e.toString()); throw new Exception("文件打包:"+e.getMessage()); }finally { //关闭流 if(zos != null){ try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } //关闭流 if(os!= null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return file; } }