package com.jwipc.util;


import cn.hutool.core.lang.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiang.zhou
 * @version 1.0
 * @date 2020/4/27 14:29
 */
public class PDFUtil {


    private static final Logger log = LoggerFactory.getLogger(PDFUtil.class);

    /**
     * html生成不带页码pdf文件
     *
     * @param htmlContent html内容
     * @param pdfFilePath 生成pdf存储绝对路径
     */
    public static void generatePdfFromHtml(String htmlContent, String pdfFilePath) {
        generatePdfFromHtml(htmlContent, pdfFilePath, false,false);
    }



    /**
     * html生成带页码pdf文件
     *
     * @param htmlContent html内容
     * @param pdfFilePath 生成pdf存储绝对路径
     * @param page        是否需要页码
     * @param transverse        是否横向
     */
    public static void generatePdfFromHtml(String htmlContent, String pdfFilePath, boolean page,boolean transverse) {
        try {
            // 创建HTML文件
            String fileName = UUID.randomUUID().toString() + ".html";
            File htmlFile = new File(fileName);
            FileWriter fileWriter = new FileWriter(htmlFile);
            fileWriter.write(htmlContent);
            fileWriter.close();
            // 使用wkhtmltopdf命令生成PDF
            String bottom = "0";
            if (page) {
                bottom = "5";
            }
            // 构建命令参数列表
            List<String> command = new ArrayList<>();
            command.add("wkhtmltopdf");
            if(transverse) {
                command.add("--orientation");
                command.add("Landscape");
            }
            command.add("--margin-top");
            command.add("0");
            command.add("--margin-bottom");
            command.add(bottom);
            command.add("--margin-left");
            command.add("0");
            command.add("--margin-right");
            command.add("0");
            command.add("--print-media-type");
            command.add("--disable-smart-shrinking");
            command.add("--enable-local-file-access");
            if(page){
                command.add("--footer-center");
                command.add("[page] / [topage]");
            }
            command.add(fileName);
            command.add(pdfFilePath);
            // 使用 ProcessBuilder 执行命令
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();
            process.waitFor();
            htmlFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void generateBarPdfFromHtml(String htmlContent, String pdfFilePath) {
        try {
            // 创建HTML文件
            String fileName = UUID.randomUUID().toString() + ".html";
            File htmlFile = new File(fileName);
            FileWriter fileWriter = new FileWriter(htmlFile);
            fileWriter.write(htmlContent);
            fileWriter.close();
            // 使用wkhtmltopdf命令生成PDF
            String bottom = "0";
            // 构建命令参数列表
            List<String> command = new ArrayList<>();
            command.add("wkhtmltopdf");
            command.add("--margin-top");
            command.add("0");
            command.add("--margin-bottom");
            command.add(bottom);
            command.add("--margin-left");
            command.add("0");
            command.add("--margin-right");
            command.add("0");
            command.add("--print-media-type");
            command.add("--disable-smart-shrinking");
            command.add("--enable-local-file-access");
            command.add("--page-width");
            command.add("80mm");
            command.add("--page-height");
            command.add("40mm");
            command.add(fileName);
            command.add(pdfFilePath);
            // 使用 ProcessBuilder 执行命令
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();
            process.waitFor();
            htmlFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
