From af47b774448bfa3ece4741664ce4d24128c8f629 Mon Sep 17 00:00:00 2001 From: fei <791364011@qq.com> Date: 星期一, 01 九月 2025 14:29:50 +0800 Subject: [PATCH] 新增了代码 --- archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java | 423 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 423 insertions(+), 0 deletions(-) diff --git a/archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java b/archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java index 723b026..c1081b0 100644 --- a/archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java +++ b/archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java @@ -11,18 +11,31 @@ 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.domain.vo.DocumentMaterialFileStyle; +import com.ruoyi.domain.vo.DocumentMaterialsVo; +import com.ruoyi.domain.vo.DocumentMaterialsVoSmall; +import com.ruoyi.service.IDocumentMaterialsService; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.imageio.ImageIO; import java.awt.*; +import java.awt.Color; import java.awt.image.BufferedImage; import java.io.*; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.util.List; @Service public class pdfGenerateService { @Autowired private BarcodeService barcodeService; + @Autowired + private IDocumentMaterialsService documentMaterialsService; //鐢熶骇浜岀淮鐮� public byte[] createQrCodeN(String content, int width, int height) throws IOException { QrConfig config = new QrConfig(width, height); @@ -50,7 +63,417 @@ return os.toByteArray(); } + private void handleXSSFImages(XSSFSheet sheet, Document pdfDoc) throws Exception { + XSSFDrawing drawing = sheet.getDrawingPatriarch(); + if (drawing == null) return; + for (XSSFShape shape : drawing.getShapes()) { + if (shape instanceof XSSFPicture) { + XSSFPicture pic = (XSSFPicture)shape; + XSSFClientAnchor anchor = pic.getPreferredSize(); + + // 鑾峰彇鍥剧墖鏁版嵁 + byte[] bytes = pic.getPictureData().getData(); + Image pdfImage = Image.getInstance(bytes); + + // 璁剧疆鍥剧墖浣嶇疆鍜屽ぇ灏� + float imageWidth = pdfImage.getWidth(); + float imageHeight = pdfImage.getHeight(); + float scale = Math.min( + (PageSize.A4.getWidth() - 100) / imageWidth, + (PageSize.A4.getHeight() - 100) / imageHeight + ); + pdfImage.scaleAbsolute(imageWidth * scale, imageHeight * scale); + + // 娣诲姞鍥剧墖鍒癙DF + pdfDoc.add(pdfImage); + pdfDoc.add(Chunk.NEWLINE); + } + } + } + + + + + private static String getCellValue(Cell cell,FormulaEvaluator evaluator) { + // System.out.println(cell); + if (cell == null) return ""; + CellValue cellValue = evaluator.evaluate(cell); + if(cellValue==null) + return ""; + // System.out.println(cellValue.getCellType()); + switch (cellValue.getCellType()) { + case STRING: + return cell.getStringCellValue(); + case NUMERIC: + // 鑾峰彇鏁板�煎苟杞负鏁村瀷 + double numericValue = cell.getNumericCellValue(); + // 鍒ゆ柇鏄惁涓烘暣鏁� + if (numericValue == (int)numericValue) { + if((int)numericValue==0) + return ""; + else + return String.valueOf((int)numericValue); + } else { + return String.valueOf(numericValue); // 鎴栬�呮牴鎹渶瑕佽繑鍥炲洓鑸嶄簲鍏ョ殑鏁村瀷 + } + + case BOOLEAN: + return String.valueOf(cell.getBooleanCellValue()); + case FORMULA: + + return cell.getCellFormula()+""; + case BLANK: + return ""; + default: + return ""; + } + } + private int getRealLastCellNum(Row row) { + int lastCellNum = 0; + for (int i = 0; i < row.getLastCellNum(); i++) { + Cell cell = row.getCell(i); + if (cell != null && cell.getCellType() != CellType.BLANK) { + lastCellNum = i + 1; + } + } + return lastCellNum; + } + + // + void generateSimpleExample() + { + +// try { +// // 1. 璇诲彇Excel鏂囦欢 +// String excelPath = RuoYiConfig.getProfile() + "/download/"+"鍗峰収鐩綍.xls"; +// String pdfPath = RuoYiConfig.getProfile() + "/download/"+"1.pdf"; +// +// Workbook workbook = WorkbookFactory.create(new File(excelPath)); +// FileOutputStream fos = new FileOutputStream(pdfPath); +// FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); +// +// +// // 鑾峰彇绗竴涓伐浣滆〃 +// Sheet sheet = workbook.getSheetAt(1); +// +// // 鍒涘缓PDF鏂囨。瀵硅薄 +// Document document = new Document(PageSize.A2, 50, 50, 50, 50); +// +// // 鍒涘缓PDF杈撳嚭娴� +// PdfWriter writer = PdfWriter.getInstance(document, fos); +// // 4. 澶勭悊宸ヤ綔琛ㄤ腑鐨勫浘鐗� +// if (workbook instanceof XSSFWorkbook) { +// handleXSSFImages((XSSFSheet)sheet, document); +// } +// // 鎵撳紑PDF鏂囨。 +// document.open(); +// +// // 鍒涘缓PDF琛ㄦ牸瀵硅薄 +// System.out.println(sheet.getRow(0).getLastCellNum()+"--00"); +// PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum()); +// table.setHeaderRows(1); +// +// // 璁剧疆琛ㄦ牸瀹藉害 +// table.setWidthPercentage(100); +// +// // 璁剧疆琛ㄦ牸鏍囬 +// Paragraph title = new Paragraph(sheet.getSheetName(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD)); +// title.setAlignment(Element.ALIGN_CENTER); +// document.add(title); +// // System.out.println(sheet.) +// // 娣诲姞琛ㄦ牸鍐呭 +// for (Row row : sheet) { +// { +// if (row == null) continue; +// System.out.println(getRealLastCellNum(row)+"---987"); +//// if(row.getRowNum()==0) +//// continue; +// if(row.getRowNum()==2) +// { +// PdfPCell pdfCell = ImageSet(100); +// pdfCell.setColspan(7); +// pdfCell.setRowspan(3); +// pdfCell.setBorderWidth(1f); +// pdfCell.setBorderColor(BaseColor.BLACK); +// pdfCell.setPadding(5f); +// pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER); +// pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE); +// table.addCell(pdfCell); +// System.out.println("{{{{{{{{{{{{{"); +// continue; +// } +// for (int i = 0; i < 7; i++) +// { +// +// Cell cell = row.getCell(i); +// if(row.getRowNum()==3) +// { +// PdfPCell pdfCell = new PdfPCell(new Paragraph("鍗峰唴鐩綍", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12))); +// pdfCell.setColspan(7); +// pdfCell.setBorderWidth(1f); +// pdfCell.setBorderColor(BaseColor.BLACK); +// pdfCell.setPadding(5f); +// pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER); +// pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE); +// table.addCell(pdfCell); +// System.out.println("{{{{{{{{{{{{{"); +// break; +// } +// if(cell==null) { +// +// table.addCell(""); +// continue; +// } +// PdfPCell pdfCell = new PdfPCell(new Paragraph(getCellValue(cell, evaluator), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12))); +// +// if(row.getRowNum()!=1) { +// pdfCell.setBorderWidth(1f); +// pdfCell.setBorderColor(BaseColor.BLACK); +// pdfCell.setPadding(5f); +// pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER); +// pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE); +// } +// if (cell.getRowIndex() == 5) { +// pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY); +// } +// +// table.addCell(pdfCell); +// +// } +// } +// } +// +// // 娣诲姞琛ㄦ牸鍒癙DF鏂囨。 +// table.setSpacingBefore(20f); +// table.setSpacingAfter(20f); +// table.setKeepTogether(true); +// document.add(table); +// +// // 鍏抽棴PDF鏂囨。 +// document.close(); +// +// } catch (Exception e) { +// e.printStackTrace(); +// } + + } + + + //瀵煎嚭鍗峰唴鐩綍鐨刾df + public void generateFileDirectoryPdf(String pdfPath,List<DocumentMaterialsVo> dvss) throws DocumentException, IOException { + Document document = new Document(); + PdfWriter.getInstance(document, new FileOutputStream(pdfPath)); + document.open(); + + // 鍒涘缓琛ㄦ牸锛�5鍒楋級 + PdfPTable table = new PdfPTable(7); + + // 璁剧疆琛ㄦ牸瀹藉害锛堝崰椤甸潰瀹藉害鐨�100%锛� + table.setWidthPercentage(100); + + + //娣诲姞鏉″舰鐮� + String volumeNumber = dvss.get(0).getRecordId(); + Image img = Image.getInstance(barcodeService.generateBarcodeImage(volumeNumber)); + // 璁剧疆鍥剧墖鍦≒DF涓殑浣嶇疆锛堝彲閫夛級 +// img.setAbsolutePosition(100, 100); + // 灏嗗浘鐗囨坊鍔犲埌PDF鏂囨。涓� + PdfPCell pdfPCell = new PdfPCell(img); + pdfPCell.setBorder(Rectangle.NO_BORDER); // 绉婚櫎鍗曞厓鏍艰竟妗� + + pdfPCell.setMinimumHeight(40); + pdfPCell.setUseAscender(true); // 璁剧疆鍙互灞呬腑 + pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); // 璁剧疆姘村钩灞呬腑 + pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 璁剧疆鍨傜洿灞呬腑 + pdfPCell.setColspan(2); + pdfPCell.setPaddingBottom(30); +// 鍒涘缓琛ㄦ牸骞惰缃垪瀹芥瘮渚� + float[] columnWidths = {35f, 65f}; // 绗竴鍒�30%锛岀浜屽垪70% + PdfPTable table1 = new PdfPTable(columnWidths); +// PdfPTable table = new PdfPTable(2); + table1.setWidthPercentage(80); // 澧炲ぇ琛ㄦ牸瀹藉害鐧惧垎姣� + table1.setHorizontalAlignment(Element.ALIGN_LEFT); // 璁剧疆琛ㄦ牸鏁翠綋灞呬腑 + table1.setSpacingBefore(30f); // 璁剧疆琛ㄦ牸鍓嶉棿璺� + + table1.addCell(pdfPCell); + + document.add(table1); + + + // 娣诲姞琛ㄥご + BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); + Font chineseFont = new Font(bfChinese, 12); + Font chineseFont1 = new Font(bfChinese, 16, Font.BOLD); + // 娣诲姞鏍囬 + Paragraph title = new Paragraph("鍗� 鍐� 鐩� 褰�", chineseFont1); + title.setAlignment(Element.ALIGN_CENTER); + document.add(title); + Paragraph withNewLine = new Paragraph("\n"); + document.add(withNewLine); + document.add(withNewLine); + + + //娣诲姞鍗峰彿 + Paragraph recordInfo = new Paragraph("鍗峰彿锛�" + volumeNumber, chineseFont); + recordInfo.setAlignment(Element.ALIGN_RIGHT); + document.add(recordInfo); + + document.add(withNewLine); + document.add(withNewLine); + String[] headers = {"搴忓彿", "鏂囦欢缂栧彿", "璐d换鑰�", "鏂囦欢棰樺悕", "鏃ユ湡", "椤靛彿", "澶囨敞"}; + for (String header : headers) { + PdfPCell cell = new PdfPCell(new Paragraph(header, + chineseFont1)); + cell.setHorizontalAlignment(Element.ALIGN_CENTER); + cell.setBackgroundColor(new BaseColor(200, 200, 200)); + table.addCell(cell); + } + + // 娣诲姞琛ㄦ牸鏁版嵁 +// Object[][] data = { +// // {1, "鍟嗗搧A", 2, 100.00, 200.00}, +// // {2, "鍟嗗搧B", 1, 250.50, 250.50}, +// // {3, "鍟嗗搧C", 3, 80.00, 240.00} +// }; +// data. + // for (Object[] row : data) { + for (DocumentMaterialsVo cellData : dvss) { + //搴忓彿 + PdfPCell cell = new PdfPCell(new Paragraph(cellData.getNum().toString(), + chineseFont)); + cell.setHorizontalAlignment(Element.ALIGN_CENTER); + cell.setVerticalAlignment(Element.ALIGN_MIDDLE); + table.addCell(cell); + + + //搴忓彿 + PdfPCell cell1 = new PdfPCell(new Paragraph(cellData.getDocumentNumber()==null?"":cellData.getDocumentNumber().toString(), + chineseFont)); + cell1.setHorizontalAlignment(Element.ALIGN_CENTER); + cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); + + table.addCell(cell1); + + + //搴忓彿 + PdfPCell cell2 = new PdfPCell(new Paragraph(cellData.getCreator()==null?"":cellData.getCreator().toString(), + chineseFont)); + cell2.setHorizontalAlignment(Element.ALIGN_CENTER); + cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); + + table.addCell(cell2); + + + //搴忓彿 + PdfPCell cell3 = new PdfPCell(new Paragraph(cellData.getTitle()==null?"":cellData.getTitle().toString(), + chineseFont)); + cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); + + cell3.setHorizontalAlignment(Element.ALIGN_CENTER); + table.addCell(cell3); + + //搴忓彿 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + // String dateString = sdf.format(date); + PdfPCell cell4 = new PdfPCell(new Paragraph(cellData.getDate()==null?"":sdf.format(cellData.getDate()), + chineseFont)); + cell4.setVerticalAlignment(Element.ALIGN_MIDDLE); + + cell4.setHorizontalAlignment(Element.ALIGN_CENTER); + table.addCell(cell4); + + + //搴忓彿 + PdfPCell cell5 = new PdfPCell(new Paragraph(cellData.getPageNumber()==null?"": cellData.getPageNumber().toString(), + chineseFont)); + cell5.setHorizontalAlignment(Element.ALIGN_CENTER); + cell5.setVerticalAlignment(Element.ALIGN_MIDDLE); + + table.addCell(cell5); + + //搴忓彿 + PdfPCell cell6 = new PdfPCell(new Paragraph(cellData.getRemarks()==null?"":cellData.getRemarks().toString(), + chineseFont)); + cell6.setVerticalAlignment(Element.ALIGN_MIDDLE); + + cell6.setHorizontalAlignment(Element.ALIGN_CENTER); + table.addCell(cell6); + } + // } + + document.add(table); + document.close(); + } + //璁剧疆澶囪�冭〃 + public void generateFileStyleInfo(String pdfPath, String volumeNumber, Long id) throws IOException, DocumentException { + Document document = new Document(); + PdfWriter.getInstance(document, new FileOutputStream(pdfPath)); + document.open(); + +// 鍒涘缓PDF鏂囨。 + List<DocumentMaterialFileStyle> dmfs = documentMaterialsService.findFileStyleInfo(Math.toIntExact(id)); + PdfWriter.getInstance(document, new FileOutputStream("09-澶囪�冭〃"+".pdf")); + document.open(); + // 璁剧疆涓枃瀛椾綋 + + BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); + Font chineseFont = new Font(bfChinese, 16, Font.BOLD); + // 娣诲姞鏍囬 + Paragraph title = new Paragraph("鍗峰唴澶囪�冭〃", chineseFont); + title.setAlignment(Element.ALIGN_CENTER); + document.add(title); + + // 娣诲姞鍐呭 + // String volumeNumber = "D3.4.1-05-2024-0002"; + Paragraph recordInfo = new Paragraph("鍗峰彿锛�" + volumeNumber, chineseFont); + recordInfo.setAlignment(Element.ALIGN_RIGHT); + document.add(recordInfo); + int allCnt = dmfs.get(0).getCnt() + dmfs.get(1).getCnt() + dmfs.get(2).getCnt(); + document.add(new Paragraph(" 鏈� 妗� 鍗� 鍏� 鏈� 鏂� 浠� 鏉� 鏂� " + allCnt + " 椤�"+"锛屽叾涓細鏂囧瓧鏉愭枡 " + + dmfs.get(1).getCnt() + " 椤�"+"锛屽浘鏍锋潗鏂� " + dmfs.get(2).getCnt() + " 椤�"+"锛岀収鐗� " + dmfs.get(0).getCnt() + " 寮�", chineseFont)); + + + document.add(new Paragraph("璇存槑: ", chineseFont)); + // 鏂规硶2锛氫娇鐢ㄥ甫鎹㈣绗︾殑Paragraph + for(int i = 0; i < 10; i++) { + Paragraph withNewLine = new Paragraph("\n"); + document.add(withNewLine); + } + + + String tis1 = "绔嬪嵎浜猴細浠囩縺锛堝箍宸炵泩瀹舵。妗堢鐞嗘湁闄愬叕鍙革級"; + Paragraph par1 = new Paragraph(tis1, chineseFont); + par1.setAlignment(Element.ALIGN_RIGHT); + document.add(par1); + //鎷垮埌褰撳墠鏃堕棿 + // 鑾峰彇褰撳墠鏃ユ湡 + LocalDate currentDate = LocalDate.now(); + + // 鎵撳嵃骞淬�佹湀銆佹棩 +// System.out.println("Year: " + currentDate.getYear()); +// System.out.println("Month: " + currentDate.getMonthValue()); // 鏈堜唤鏄粠1寮�濮嬬殑 +// System.out.println("Day of Month: " + currentDate.getDayOfMonth()); + Paragraph withNewLine = new Paragraph("\n"); + document.add(withNewLine); + String cdt = currentDate.getYear()+"骞�"+currentDate.getMonthValue()+"鏈�"+currentDate.getDayOfMonth()+"鏃�"; + + + Paragraph par3 = new Paragraph(cdt, chineseFont); + par3.setAlignment(Element.ALIGN_RIGHT); + document.add(par3); + String tis2 = "瀹℃牳浜猴細鏇剧憺鑾癸紙骞垮窞鐩堝妗f绠$悊鏈夐檺鍏徃锛�"; + document.add(withNewLine); + + Paragraph par2 = new Paragraph(tis2, chineseFont); + par2.setAlignment(Element.ALIGN_RIGHT); + document.add(par2); + document.add(withNewLine); + document.add(par3); + + document.close(); + + } public void generatePdf(String pdfPath) throws IOException, DocumentException { -- Gitblit v1.9.1