I know, this question has been asked e. g. here and here but whatever I try, I do not get my custom font in the generated pdf. Here are my steps:
<body>
:<script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js"></script>
<script src="./SourceSansPro-Regular-normal.js" type="module"></script>
export function generateAndDownloadPdf() {
const doc = new jspdf.jsPDF({
orientation: 'p',
unit: 'pt',
format: 'a4',
putOnlyUsedFonts: true
});
doc.setFont("SourceSansPro-Regular", "normal");
doc.text(50, 50, 'Hello world');
doc.save("sample.pdf");
}
@inject IJSRuntime JSRuntime
<button class="btn btn-primary" @onclick="DownloadPdf">pdf</button>
@code {
private async Task DownloadPdf()
{
await using var module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./create-pdf.js");
await module.InvokeVoidAsync("generateAndDownloadPdf");
}
}
What am I doing wrong?
To get the custom font, follow the steps in the question and do the following:
Delete type="module"
<script src="./SourceSansPro-Regular-normal.js"></script>
var font = 'AAE...';
var callAddFont = function () {
this.addFileToVFS('SourceSansPro-Regular-normal.ttf', font);
this.addFont('SourceSansPro-Regular-normal.ttf', 'SourceSansPro-Regular', 'normal');
};
jspdf.jsPDF.API.events.push(['addFonts', callAddFont])
(function () {
var font = 'AAE...';
var callAddFont = function () {
this.addFileToVFS('SourceSansPro-Regular-normal.ttf', font);
this.addFont('SourceSansPro-Regular-normal.ttf', 'SourceSansPro-Regular', 'normal');
};
jspdf.jsPDF.API.events.push(['addFonts', callAddFont])
})();
If you do not add the first and last line, the font will always be the last one, you added in index.html