package com.huatek.busi.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;

@Slf4j
public class PDF2ImageUtil {

    /**
     * 缩放的倍数
     */
    public final static float PDF2IMAGE_ZOOM_NUMBER = 1f;

    /**
     * 缩放后的图片
     */
    public final static String PDF2IMAGE_ZOOM_IMAGE = "jpg";

    /**
     * 将PDF转化为缩略图
     *
     * @param pdf
     * @return
     */
    public static byte[] PDF2Image(byte[] pdf) {
        try {
            BufferedImage image = PDF2ImageUtil(pdf);
            if (image != null) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ImageIO.write(ResizeImageUtil.zoomImage(image, PDF2IMAGE_ZOOM_NUMBER), PDF2IMAGE_ZOOM_IMAGE, out);

                return out.toByteArray();
            } else {
                return null;
            }
        } catch (Exception e) {
            log.info("PDF转换为缩略图失败===================={}", e.getMessage());
        }
        return null;
    }

    /**
     * 将PDF转化为图片
     *
     * @param
     * @return
     */
    public static BufferedImage PDF2ImageUtil(File filePath) {
        BufferedImage combined = null;

        try {
            PDDocument pd = PDDocument.load(filePath);
            combined = PDF2ImageUtil(pd);
            if (pd != null) {
                pd.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("PDF转换为缩略图失败(PDDocument)===================={}", e.getMessage());
        }

        return combined;
    }

    public static BufferedImage PDF2ImageUtil(byte[] filePath) {
        BufferedImage combined = null;

        try {
            PDDocument pd = PDDocument.load(filePath);
            combined = PDF2ImageUtil(pd);
            if (pd != null) {
                pd.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("PDF转换为缩略图失败(PDDocument)===================={}", e.getMessage());
        }

        return combined;
    }

    private static BufferedImage PDF2ImageUtil(PDDocument pd) {
        BufferedImage combined = null;

        try {
            PDFRenderer pdfRenderer = new PDFRenderer(pd);
            for (int page = 0; page < pd.getNumberOfPages(); ++page) {
                BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 96, ImageType.RGB);
                if (page == 0) {
                    combined = bim;
                } else {
                    combined = merge(combined, bim);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("PDF转换为缩略图失败(BufferedImage)===================={}", e.getMessage());
        }

        return combined;
    }

    private static BufferedImage merge(BufferedImage image1, BufferedImage image2) {
        BufferedImage combined = new BufferedImage(
                image1.getWidth(),
                image1.getHeight() + image2.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics g = combined.getGraphics();
        g.drawImage(image1, 0, 0, null);
        g.drawImage(image2, 0, image1.getHeight(), null);
        g.dispose();
        return combined;
    }

}

