package com.zt.plat.module.qms.framework.gridreport;

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

import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfImageType;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PdfHelper {

    /**
     * 修改pdf属性
     * @param pdfBytes pdf
     * @param author 作者
     * @return
     */
    public static byte[] modifyDocInfo(byte[] pdfBytes) {
        return modifyDocInfo(pdfBytes, null, null, null, null, null, null);
    }
    
    /**
     * 修改pdf属性
     * @param pdfBytes pdf
     * @param author 作者
     * @return
     */
    public static byte[] modifyDocInfo(byte[] pdfBytes, String author) {
        return modifyDocInfo(pdfBytes, null, author, null, null, null, null);
    }
    
    /**
     * 修改pdf属性
     * @param pdfBytes pdf
     * @param author 作者
     * @param subject 主题
     * @return
     */
    public static byte[] modifyDocInfo(byte[] pdfBytes, String author, String subject) {
        return modifyDocInfo(pdfBytes, null, author, subject, null, null, null);
    }
    
    /**
     * 修改pdf属性
     * @param pdfBytes pdf
     * @param title 标题
     * @param author 作者
     * @param subject 主题
     * @param keywords 关键字
     * @param creator 应用程序
     * @param producer 制作程序
     * @return
     */
    public static byte[] modifyDocInfo(byte[] pdfBytes, String title, String author, String subject, String keywords, String creator, String producer) {
        //加载PDF文件
        try (PDDocument pdfDocument = PDDocument.load(pdfBytes); ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
            //属性信息
            PDDocumentInformation info = pdfDocument.getDocumentInformation();
            //标题
            if (StringUtils.isNotBlank(title)) {
                info.setTitle(title);
            }
            
            //作者
            if (StringUtils.isNotBlank(author)) {
                info.setAuthor(author);
            } else {
                info.setAuthor("王兴荣<wxr@wangxingrong.com>");
            }
            
            //主题
            if (StringUtils.isNotBlank(subject)) {
                info.setSubject(subject);
            } else {
                info.setSubject("志者竟成报告系统");
            }
            
            //关键字
            if (StringUtils.isNotBlank(keywords)) {
                info.setKeywords(keywords);
            }
            
            //应用程序
            if (StringUtils.isNotBlank(creator)) {
                info.setCreator(creator);
            } else {
                info.setCreator("志者竟成报告系统");
            }
            
            //pdf制作程序
            if (StringUtils.isNotBlank(producer)) {
                info.setProducer(producer);
            } else {
                info.setProducer("志者竟成报告系统V1.0");
            }            
            //将修改后的PDF保存
            pdfDocument.save(baos);
            return baos.toByteArray();
        } catch (Exception e) {
            log.error("处理pdf出错！", e);
        }
        return null;
    }

    /**
     * pdf转png图片
     * @param pdfBytes pdf
     * @param dpi DPI
     * @return
     */
    public static byte[] pdfToPng(byte[] pdfBytes, int dpi) {
        long startTime = System.nanoTime();
        //加载PDF文件
        PdfDocument pdfDocument = new PdfDocument();
        pdfDocument.loadFromBytes(pdfBytes);
        try ( ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
        	BufferedImage bufferedImage = pdfDocument.saveAsImage(0, PdfImageType.Bitmap, dpi, dpi);
			ImageIO.write(bufferedImage, "PNG", baos);
	        long endTime = System.nanoTime();
	        log.info("png转换用时：{}ms", (endTime - startTime)/1000000);
            return baos.toByteArray();
        } catch (Exception e) {
            log.error("处理pdf出错！");
        }
        return null;
	}
    
}
