I am utilizing the pdf-lib npm module to generate and edit some pdfs for new employee contracts, etc. All is working perfectly except that when trying to embed a custom font I get the error that fontkit.create() is not an existing function. I’m not even calling it directly, it’s being called as part of the embed process.
Here’s an example of the relevant code:
import { PDFDocument , StandardFonts , TextAlignment } from ‘pdf-lib’ ;
var fontkit = require ( ‘@pdf-lib/fontkit’ );
export async function acceptContractButton_click ( event ) {
const formUrl = thisContract ;
const formPdfBytes = await fetch ( formUrl ). then ( res => res . arrayBuffer ())
const pdfDoc = await PDFDocument . load ( formPdfBytes )
const times = await pdfDoc . registerFontkit ( fontkit );
const fontBytes = await fetch ( ‘https://static.wixstatic.com/ufonts/6341cc_e9fcb46a75cd406694e16ff1b8e69bbb/woff2/file.woff2’ ). then ( res => res . arrayBuffer ())
let font = await fontkit . create ( fontBytes )
const customFont = await pdfDoc . embedFont ( font );
//*** ^ This is where the error is occurring. ***//
console . log ( “Successfully embedded font.” )
const page = pdfDoc . getPage ( 6 );
page . drawText (
` ${ info.fullName } ` ,
{
x : 233 ,
y : 454 ,
font : customFont ,
size : 18
},)
const pdfDataUri = **await** pdfDoc . saveAsBase64 ({ dataUri : **true** });
thisContract = pdfDataUri ;
$w ( '#contractFrame' ). postMessage ( thisContract );
}
Any ideas why this is happening?