// 导出页面为PDF格式
import html2canvas from "html2canvas";
import JSPDF from "jspdf";
import axios from "axios";
/*** * elementName: 需要输出PDF的页面id * htmlTitle: 页面标题 * currentTime：创建时间 */
export function html2canva(elementName, htmlTitle, currentTime) {
    var element = document.getElementById(elementName)
    html2canvas(element, {
        logging: false
    }).then(function (canvas) {
        var pdf = new JSPDF("p", "mm", "a4") // A4纸，纵向
        var ctx = canvas.getContext("2d")
        var a4w = 190;
        var a4h = 257 // A4大小，210mm x 297mm，四边各保留20mm的边距
        var imgHeight = Math.floor(a4h * canvas.width / a4w) // 按A4显示比例换算一页图像的像素高度
        var renderedHeight = 0
        while (renderedHeight < canvas.height) {
            var page = document.createElement("canvas")
            page.width = canvas.width
            page.height = Math.min(imgHeight, canvas.height - renderedHeight) // 可能内容不足一页
            // 用getImageData剪裁指定区域，并画到前面创建的canvas对象中
            page.getContext("2d").putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)), 0, 0)
            pdf.addImage(page.toDataURL("image/jpeg", 1.0), "JPEG", 10, 10, a4w, Math.min(a4h, a4w * page.height / page.width)) // 添加图像到页面，保留10mm边距
            renderedHeight += imgHeight
            if (renderedHeight < canvas.height) { pdf.addPage() } // 如果后面还有内容，添加一个空页
            // delete page;
        }
        pdf.save(htmlTitle + currentTime)
    })
}
//将html转换成图片png
export function Html2canvasImg(elementName, htmlTitle) {
    html2canvas(document.getElementById(elementName), {
        useCORS: true,
        allowTaint: true,
        borderColor: null,
        dpi: 300,
        scale: 2,
        scrollY: 0,
        scrollx: 0,
    }).then(canvas => {
        var image = canvas.toDataURL("image/jpg");
        let name = htmlTitle;
        downloadFile(name, image);
    });
}
export function saveCanvas(elementName, htmlTitle) {
    // 画布基础元素，要绘制的元素
    const canvas = document.getElementById(elementName);


    const options = { scale: 1, useCORS: true };
    html2canvas(canvas, options).then((canvas) => {
        const type = "png";
        // 返回值是一个数据url，是base64组成的图片的源数据
        let imgDt = canvas.toDataURL(type);
        let fileName = htmlTitle + "." + type;
        // 保存为文件
        let a = document.createElement("a");
        document.body.appendChild(a);
        a.href = imgDt;
        a.download = fileName;
        a.click();
    });
};

//下载文件
export function downloadFile(fileName, content) {
    var base64ToBlob = function (code) {
        let parts = code.split(';base64,');
        let contentType = parts[0].split(':')[1];
        let raw = window.atob(parts[1]);
        let rawLength = raw.length;
        let uInt8Array = new Uint8Array(rawLength);
        for (let i = 0; i < rawLength; ++i) {
            uInt8Array[i] = raw.charCodeAt(i);
        }
        return new Blob([uInt8Array], {
            type: contentType
        });
    };
    let aLink = document.createElement('a');
    let blob = base64ToBlob(content); //new Blob([content]);
    let evt = document.createEvent("HTMLEvents");
    evt.initEvent("click", true, true);
    aLink.download = fileName;
    aLink.href = URL.createObjectURL(blob);
    aLink.click();
}
export function onloadewm1(url, filename) {
    var a = document.createElement('a');      // 创建一个a节点插入的document
    var event = new MouseEvent('click')          // 模拟鼠标click点击事件
    a.download = filename;                  // 设置a节点的download属性值
    a.href = url;                                     // 将图片的src赋值给a节点的href
    a.dispatchEvent(event)
    a.remove(); // 下载之后把创建的元素删除
}