"错误:未知版本0"使用NodeJS PDFKit时 [英] "Error: Unknown Version 0" when using NodeJS PDFKit

查看:149
本文介绍了"错误:未知版本0"使用NodeJS PDFKit时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得非常出色的 NodeJS PDFKit ,以便从以下版本使用自定义字体OpenSans和Roboto Google字体.我的代码如下所示:

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:

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

当我删除Roboto字体并尝试使用OpenSans字体时,它至少可以正常工作,但是一切看上去都很糟糕.信件一起流血,看上去几乎被弄脏了.

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.

我已经从fonts.google.com下载了字体,方法是单击选择此字体",单击出现的"1 Family Selected"弹出窗口,然后单击该字体右上角的下载图标.弹出窗口.

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.

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

Why won't these fonts work?

推荐答案

解决方案是将字体转换为base64编码,然后将其导入.因此,在命令行上,使用Linux/Cygwin,输入:

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

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

这将产生一个新的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.

然后,在您的NodeJS代码中,执行以下操作:

Then, in your NodeJS code, do:

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.

这篇关于"错误:未知版本0"使用NodeJS PDFKit时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆