使用NodeJS PDFKit时出现“错误:未知版本0”

时间:2022-08-03 18:02:29

I'm trying to get the very excellent NodeJS PDFKit to use custom fonts OpenSans and Roboto from Google Fonts. My code looks like the following:

我正在尝试使用非常出色的NodeJS PDFKit来使用Google字体中的自定义字体OpenSans和Roboto。我的代码如下所示:

this.doc = new PDFDocument({bufferPages: true});
this.doc.registerFont("Roboto-Black", path.join(__dirname, "fonts", "Roboto-Black.ttf"));

I've printed the path - it's finding the right file. I'm getting the following error:

我打印了路径 - 它找到了正确的文件。我收到以下错误:

C:\projects\qbdvision\node_modules\restructure\src\VersionedStruct.js:37
        throw new Error("Unknown version " + res.version);
        ^

Error: Unknown version 0
    at VersionedStruct.decode (C:\projects\qbdvision\node_modules\restructure\src\VersionedStruct.js:37:15)
    at C:\projects\qbdvision\node_modules\restructure\src\Pointer.js:69:30
    at Pointer.decode (C:\projects\qbdvision\node_modules\restructure\src\Pointer.js:79:16)
    at ArrayT.decode (C:\projects\qbdvision\node_modules\restructure\src\Array.js:49:30)
    at VersionedStruct.Struct._parseFields (C:\projects\qbdvision\node_modules\restructure\src\Struct.js:53:22)
    at VersionedStruct.decode (C:\projects\qbdvision\node_modules\restructure\src\VersionedStruct.js:42:12)
    at VersionedStruct.decode (C:\projects\qbdvision\node_modules\restructure\src\VersionedStruct.js:40:23)
    at C:\projects\qbdvision\node_modules\restructure\src\Pointer.js:69:30
    at Pointer.decode (C:\projects\qbdvision\node_modules\restructure\src\Pointer.js:79:16)
    at ArrayT.decode (C:\projects\qbdvision\node_modules\restructure\src\Array.js:49:30)
 FAILED

When I removed the Roboto font, and tried the OpenSans one, it worked at least, but everything looked terrible. Letters were bleeding together and looked almost smudged.

当我删除Roboto字体并尝试使用OpenSans字体时,它至少起作用,但一切看起来都很糟糕。信件一起流血,看起来几乎是污迹。

I've downloaded the fonts from fonts.google.com by clicking "Select this font", clicking on the "1 Family Selected" popup that comes up and then clicking on the download icon in the upper right hand corner of that popup.

我已经通过点击“选择此字体”从fonts.google.com下载了字体,单击出现的“1 Family Selected”弹出窗口,然后单击该弹出窗口右上角的下载图标。

Why won't these fonts work?

为什么这些字体不起作用?

1 个解决方案

#1


0  

The solution is to convert the fonts into base64 encoding and then import them. So at the command line, using Linux / Cygwin, type:

解决方案是将字体转换为base64编码,然后导入它们。所以在命令行中,使用Linux / Cygwin,键入:

base64 --wrap=0 Roboto-Black.ttf > Roboto-Black-Base64.ttf

That'll produce a new TTF file that should be all text inside. If you use an external service, make sure there isn't any wrapping. It should be one continuous block of text.

这将生成一个新的TTF文件,该文件应该是所有文本内部。如果您使用外部服务,请确保没有任何包装。它应该是一个连续的文本块。

Then, in your NodeJS code, do:

然后,在您的NodeJS代码中,执行:

let fs = require("fs");

let doc = new PDFDocument({bufferPages: true});
let filePath = path.join(__dirname, "fonts", "Roboto-Black-Base64.ttf");
let fileContents = fs.readFileSync(filePath, "utf8");
this.doc.registerFont(fontName, new Buffer(fileContents, "base64"));

Then your fonts will show up crystal clear. Props to this answer for giving me the clues I needed.

然后你的字体将显示清晰。为这个答案提供了道具,为我提供了我需要的线索。

#1


0  

The solution is to convert the fonts into base64 encoding and then import them. So at the command line, using Linux / Cygwin, type:

解决方案是将字体转换为base64编码,然后导入它们。所以在命令行中,使用Linux / Cygwin,键入:

base64 --wrap=0 Roboto-Black.ttf > Roboto-Black-Base64.ttf

That'll produce a new TTF file that should be all text inside. If you use an external service, make sure there isn't any wrapping. It should be one continuous block of text.

这将生成一个新的TTF文件,该文件应该是所有文本内部。如果您使用外部服务,请确保没有任何包装。它应该是一个连续的文本块。

Then, in your NodeJS code, do:

然后,在您的NodeJS代码中,执行:

let fs = require("fs");

let doc = new PDFDocument({bufferPages: true});
let filePath = path.join(__dirname, "fonts", "Roboto-Black-Base64.ttf");
let fileContents = fs.readFileSync(filePath, "utf8");
this.doc.registerFont(fontName, new Buffer(fileContents, "base64"));

Then your fonts will show up crystal clear. Props to this answer for giving me the clues I needed.

然后你的字体将显示清晰。为这个答案提供了道具,为我提供了我需要的线索。