<script src="/npm/jspdf"></script><script src="/npm/vxe-table-plugin-export-pdf/fonts/source-han-sans-normal.js"></script>
source-han-sans-normal.js解决中午乱码
pdf()导出pdf
function pdf(pdff) {let [imgX, imgY] = [375, 640]var doc = new jsPDF('p', 'pt', [imgX, imgY])var img = new Image();img.src = pdffvar height = doc.internal.pageSize.getHeight;var position_1 = 0var position_2 = 0var txt_1 = 0var txt_2 = 75var num = 0var su = 7doc.addFont('SourceHanSans-Normal.ttf', 'SourceHanSans-Normal', 'normal');doc.setFont('SourceHanSans-Normal');for (var i = 0; i < 100; i++) {num++//doc.addImage(img, 'png', position_1, position_2, 45, 60);let imgHeight = imgX / (45 / 60); //根据宽度计算高度doc.setFontSize(10)if (i == 0) {doc.addImage(img, 'png', position_1, position_2, 45, 60);doc.text(txt_1, txt_2, '张三');//doc.text(position_1, position_2, num+"******");} else if (num % su == 1) {position_1 = 0txt_1 = 0doc.addImage(img, 'png', position_1, position_2, 45, 60);doc.text(txt_1, txt_2, '张三');} else if (num % su == 2) {position_1 = 55txt_1 = 55doc.addImage(img, 'png', position_1, position_2, 45, 60);doc.text(txt_1, txt_2, '张三');} else if (num % su == 3) {position_1 = 110txt_1 = 110doc.addImage(img, 'png', position_1, position_2, 45, 60);doc.text(txt_1, txt_2, '张三');} else if (num % su == 4) {position_1 = 165txt_1 = 165doc.addImage(img, 'png', position_1, position_2, 45, 60);doc.setFontSize(10)doc.text(txt_1, txt_2, '张三');} else if (num % su == 5) {position_1 = 220txt_1 = 220doc.addImage(img, 'png', position_1, position_2, 45, 60);doc.text(txt_1, txt_2, '张三');} else if (num % su == 6) {position_1 = 275txt_1 = 275doc.addImage(img, 'png', position_1, position_2, 45, 60);doc.text(txt_1, txt_2, '张三');} else if (num % su == 0) {position_1 = 330txt_1 = 330doc.addImage(img, 'png', position_1, position_2, 45, 60);//doc.text(txt_1, txt_2, '张三');position_2 = position_2 + 80txt_2 = txt_2 + 80}if (num % 56 == 0) {txt_1 = 0txt_2 = 75position_1 = 0position_2 = 0doc.addPage();//分页}}doc.output('dataurlnewwindow')}
encodeImageFileAsURL()将图片链接转base64
function encodeImageFileAsURL(element) {var b64 = ''var imgUrl ='https://vkceyugu./VKCEYUGU-b0e583f2-e3b6-479a-a561-36f0922b5c59/bf099a60-48d8-4ad8-a940-34b2a1ded019.png'var xhr = new XMLHttpRequest();xhr.open("get", imgUrl, true);xhr.responseType = "blob";xhr.onload = function() {if (this.status == 200) {//得到一个blob对象var blob = this.response;console.log("blob", blob)// 至关重要let oFileReader = new FileReader();oFileReader.onloadend = function(e) {// 此处拿到的已经是base64的图片了,可以赋值做相应的处理console.log(e.target.result)//b64=e.target.resultpdf(e.target.result)}oFileReader.readAsDataURL(blob);}}xhr.send();}
导出的pdf