1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| import jsPDF from 'jspdf'; import 'jspdf-autotable'; import { AutoText as jsPDFAutoText } from 'jspdf-autotext'; import { fontString } from "./pdf-font";
const pdfDoc = new jsPDF('l', 'mm', [ 210, 297 ], true);
pdfDoc.addFileToVFS("hei.ttf", fontString); pdfDoc.addFont("hei.ttf", "hei", "normal"); pdfDoc.setFont('hei', 'normal');
const autoText = new jsPDFAutoText({ pdfFile: pdfDoc }); autoText.render({ text: '这是一个标题', indent: 10 });
const totalPagesExp = '{total_pages_count_string}';
pdfDoc.autoTable( { theme: "grid", startY: 10, margin: { }, styles: { font: 'hei', fontSize: 8, valign: 'middle', lineColor: [0, 51, 51] }, tableWidth: 'wrap', rowPageBreak: "avoid", headStyles: { valign: 'middle', halign: 'center', fontStyle: 'bold', lineWidth: 0.1, fillColor: [119, 119, 119] }, body: [ { name: '小A', age: 22, address: '地址1' }, { name: '小B', age: 25, address: '地址2' }, { name: '小C', age: 27, address: '地址3' }, ], bodyStyles: { textColor: [51, 51, 51] }, columns: [ { dataKey: 'name', header: '姓名' }, { dataKey: 'age', header: '年龄' }, { dataKey: 'address', header: '性别' } ], columnStyles: { name: { halign: 'left', cellWidth: 100 }, age: { halign: 'center', cellWidth: 20 }, address: { halign: 'right', cellWidth: 200 }, }, foot: [ [ { content: '合计', colSpan: 2 , styles: { halign: 'center' } }, '这是合计后面的表格,要显示的值' ] ], showFoot: 'lastPage', footStyles: { fillColor: [255, 255, 255], textColor: [51, 51, 51], fontStyle: 'bold', lineWidth: 0.1, lineColor: [0, 51, 51], halign: 'right' }, willDrawPage: (data) => { }, didParseCell: (data) => { }, didDrawPage: (data) => { let str = 'Page ' + pdfDoc.getNumberOfPages(); if (typeof doc.putTotalPages === 'function') { str = str + ' of ' + totalPagesExp } pdfDoc.setFontSize(10)
var pageSize = pdfDoc.internal.pageSize var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight() pdfDoc.text(str, 10, pageHeight - 10) }, })
if (typeof doc.putTotalPages === 'function') { pdfDoc.putTotalPages(totalPagesExp) }
pdfDoc.save('/home/dev/pdf/download/test.pdf');
|