package com.artfess.rescue.utils;

import com.aspose.cells.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * excel转pdf
 * @description：xlsx转pdf
 */
public class ExcelToPdfUtil {
    /**
     * 获取license 去除水印
     * @return
     */


    /**
     * excel 转为pdf 输出。
     *
     * @param sourceFilePath  excel文件
     * @param desFilePathd  pad 输出文件目录
     */
    public static void excelToPdf(String sourceFilePath, String desFilePathd){
        try {
            IndividualFontConfigs configs = new IndividualFontConfigs();
            configs.setFontFolder("/usr/share/fonts/chinese", true);
            LoadOptions loadOptions  = new LoadOptions();
            loadOptions.setFontConfigs(configs);
            Workbook wb = new Workbook(sourceFilePath);// 原始excel路径

            FileOutputStream fileOS = new FileOutputStream(desFilePathd);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
            int[] autoDrawSheets={3};
            //当excel中对应的sheet页宽度太大时，在PDF中会拆断并分页。此处等比缩放。
            autoDraw(wb,autoDrawSheets);
            int[] showSheets={0};
            //隐藏workbook中不需要的sheet页。
            printSheetPage(wb,showSheets);
            wb.save(fileOS, pdfSaveOptions);
            fileOS.flush();
            fileOS.close();
            //System.out.println("转换PDF完毕！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void excelToPdf(Workbook wb, String desFilePathd){
        try {
            FileOutputStream fileOS = new FileOutputStream(desFilePathd);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
            pdfSaveOptions.setOnePagePerSheet(true);
            int[] autoDrawSheets={3};
            //当excel中对应的sheet页宽度太大时，在PDF中会拆断并分页。此处等比缩放。
            autoDraw(wb,autoDrawSheets);
            int[] showSheets={0};
            //隐藏workbook中不需要的sheet页。
            printSheetPage(wb,showSheets);
            wb.save(fileOS, pdfSaveOptions);
            fileOS.flush();
            fileOS.close();
            //System.out.println("转换PDF完毕！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * excel 转为pdf 输出。
     *
     * @param sourceFilePath excel文件
     * @param desFilePathd   pad 输出文件目录
     * @param showSheet      是否需要隐藏workbook中不需要的sheet页。
     */
    public static void excelToPdf(String sourceFilePath, String desFilePathd, Boolean showSheet) {
        try {
            File file = new File(sourceFilePath);
            InputStream in = Files.newInputStream(file.toPath());
            Workbook wb = new Workbook(in);
        if (showSheet) {
            excelToPdf(wb, desFilePathd);
        } else {

                IndividualFontConfigs configs = new IndividualFontConfigs();
                configs.setFontFolder("/usr/share/fonts/chinese", true);
                LoadOptions loadOptions = new LoadOptions();
                loadOptions.setFontConfigs(configs);
                // 原始excel路径
                // Workbook wb = new Workbook(sourceFilePath);
                FileOutputStream fileOS = new FileOutputStream(desFilePathd);
                PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
                pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
                pdfSaveOptions.setOnePagePerSheet(true);
                int[] autoDrawSheets = {0,1};
                //当excel中对应的sheet页宽度太大时，在PDF中会拆断并分页。此处等比缩放。
                autoDraw(wb, autoDrawSheets);
                int[] showSheets={0,1};
                //隐藏workbook中不需要的sheet页。
                printSheetPage(wb,showSheets);
                wb.save(fileOS, pdfSaveOptions);
                fileOS.flush();
                fileOS.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 设置打印的sheet 自动拉伸比例
     * @param wb
     * @param page 自动拉伸的页的sheet数组
     */
    public static void autoDraw(Workbook wb,int[] page){
        if(null!=page&&page.length>0){
            for (int i = 0; i < page.length; i++) {
                wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
                wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
            }
        }
    }


    /**
     * 隐藏workbook中不需要的sheet页。
     * @param wb
     * @param page 显示页的sheet数组
     */
    public static void printSheetPage(Workbook wb,int[] page){
        for (int i= 1; i < wb.getWorksheets().getCount(); i++)  {
            wb.getWorksheets().get(i).setVisible(false);
        }
        if(null==page||page.length==0){
            wb.getWorksheets().get(0).setVisible(true);
        }else{
            for (int i = 0; i < page.length; i++) {
                wb.getWorksheets().get(i).setVisible(true);
            }
        }
    }

    public static void main(String[] args) {
        excelToPdf("/Users/lionsong/Downloads/1.xlsx","/Users/lionsong/Downloads/1.pdf");
    }
}