引入所需maven依赖
<!-- PDF转图片 -->
<dependency>
<groupId>org.icepdf.os</groupId>
<artifactId>icepdf-core</artifactId>
<version>6.2.2</version>
<exclusions>
<exclusion>
<groupId>javax.media</groupId>
<artifactId>jai_core</artifactId>
</exclusion>
</exclusions>
</dependency>
JAVA代码工具类
package com.util;
import com.lowagie.text.pdf.PdfReader;
import lombok.extern.slf4j.Slf4j;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
//java 项目 www.fhadmin.org
@Slf4j
public class PdfToImageUtil {
//支持文件格式
public static final String SUPPORT_FILE = "pdf";
//图片文件格式
public static final String IMAGE_SUFFIX = "jpg"; //png
//压缩文件格式
public static final String ZIP_SUFFIX = "zip";
//PDF是否为一页
private static boolean isImage;
/**
* 对外的开放接口,用于将PDF文件转换为图片文件压缩包进行下载
* @param file SpringMVC获取的图片文件
*/
public static synchronized void pdfToTransformation(MultipartFile file, HttpServletResponse response) throws Exception {
String fileName = file.getOriginalFilename();
if(null == fileName) return;
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
log.info("文件名称:" + fileName + ",文件后缀:" + suffix);
if(!SUPPORT_FILE.equals(suffix)) return;
isImage = false;
File imageFile = generateFile(file);
log.info("文件生成成功!");
downloadFile(imageFile, response);
}
/**
* 将PDF文件转换为多张图片并放入一个压缩包中
* @param file SpringMVC获取的图片文件
* @return 图片文件压缩包
* @throws Exception 抛出异常
*/
private static File generateFile(MultipartFile file) throws Exception {
String fileName = file.getOriginalFilename();
if(null == fileName) return null;
Document document = new Document();
document.setByteArray(file.getBytes(), 0, file.getBytes().length, fileName);
log.info("PDF页数:" + document.getNumberOfPages());
isImage = 1 == document.getNumberOfPages();
File imageReturnFile = null;
List<File> fileList = new ArrayList<>();
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
Page.BOUNDARY_CROPBOX, 0F, 2.5F);
File imageFile = new File((i + 1) + "." + IMAGE_SUFFIX);
ImageIO.write(image, IMAGE_SUFFIX, imageFile);
image.flush();
if(isImage){
imageReturnFile = imageFile;
break;
}
fileList.add(imageFile);
}
document.dispose();
if(isImage) return imageReturnFile;
//压缩图片文件
String directoryName = fileName.substring(0, fileName.lastIndexOf("."));
File zipFile = new File(directoryName + "." + ZIP_SUFFIX);
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
zipFile(fileList, zipOutputStream);
zipOutputStream.close();
return zipFile;
}
/**
* 下载image/zip文件
* @param downloadFile 文件
* @param response HttpServletResponse
* @throws IOException IO异常
*/
private static void downloadFile(File downloadFile, HttpServletResponse response) throws IOException {
FileInputStream fileInputStream = new FileInputStream(downloadFile);
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
fileInputStream.close();
//设置response参数
response.reset();
if(isImage){
response.setContentType("image/jpeg");
} else {
response.setContentType("application/zip");
}
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(downloadFile.getName(), "UTF-8"));
OutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
if(!isImage) downloadFile.delete();
}
/**
* 压缩文件
* @param inputFiles 具体需要压缩的文件集合
* @param zipOutputStream ZipOutputStream对象
* @throws IOException IO异常
*/
private static void zipFile(List<File> inputFiles, ZipOutputStream zipOutputStream) throws IOException {
byte[] buffer = new byte[1024];
for (File file : inputFiles) {
if (file.exists()) {
if (file.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
int size = 0;
while ((size = bis.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, size);
}
zipOutputStream.closeEntry();
bis.close();
file.delete();
} else {
File[] files = file.listFiles();
if(null == files) continue;
List<File> childrenFileList = Arrays.asList(files);
zipFile(childrenFileList, zipOutputStream);
}
}
}
}
/**
* 获取PDF页数
* @throws IOException
*/
public static String getPdfPageSize(MultipartFile file) throws IOException {
PdfReader pdfReader = new PdfReader(file.getBytes());
int pages = pdfReader.getNumberOfPages();
return String.valueOf(pages);
}
}
生成图片PdfController.java
package com.web.transformation.controller;
import com.util.PdfToImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//java项目 www.fhadmin.org
@Controller
public class PdfController {
//PDF转图片或者ZIP
@RequestMapping("/pdfToImage")
@ResponseBody
public void pdfToImage(MultipartFile file, HttpServletResponse response) throws Exception{
PdfToImageUtil.pdfToTransformation(file,response);
}
//获取PDF页数
@RequestMapping("/getPdfPageSize")
@ResponseBody
public String pdfToImage(MultipartFile file) throws IOException {
return PdfToImageUtil.getPdfPageSize(file);
}
}