如何将字体文件ttf转换为字符串数据,如vfs_fonts.js呢?

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

我打算将自定义字体 ttf 嵌入到 pdfmake 插件中。文档建议将自定义字体添加到 vfs_fonts.js 文件。

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

我有 MyFont.ttf 文件,但我不知道如何将其转换为 string/encoded 格式。有没有办法通过 javascript 以编程方式进行?

它是 base64 格式。

网上有很多工具。

Here is one

程序化:

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