vue3 pdf转文件流 直接上传服务器 html2canvas--jspdf
import html2Canvas from "html2canvas";
import jsPDF from "jspdf";
html2Canvas(document.getElementById("content"), {
allowTaint: true,
backgroundColor: "#000",
useCORS: true,
}).then((canvas) => {
console.log(canvas.toDataURL);
const contentWidth = canvas.width;
const contentHeight = canvas.height;
const pageHeight = (contentWidth / 592.28) * 841.89;
let leftHeight = contentHeight;
let position = 0;
const imgWidth = 595.28;
const imgHeight = (592.28 / contentWidth) * contentHeight;
const pageData = canvas.toDataURL("image/png");
const pdf = new jsPDF("l", "px", "a4", true);
if (leftHeight <= pageHeight) {
pdf.addImage(pageData, "PNG", 18, 13, imgWidth, imgHeight, "", "FAST");
} else {
while (leftHeight > 0) {
pdf.addImage(pageData, "PNG", 18, position, imgWidth, imgHeight, "", "FAST");
leftHeight -= pageHeight;
position -= 841.89;
if (leftHeight > 0) {
pdf.addPage();
}
}
}
var pdfData = pdf.output("datauristring");
var myfile = dataURLtoFile(pdfData, "123" + ".pdf");
console.log(myfile);
setTimeout(async () => {
const res = await uploadFile(myfile);
if (res) {
console.log(res);
}
}, 100);
});
const dataURLtoFile = (dataurl, filename) => {
var arr = dataurl.split(","),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
};