download.js 7.24 KB
import html2canvas from "html2canvas";
import jsPDF from "jspdf";
export const mixin = {
    data() {
        return {
            chartList: [],
            pdftitle: ''
        }
    },
    methods: {
        downLoadpdf(data) {
            if (data == 'day') {
                let date = document.querySelector('.day-piack').querySelector('input').value;
                this.pdftitle = `${date}日报`;
            } else if (data == 'week') {
                let date = document.querySelector('.week-piack').querySelector('input').value;
                this.pdftitle = `${date}周报`;
            } else if (data == 'month') {
                let date = document.querySelector('.month-piack').querySelector('input').value;
                this.pdftitle = `${date}月报`;
            } else if (data == 'year') {
                let date = document.querySelector('.year-piack').querySelector('input').value;
                this.pdftitle = `${date}年报`;
            }
            document.querySelector('.index').scrollTop = 0;
            let chartData = document.querySelectorAll(".dchart");
            let headeHtml = document.getElementById('headBox');
            document.getElementById('downheader').innerHTML = headeHtml.innerHTML
            if (chartData.length > 0) {
                for (let i = 0; i < chartData.length; i++) {
                    let ele = chartData[i];
                    let obj = {};
                    obj.id = ele.id;
                    if (!ele.id) continue;
                    obj.title = ele.parentElement.querySelector('.ehcarts-title') ? ele.parentElement.querySelector('.ehcarts-title').textContent : ele.parentElement.parentElement.querySelector('.ehcarts-title') ? ele.parentElement.parentElement.querySelector('.ehcarts-title').textContent : "";
                    obj.src = this.saveFile(ele.id);
                    let { left: x, top: y } = ele.getBoundingClientRect();
                    obj.left = x;
                    obj.top = y - 60;
                    let { offsetWidth } = document.body;
                    let { clientWidth, clientHeight } = ele;
                    //图片相对于1920尺寸的大小
                    let ratio = 1920 / offsetWidth;
                    let pw = clientWidth * ratio;
                    let ph = clientHeight * ratio;
                    obj.width = pw;
                    obj.height = ph;
                    this.chartList.push(obj);
                }
            }
            setTimeout(() => {
                this.exportall(document.querySelector("#downloadbox"));
            }, 2000);
        },
        exportall(container, options = {}) {
            // 设置放大倍数
            const scale = window.devicePixelRatio;
            // 传入节点原始宽高
            const width = document.querySelector("#downloadbox").scrollWidth;
            const height = document.querySelector("#downloadbox").scrollHeight;

            // html2canvas配置项
            const ops = {
                scale,
                width,
                height,
                useCORS: true,
                allowTaint: false,
                ...options,
            };

            html2canvas(container, ops).then((canvas) => {
                let contentWidth = document.querySelector("#downloadbox").scrollWidth;
                let contentHeight = document.querySelector("#downloadbox").scrollHeight;
                // 返回图片的二进制数据
                let pageData = canvas.toDataURL("image/jpeg", 1.0); // 清晰度 0 - 1
                let pdf;

                if (true) {
                    // 单页
                    console.log(contentWidth, "contentWidth");
                    console.log(contentHeight, "contentHeight");

                    // jspdf.js 插件对单页面的最大宽高限制 为 14400
                    let limit = 14400;

                    if (contentHeight > limit) {
                        let contentScale = limit / contentHeight;
                        contentHeight = limit;
                        contentWidth = contentScale * contentWidth;
                    }

                    let orientation = "p";
                    // 在 jspdf 源码里,如果是 orientation = 'p' 且 width > height 时, 会把 width 和 height 值交换,
                    // 类似于 把 orientation 的值修改为 'l' , 反之亦同。
                    if (contentWidth > contentHeight) {
                        orientation = "l";
                    }

                    // orientation Possible values are "portrait" or "landscape" (or shortcuts "p" or "l")
                    pdf = new jsPDF(orientation, "pt", [contentWidth, contentHeight]); // 下载尺寸 a4 纸 比例
                    // pdf.addImage(pageData, 'JPEG', 左,上,宽度,高度)设置
                    pdf.addImage(pageData, "JPEG", 0, 0, contentWidth, contentHeight);
                } else {
                    //一页 pdf 显示 html 页面生成的 canvas高度
                    let pageHeight = (contentWidth / 552.28) * 841.89;
                    //未生成 pdf 的 html页面高度
                    let leftHeight = contentHeight;
                    //页面偏移
                    let position = 0;
                    //a4纸的尺寸[595.28,841.89],html 页面生成的 canvas 在pdf中图片的宽高
                    let imgWidth = 555.28;
                    let imgHeight = (imgWidth / contentWidth) * contentHeight;

                    pdf = new jsPDF("", "pt", "a4"); // 下载尺寸 a4 纸 比例
                    //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
                    //当内容未超过pdf一页显示的范围,无需分页
                    if (leftHeight < pageHeight) {
                        pdf.addImage(pageData, "JPEG", 20, 0, imgWidth, imgHeight);
                    } else {
                        while (leftHeight > 0) {
                            pdf.addImage(pageData, "JPEG", 20, position, imgWidth, imgHeight);
                            leftHeight -= pageHeight;
                            position -= 841.89;
                            //避免添加空白页
                            if (leftHeight > 0) {
                                pdf.addPage();
                            }
                        }
                    }
                }
                this.$emit('downloadSuccess', true);
                pdf.save(this.pdftitle + '.pdf');
            });
        },
        fixType(type) {
            type = type.toLowerCase().replace(/jpg/i, "jpeg");
            let suffix = type.match(/png|jpeg|bmp|gif/)[0];
            return "image/" + suffix;
        },
        saveFile(domId, title) {
            let myChart = {};
            let chartDom = document.getElementById(domId);
            if (this.$echarts.getInstanceByDom(chartDom)) {
                myChart = this.$echarts.getInstanceByDom(chartDom);
            } else {
                myChart = this.$echarts.init(chartDom);
            }

            let type = "png";
            let imgData = myChart.getDataURL({
                type: "png",
                backgroundColor: "#fff",
            });

            let img = new Image();
            img.src = imgData;
            return imgData;
        },
    },
}