1
yangbin
2024-08-15 fc38e2635216775a80210d0df109dc6174d66813
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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将整个pdf转换成图片
     *
     * @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test
     * @param filename    PDF文件名不带后缀名
     * @param type        图片类型 png 和jpg
     */
    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;
    }
 
 
}