I am using jsPDF to generate PDF and I need to load a custom font with multiple font-stretch values (normal, condensed and expanded) from a Font Family I defined which supports it.
However, I don't see how I can specify that font-stretch property when loading the font.
This is how I declare my Font Family in CSS
@font-face {
font-family: Bahnschrift;
src: url("~@/assets/fonts/Bahnschrift/bahnschrift-normal.ttf") format("truetype");
font-stretch: normal;
}
@font-face {
font-family: Bahnschrift;
src: url("~@/assets/fonts/Bahnschrift/bahnschrift-condensed.ttf") format("truetype");
font-stretch: condensed;
}
@font-face {
font-family: Bahnschrift;
src: url("~@/assets/fonts/Bahnschrift/bahnschrift-expanded.ttf") format("truetype");
font-stretch: expanded;
}
This is how I load the three custom fonts into jsPDF
Here, in line 5 is where I think I should declare the condensed / expanded variations, but the other possible values I am aware of are Italic, Bold and their combinations
import { jsPDF } from "jspdf"
var font = "<base64 font file>"
var callAddFont = function () {
this.addFileToVFS('Bahnschrift-normal.ttf', font);
this.addFont('Bahnschrift-normal.ttf', 'Bahnschrift', 'normal');
};
jsPDF.API.events.push(['addFonts', callAddFont])
import { jsPDF } from "jspdf"
var font = "<base64 font file>"
var callAddFont = function () {
this.addFileToVFS('Bahnschrift-condensed.ttf', font);
this.addFont('Bahnschrift-condensed.ttf', 'Bahnschrift', 'normal'); // I think I should be able to set condensed here
};
jsPDF.API.events.push(['addFonts', callAddFont])
import { jsPDF } from "jspdf"
var font = "<base64 font file>"
var callAddFont = function () {
this.addFileToVFS('Bahnschrift-expanded.ttf', font);
this.addFont('Bahnschrift-expanded.ttf', 'Bahnschrift', 'normal'); // I think I should be able to set expanded here
};
jsPDF.API.events.push(['addFonts', callAddFont])
Finally, I use the font like this
const font = `normal normal 400 16px Bahnschrift`;
await document.fonts.load(font);
pdf.font = font;
const font = `condensed normal 400 16px Bahnschrift`;
await document.fonts.load(font);
pdf.font = font;
const font = `expanded normal 400 16px Bahnschrift`;
await document.fonts.load(font);
pdf.font = font;
Could anyone help me out here please?