From 2166c1a14f9629aa49a5f3bb849ce878df4c4892 Mon Sep 17 00:00:00 2001 From: yangbin <yangbin@qq.com> Date: 星期三, 28 八月 2024 17:23:25 +0800 Subject: [PATCH] 2 --- lxzn-module-ai/src/main/java/org/jeecg/modules/utils/PdfTurnPngUtils.java | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 115 insertions(+), 0 deletions(-) diff --git a/lxzn-module-ai/src/main/java/org/jeecg/modules/utils/PdfTurnPngUtils.java b/lxzn-module-ai/src/main/java/org/jeecg/modules/utils/PdfTurnPngUtils.java new file mode 100644 index 0000000..18f3a12 --- /dev/null +++ b/lxzn-module-ai/src/main/java/org/jeecg/modules/utils/PdfTurnPngUtils.java @@ -0,0 +1,115 @@ +package org.jeecg.modules.utils; + +import lombok.extern.slf4j.Slf4j; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.rendering.PDFRenderer; +import org.jeecg.common.util.SHA256Util; +import org.jeecg.modules.ai.vo.PhotoListVo; +import org.jeecg.modules.utils.file.FileUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * @author clown + * * @date 2024/8/1 + */ +@Slf4j +@Component +public class PdfTurnPngUtils { + + private static String filePngPath; + + @Value("${jeecg.path.upload}") + public void setFilePngPath(String filePngPath) { + PdfTurnPngUtils.filePngPath = filePngPath; + } + + + + /** + * 浣跨敤pdfbox灏嗘暣涓猵df杞崲鎴愬浘鐗� + * + * @param fileAddress 鏂囦欢鍦板潃 濡�:C:\\Users\\user\\Desktop\\test + * @param filename PDF鏂囦欢鍚嶄笉甯﹀悗缂�鍚� + * @param type 鍥剧墖绫诲瀷 png 鍜宩pg + */ + public static void pdf2png(String fileAddress, String filename, String type) { + long startTime = System.currentTimeMillis(); + // 灏嗘枃浠跺湴鍧�鍜屾枃浠跺悕鎷兼帴鎴愯矾寰� 娉ㄦ剰锛氱嚎涓婄幆澧冧笉鑳戒娇鐢╘\鎷兼帴 + File file = new File(fileAddress + "\\" + filename + ".pdf"); + try { + // 鍐欏叆鏂囦欢 + PDDocument doc = PDDocument.load(file); + PDFRenderer renderer = new PDFRenderer(doc); + int pageCount = doc.getNumberOfPages(); + for (int i = 0; i < pageCount ; i++) { + // dpi涓�144锛岃秺楂樿秺娓呮櫚锛岃浆鎹㈣秺鎱� + BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI + // 灏嗗浘鐗囧啓鍑哄埌璇ヨ矾寰勪笅 + ImageIO.write(image, type, + new File(fileAddress + "\\澶囦唤\\" + filename + "_" + (i+1) + "." + type)); + } + long endTime = System.currentTimeMillis(); + System.out.println("鍏辫�楁椂锛�" + ((endTime - startTime) / 1000.0) + "绉�"); //杞寲鐢ㄦ椂 + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public static List<PhotoListVo> pdf2pngList(File filePdf, String type,String pathImg){ + PDDocument doc = null; + try { + List<PhotoListVo> files = new ArrayList<>(); + doc = PDDocument.load(filePdf); + String pdfName = FileUtils.getFilenameNonSuffix(filePdf.getName()); + if (doc == null ) { + return null; + } + PDFRenderer renderer = new PDFRenderer(doc); + int pageCount = doc.getNumberOfPages(); + Date currentDate = DateUtil.getNow(); + String monthStr = DateUtil.format(currentDate, DateUtil.STR_YEAR_MONTH); + String relativePath = "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/"+ pathImg + "/"; + //缁濆璺緞 + String absolutePath = filePngPath + "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/" + pathImg + "/"; + for (int i = 0; i < pageCount ; i++) { + PhotoListVo pVo = new PhotoListVo(); + // dpi涓�144锛岃秺楂樿秺娓呮櫚锛岃浆鎹㈣秺鎱� + BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI 144 + // 灏嗗浘鐗囧啓鍑哄埌璇ヨ矾寰勪笅 + //鏂囦欢璺緞 + //鐩稿璺緞 + String encodeFileName = SHA256Util.getSHA256Str(pdfName + "_" + (i+1) + System.currentTimeMillis()); + String imgPath = absolutePath + "/" + encodeFileName + "." + type; + File targetFile = new File(imgPath); + if(!targetFile.getParentFile().exists()){ + targetFile.getParentFile().mkdirs(); + } + ImageIO.write(image, type, targetFile); + pVo.setImgEncodeName(encodeFileName + "." + type); + pVo.setImgName(pdfName + "_" + (i+1)); + pVo.setImgSize(Long.valueOf(new File(imgPath).length())); + pVo.setImgSuffix(type); + pVo.setPageNumber(i+1); + pVo.setImgPath(relativePath); + files.add(pVo); + } + return files; + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + +} -- Gitblit v1.9.3