package com.dk.mdm.infrastructure.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.print.PrintService;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.io.File;

@Slf4j
@SpringBootApplication
public class PrintApplication {

    public static void main(String[] args) {
        SpringApplication.run(PrintApplication.class, args);
    }

    /**
     * @desc   : 打印方法，传入文件位置，以及打印设备的名称
     * @author : 张潇木
     * @date   : 2022/11/16 14:16
     */
    public static PDDocument printPdf(String pdfPath, String printerName) throws Exception {
        File file = new File(pdfPath);
        PDDocument document = null;
        RuntimeException runtimeException = null;
        try {
            document = PDDocument.load(file);
            PrinterJob job = getPrintServiceByName(printerName);
            setPageStyle(document, job);
            // 开始打印
            job.print();
        }catch (RuntimeException e){
            e.printStackTrace();
            runtimeException = e;
        }finally {
            if (document!=null){
                document.close();
            }
        }

        if (runtimeException!=null) {
            throw runtimeException;
        }

        return document;
    }

    /**
     * @desc   : 获取PrinterJob
     * @author : 张潇木
     * @date   : 2022/11/16 14:16
     */
    public static PrinterJob getPrintServiceByName(String printerName) throws Exception{
        PrinterJob job = PrinterJob.getPrinterJob();
        // 遍历查询打印机名称
        boolean flag = false;
        for (PrintService ps : PrinterJob.lookupPrintServices()) {
            String psName = ps.toString();
            log.info("设备名称=>{}",psName);
            // 选用指定打印机，需要精确查询打印机就用equals，模糊查询用contains
            if (psName.contains(printerName)) {
                flag = true;
                job.setPrintService(ps);
                break;
            }
        }
        if(!flag){
            throw new RuntimeException("打印失败，未找到名称为" + printerName + "的打印机，请检查。");
        }
        return job;
    }

    /**
     * @desc   : 设置PrinterJob纸张样式
     * @author : 张潇木
     * @date   : 2022/11/16 14:16
     */
    public static void setPageStyle(PDDocument document, PrinterJob job) {
        job.setPageable(new PDFPageable(document));
        Paper paper = new Paper();
        //pdf的单位是pt 72pt=1英寸， 而A4纸宽8.27英寸。高11.69英寸，故而得出A4的纸张大小
        int width = (int) (72*8.27);
        int height = (int) (72*11.69);
        // 设置打印纸张大小
        paper.setSize(width,height); // 1/72 inch
        // 设置边距，单位是像素，10mm边距，对应 28px
        int marginLeft = 0;
        int marginRight = 0;
        int marginTop = 0;
        int marginBottom = 0;
        // 设置打印位置 坐标
        paper.setImageableArea(marginLeft, marginRight, width - (marginLeft + marginRight), height - (marginTop + marginBottom));
        // custom page format
        PageFormat pageFormat = new PageFormat();
        pageFormat.setPaper(paper);
        // override the page format
        Book book = new Book();
        // append all pages 设置一些属性 是否缩放 打印张数等
        book.append(new PDFPrintable(document, Scaling.ACTUAL_SIZE), pageFormat, 1);
        job.setPageable(book);
    }



}
