下載pdf

/* eslint-disable prefer-destructuring */
// 不使用JQuery版的

import html2canvas from 'html2canvas';
import JsPDF from 'jspdf';
import _ from 'lodash';

/**

  • @param ele 要生成 pdf 的DOM元素(容器)
  • @param padfName PDF文件生成后的文件名字
  • */

async function downloadPDF(ele, pdfName) {
const eleW = ele.offsetWidth; // 獲得該容器的寬
const eleH = ele.offsetHeight; // 獲得該容器的高

const eleOffsetTop = ele.offsetTop; // 獲得該容器到文檔頂部的距離
const eleOffsetLeft = ele.offsetLeft; // 獲得該容器到文檔最左的距離

const canvas = document.createElement('canvas');
let abs = 0;

const winIn = document.documentElement.clientWidth || document.body.clientWidth; // 獲得當(dāng)前可視窗口的寬度(不包含滾動(dòng)條)
const winOut = window.innerWidth; // 獲得當(dāng)前窗口的寬度(包含滾動(dòng)條)

if (winOut > winIn) {
abs = (winOut - winIn) / 2; // 獲得滾動(dòng)條寬度的一半
}

canvas.width = eleW * 2; // 將畫布寬&&高放大兩倍
canvas.height = eleH * 2;

const context = canvas.getContext('2d');

context.scale(2, 2);
context.translate(-eleOffsetLeft - abs, -eleOffsetTop);
// 縮放比例
const scale = 794 / eleW;
const handlerData = async el => {
const canvas = await html2canvas(el, {
dpi: 300,
useCORS: true,
});
const pageData = canvas.toDataURL('image/jpeg', 1.0);
const contentWidth = canvas.width;
const contentHeight = canvas.height;
const imgWidth = 595.28;
const imgHeight = (595.28 / contentWidth) * contentHeight;
return {
pageData,
imgWidth,
imgHeight,
};
};
// 元素分組
const handlerImage = e => {
if (_.isEmpty(e)) return [];
return _.map(e, el => {
if (el.offsetHeight * scale > 1123) {
return handlerImage(el.children);
}
return el;
});
};
const nodes = _.flattenDeep(handlerImage(ele.children));
// 元素分組
const data = []; // --------------
nodes.forEach((e, index) => {
if (!index) {
data.push([e]);
} else {
const h = _.sumBy(data[data.length - 1], 'offsetHeight') + e.offsetHeight;
if (h * scale > 1123) {
data.push([e]);
} else {
data[data.length - 1].push(e);
}
}
});
// 找到元素的offsetParent 為 main
const getoffsetTop = (e, h) => {
if (e.offsetParent.tagName === 'MAIN') {
return e.offsetTop + h;
}
return getoffsetTop(e.offsetParent, h);
};
const a = data.map(e => {
// --------------
if (e[0].offsetParent.tagName === 'MAIN') {
return e[0].offsetTop;
}
return getoffsetTop(e[0].offsetParent, e[0].offsetTop);
});

const image = a.map((e, index) => {
if (index === a.length - 1) return eleH - e - 24;
return Math.abs(a[index + 1] - e);
});
// imageData.pageData base64
const imageData = await handlerData(ele);

// create div
const div = document.createElement('div');
div.id = 'imgdiv';
const img = new Image();
img.src = imageData.pageData;
img.width = imageData.imgWidth;
img.height = imageData.imgHeight;
img.id = 'dowanloadImg';
div.appendChild(img);
document.getElementById('download').appendChild(div);
// 分塊 畫img
const canvasImage = (imgData, x, y, nHeight, index) => {
const canvasOther = document.createElement('canvas');
canvasOther.width = eleW;
canvasOther.height = nHeight;
canvasOther.id = id-${index};
canvasOther.type = 'canvasOther';
const ctx = canvasOther.getContext('2d');
ctx.drawImage(imgData, x, y, eleW, nHeight, 0, 0, eleW, nHeight); // 重繪
const data = canvasOther.toDataURL('image/jpeg', 1.0);
document.getElementById('imgdiv').appendChild(canvasOther);

const width = 595.28;
const height = (595.28 / eleW) * nHeight;
return {
  data,
  width,
  height,
};

};
// imgData 整張圖片信息
const imgData = document.getElementById('dowanloadImg');
imgData.onload = async () => {
// 根據(jù)image信息把imgData循環(huán)切割成多個(gè)圖片
const imageArray = image.map(async (nHeight, index) => {
const data = await canvasImage(imgData, 0, _.sum(index ? _.chunk(image, index)[0] : [0]), nHeight, index);
return data;
});
// del node
document.getElementById('download').removeChild(document.getElementById('imgdiv'));
// img => pdf
const images = await Promise.all(imageArray);
const {
length,
} = images;
const pdf = new JsPDF('', 'pt', 'a4');
images.forEach((e, index) => {
const {
data,
width,
height,
} = e;
const h = index ? 10 : 0;
pdf.addImage(data, 'JPEG', -3, h, width, height);
if (index === length - 1) return;
pdf.addPage();
});
pdf.save(pdfName);
};
}

export default {
downloadPDF,
};

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容