首页>代码>spring boot整合icepdf实现pdf转图片>/springboot-pdf2Image/src/main/java/com/simon/springbootpdftoimage/util/ImageUtils.java
package com.simon.springbootpdftoimage.util;

import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
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;

/**
 * @author Simon
 * @description: PDF转换为图片的工具类
 */
@Component
public class ImageUtils {

    /**
     * 图片文件格式
     */
    public static final String FORMAT_NAME = "png";
    /**
     * 图片文件后缀名
     */
    public static final String PNG_SUFFIX = ".png";
    /**
     * 压缩文件后缀名
     */
    public static final String ZIP_SUFFIX = ".zip";

    /**
     * 对外的开放接口,用于将PDF文件转换为图片文件压缩包进行下载
     *
     * @param file     SpringMVC获取的图片文件
     * @param response
     * @throws Exception
     */
    public static void pdfToImage(MultipartFile file, HttpServletResponse response) throws Exception {
        File zipFile = generateImageFile(file);
        downloadZipFile(zipFile, response);
    }


    /**
     * 将PDF文件转换为多张图片并放入一个压缩包中
     *
     * @param file SpringMVC获取的图片文件
     * @return 图片文件压缩包
     * @throws Exception 抛出异常
     */
    private static File generateImageFile(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        Document document = new Document();
        document.setByteArray(file.getBytes(), 0, file.getBytes().length, fileName);

        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) + PNG_SUFFIX);
            ImageIO.write(image, FORMAT_NAME, imageFile);
            image.flush();
            fileList.add(imageFile);
        }
        document.dispose();
        String directoryName = fileName.substring(0, fileName.lastIndexOf("."));
        File zipFile = new File(directoryName + ZIP_SUFFIX);
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
        zipFile(fileList, zos);
        zos.close();
        return zipFile;
    }

    /**
     * 下载zip文件
     *
     * @param zipFile  zip压缩文件
     * @param response HttpServletResponse
     * @throws IOException IO异常
     */
    private static void downloadZipFile(File zipFile, HttpServletResponse response) throws IOException {
        FileInputStream fis = new FileInputStream(zipFile);
        byte[] bytes = new byte[fis.available()];
        fis.read(bytes);
        fis.close();

        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8"));
        OutputStream out = response.getOutputStream();
        out.write(bytes);
        out.flush();
        out.close();
        zipFile.delete();
    }

    /**
     * 压缩文件
     *
     * @param inputFiles 具体需要压缩的文件集合
     * @param zos        ZipOutputStream对象
     * @throws IOException IO异常
     */
    private static void zipFile(List<File> inputFiles, ZipOutputStream zos) throws IOException {
        byte[] buffer = new byte[1024];
        for (File file : inputFiles) {
            if (file.exists()) {
                if (file.isFile()) {
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                    zos.putNextEntry(new ZipEntry(file.getName()));
                    int size = 0;
                    while ((size = bis.read(buffer)) > 0) {
                        zos.write(buffer, 0, size);
                    }
                    zos.closeEntry();
                    bis.close();
                    file.delete();
                } else {
                    File[] files = file.listFiles();
                    List<File> childrenFileList = Arrays.asList(files);
                    zipFile(childrenFileList, zos);
                }
            }
        }
    }

}
最近下载更多
做你的景天  LV7 2023年4月12日
364550246  LV15 2023年3月5日
crosa_Don  LV18 2023年3月1日
超维智能编程  LV6 2023年2月26日
最代码官方  LV167 2023年2月19日
最近浏览更多
strongning  LV3 2023年6月1日
szf123  LV12 2023年5月30日
master_guo  LV7 2023年4月12日
做你的景天  LV7 2023年4月12日
interface  LV22 2023年4月5日
8战魂5无双8  LV43 2023年4月1日
随便取个名字_哈哈  LV27 2023年3月29日
Yanxigul 2023年3月28日
暂无贡献等级
舞绝城 2023年3月21日
暂无贡献等级
yyh1252  LV8 2023年3月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友