|
|
|
|
package com.docus.demo.utils;
|
|
|
|
|
|
|
|
|
|
import com.docus.demo.entity.ScanAssort;
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class PDFFileUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* pdf拆分图片方法
|
|
|
|
|
* @param base64Content
|
|
|
|
|
* @param rootPath
|
|
|
|
|
* @param repId
|
|
|
|
|
*/
|
|
|
|
|
public static List<ScanAssort> base64StringToPDF(String base64Content, String rootPath, String repId,String fileTitle,int index) throws IOException {
|
|
|
|
|
BASE64Decoder decoder = new BASE64Decoder();
|
|
|
|
|
List<ScanAssort> addScanList = new ArrayList<>();
|
|
|
|
|
PDDocument document =null ;
|
|
|
|
|
try {
|
|
|
|
|
//1.base64编码内容转换为字节数组
|
|
|
|
|
byte[] bytes = decoder.decodeBuffer(base64Content);
|
|
|
|
|
//2.生成jpg图片
|
|
|
|
|
document = PDDocument.load(new ByteArrayInputStream(bytes));
|
|
|
|
|
PDFRenderer renderer = new PDFRenderer(document);
|
|
|
|
|
// Iterate over each page and save it as an image
|
|
|
|
|
for (int pageIndex = 0; pageIndex < document.getNumberOfPages(); pageIndex++) {
|
|
|
|
|
// Render the page as an image
|
|
|
|
|
BufferedImage image = renderer.renderImageWithDPI(pageIndex, 300); // Set DPI value as needed
|
|
|
|
|
// Save the image to a file
|
|
|
|
|
String outputFilePath = rootPath +File.separator+ repId +pageIndex+ ".jpg"; // Output file path
|
|
|
|
|
//判断父级目录是否存在 不存在需要创建
|
|
|
|
|
File file = new File(outputFilePath);
|
|
|
|
|
String parentPath = file.getParent();
|
|
|
|
|
File dir = new File(parentPath);
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
dir.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
ImageIO.write(image, "jpg", new File(outputFilePath));
|
|
|
|
|
|
|
|
|
|
ScanAssort scanAssort = new ScanAssort();
|
|
|
|
|
scanAssort.setFileTitle(fileTitle);
|
|
|
|
|
scanAssort.setScanPage(repId +pageIndex+ ".jpg");
|
|
|
|
|
scanAssort.setImagePath(rootPath);
|
|
|
|
|
scanAssort.setFileSource(2);
|
|
|
|
|
scanAssort.setSource("jy");
|
|
|
|
|
scanAssort.setFileStorageType(1);
|
|
|
|
|
scanAssort.setFilePages(1);
|
|
|
|
|
scanAssort.setCreater("jianyan");
|
|
|
|
|
scanAssort.setCreaterName("检验");
|
|
|
|
|
scanAssort.setCreateTime(new Date());
|
|
|
|
|
scanAssort.setSort((index+1)*1000+pageIndex);
|
|
|
|
|
scanAssort.setAssortId("A5A7AA6796D1715A2F1E35699C706C84");
|
|
|
|
|
scanAssort.setTaskId("-1");
|
|
|
|
|
|
|
|
|
|
addScanList.add(scanAssort);
|
|
|
|
|
}
|
|
|
|
|
// Close the PDF document
|
|
|
|
|
|
|
|
|
|
}finally {
|
|
|
|
|
|
|
|
|
|
if (document!=null){
|
|
|
|
|
document.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return addScanList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|