From 73a816fd2b1e29c25d615c85ce34b12b55c16ccf Mon Sep 17 00:00:00 2001 From: zjh <1084500556@qq.com> Date: 星期一, 20 一月 2025 13:41:45 +0800 Subject: [PATCH] zjh20250120 --- ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java | 75 +++++++++++++++++++++++++++++++------ 1 files changed, 62 insertions(+), 13 deletions(-) diff --git a/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java b/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java index bfb911b..7bef253 100644 --- a/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java +++ b/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java @@ -1,16 +1,19 @@ package com.ltkj.common.utils; -import cn.hutool.json.JSONArray; -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; +import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; +import com.google.zxing.MultiFormatWriter; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; +import org.apache.commons.codec.binary.Base64; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -31,7 +34,7 @@ public static Map<String ,Object> getDocument() throws IOException, DocumentException { // 鍒涘缓涓�涓狣ocument瀵硅薄锛屽苟鎸囧畾A4澶у皬鐨勯〉闈㈠拰杈硅窛 Document document = new Document(PageSize.A4, 30, 30, 30, 30); - // 鏈湴娴嬭瘯鏂囦欢 + // 鏈湴娴嬭瘯鏂囦欢 鎵撳紑娉ㄩ噴鍗冲彲 涓ょ閮藉彲瀛樺湪 // String PDF_FILE = "d:\\Users\\w\\Desktop\\test.pdf"; // PdfWriter.getInstance(document, Files.newOutputStream(Paths.get(PDF_FILE))); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -40,13 +43,13 @@ HashMap<String, Object> map = new HashMap<>(); map.put("document",document); map.put("stream",outputStream); -// map.put("stream",null); return map; } public static Font getFont() throws DocumentException, IOException { - return new Font(BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)); +// return new Font(BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)); + return new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED)); } /** @@ -88,10 +91,28 @@ cell.setBorder(Rectangle.NO_BORDER); table1.addCell(cell); // 鏉″舰鐮� - cell = new PdfPCell(new Phrase(" ", font)); + HashMap<EncodeHintType, Object> map = new HashMap<>(); + map.put(EncodeHintType.CHARACTER_SET,"utf-8"); + BitMatrix encode = null; + try { + encode = new MultiFormatWriter().encode(tjNumber, BarcodeFormat.CODE_128, 300, 100, map); + } catch (WriterException e) { + e.printStackTrace(); + } + BufferedImage toBufferedImage = MatrixToImageWriter.toBufferedImage(encode); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ImageIO.write(toBufferedImage,"png",byteArrayOutputStream); + Image image = Image.getInstance(byteArrayOutputStream.toByteArray()); + cell = new PdfPCell(image,true); cell.setColspan(4); cell.setBorder(Rectangle.NO_BORDER); table1.addCell(cell); + // 娣诲姞涓�琛岀┖鍊� 鎷夊紑鏉″舰鐮佸拰鐢ㄦ埛淇℃伅鐨勮窛绂� + for (int k = 0;k<10;k++){ + cell = new PdfPCell(new Phrase(" ")); + cell.setBorder(Rectangle.NO_BORDER); + table1.addCell(cell); + } break; } else if (i == 1 && j == 5) { // 浣撴鍙� @@ -188,15 +209,18 @@ PdfPCell cell; Font font; font = getFont(); - if (fontStyle != null) + if (fontStyle != null) { font.setStyle(fontStyle); + } // 鏂囧瓧灞呭彸 cell = new PdfPCell(new Phrase(title, font)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); - if (titleCol != null) + if (titleCol != null) { cell.setColspan(titleCol); - if (isCenter) + } + if (isCenter) { cell.setHorizontalAlignment(Element.ALIGN_CENTER); + } cell.setBorder(Rectangle.NO_BORDER); table1.addCell(cell); font = getFont(); @@ -272,4 +296,29 @@ document.close(); return (ByteArrayOutputStream) map.get("stream"); } + + /** + * 灏嗗涓猙ase64pdf鍚堝苟涓轰竴涓� + * @param base64PdfList pdf闆嗗悎 + * @return + */ + public static byte[] mergePDFs(List<String > base64PdfList) throws DocumentException, IOException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + Document document = new Document(); + PdfCopy copy = new PdfCopy(document, byteArrayOutputStream); + document.open(); + for (String base64Pdf : base64PdfList) { + byte[] pdfBytes = Base64.decodeBase64(base64Pdf); + PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfBytes)); + int numberOfPages = reader.getNumberOfPages(); + for (int i = 1; i <= numberOfPages; i++) { + document.newPage(); + PdfImportedPage page = copy.getImportedPage(reader, i); + copy.addPage(page); + } + reader.close(); + } + document.close(); + return byteArrayOutputStream.toByteArray(); + } } -- Gitblit v1.8.0