Search code examples
javascriptjquerytruetypepdfmakenode-pdfkit

How to convert a font file ttf into string data like vfs_fonts.js does?


I am planning to embed custom font ttf into pdfmake plugin. The documentation recommends to add the custom fonts to the vfs_fonts.js file.

 window.pdfMake = window.pdfMake || {}; window.pdfMake.vfs = {
  "Roboto-Italic.ttf": "AAEAAAASAQAABA",
  "Roboto-Medium.ttf": "AAEAAA",
  "MyFont.ttf":"???????????????????"
}

I have MyFont.ttf file but I don't know how to convert that into string/encoded format. Is there anyway to do it programatically through javascript?


Solution

  • It is a base64 format.

    There are many tools available online.

    Here is one

    Programatically :

    In JavaScript there are two functions respectively for decoding and encoding base64 strings:

    atob()

    btoa()

    The atob() function decodes a string of data which has been encoded using base-64 encoding.

    Conversely, the btoa() function creates a base-64 encoded ASCII string from a "string" of binary data.

    source