C:\Windows\Fonts\Calibri
并将 .ttf
字体文件复制到桌面。import { jsPDF } from "jspdf"
var font = 'AAEAAAAWAQQAAB...==';
var callAddFont = function () {
this.addFileToVFS('calibri-normal.ttf', font);
this.addFont('calibri-normal.ttf', 'calibri', 'normal');
};
jsPDF.API.events.push(['addFonts', callAddFont]);
this.addFont('calibri-normal.ttf', 'calibri', 'normal');
中的第二个字符串将是您字体的名称。请记住它。
import '../utils/calibri-normal';
jsPDF
对象的位置并设置您的字体:const doc = new jsPDF('p', 'pt', 'a4');
doc.setFont("calibri"); // <-- place here your font name, which you remeber before
//...
doc.save('file001.pdf');
doc.autoTable({
head: [['Column1', 'Column2', 'Column3']],
body: someDataRows,
styles: {
font: 'calibri', // <-- place name of your font here
fontStyle: 'normal',
},
margin: { bottom: 60 }
});
let font = null;
await fetch('/fonts/poppins-v20-latin-ext_latin-regular.ttf')
.then(response => response.arrayBuffer())
.then(fontData => {
// Convert the ArrayBuffer to a base64-encoded string
const fontDataArray = new Uint8Array(fontData);
const base64FontData = btoa(String.fromCharCode(...fontDataArray));
font = base64FontData;
})
.catch(err => {
// Handle the error here
console.log(err);
});
if (!font) return;
// Add the font to vFS
doc.addFileToVFS('poppins-v20-latin-ext_latin-regular.ttf', font);
// Set unicode font
doc.addFont('poppins-v20-latin-ext_latin-regular.ttf', 'Poppins', 'normal');
doc.setFont('Poppins');
doc.text(supplier, 10, 10);
doc.text(date, 285, 10, {
align: 'right'
});
// Use the autoTable function to create the table
doc.autoTable({
head: [headers.map(header => header.prompt)],
body: tableData,
startY: 20,
theme: 'grid', // You can adjust the theme if needed
styles: { fontSize: fontSize, font: 'Poppins' },
headStyles: { fillColor: '#fbfbfb' },
margin: { top: 10, right: 10, bottom: 10, left: 10 },
didDrawPage: function (data) {
// Add page numbers
doc.text('Page ' + doc.internal.getNumberOfPages(), data.settings.margin.left, doc.internal.pageSize.height - 10);
},
});