You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.3 KiB
Java
67 lines
2.3 KiB
Java
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 fileTitle
|
|
*/
|
|
public static List<ScanAssort> base64StringToPDF(String base64Content, String rootPath, String fileTitle) {
|
|
BASE64Decoder decoder = new BASE64Decoder();
|
|
List<ScanAssort> addScanList = new ArrayList<>();
|
|
try {
|
|
//1.base64编码内容转换为字节数组
|
|
byte[] bytes = decoder.decodeBuffer(base64Content);
|
|
//2.生成jpg图片
|
|
PDDocument 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+ fileTitle +pageIndex+ ".jpg"; // Output file path
|
|
ImageIO.write(image, "jpg", new File(outputFilePath));
|
|
//todo 拼装数据
|
|
ScanAssort scanAssort = new ScanAssort();
|
|
scanAssort.setFileTitle(fileTitle +pageIndex);
|
|
scanAssort.setImagePath(rootPath);
|
|
scanAssort.setFileSource(1);
|
|
scanAssort.setFileStorageType(1);
|
|
scanAssort.setFilePages(1);
|
|
scanAssort.setCreater("检验");
|
|
scanAssort.setCreateTime(new Date());
|
|
scanAssort.setSort(pageIndex);
|
|
|
|
addScanList.add(scanAssort);
|
|
}
|
|
// Close the PDF document
|
|
document.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return addScanList;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |