Custom font in Firebase Cloud function for creating a PDF

815 Views Asked by At

I am trying to use PDFKit in Firebase cloud functions to create a pdf with 1 line of text a custom font. I have created the folder 'fonts' and added the ttf to it. Only I constantly get this error in the logs:

TestPdf Error: ENOENT: no such file or directory, open 'upc-a.tff' at Object.openSync (fs.js: 462: 3) at Object.readFileSync (fs.js: 364: 35) at

The function in question looks like this:

function generateBarcode (doc) {
   doc
   .font ('fonts / upc-a.tff')
     .fontSize (50)
     .text (
       "Testtest2",
       80,
       780,
       {align: "center", width: 500}
     );
}

I've already tried everything. I added the file in the root folder and tried the following options:

  • .font ('fonts / upc-a.tff')
  • .font ('./ fonts / upc-a.tff')
  • .font ('/ fonts / upc-a.tff')
  • .font ('upc-a.tff') (when the file was in the root folder)

This is a screenshot of the folders: enter image description here

But all to no success. Is there anyone who can help me?

1

There are 1 best solutions below

0
On

The issue seems to be happening because you have created the file upc-a.ttf correctly but when you are providing the path in the function generateBarcode you are giving the path ./fonts/upc-a.tff - a path with the wrong file extension. It should be .ttf (TrueType font file extension), hence your file not found error.

Please change .tff to .ttf as below which would resolve your issue.

function generateBarcode (doc) {
    doc
       .font('fonts/upc-a.ttf')
       .fontSize (50)
       .text(
           "Testtest2",80,780,{align: "center", width: 500}
       );
}