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;
|
}
|
|
|
}
|