"Error: Unknown version 0" when using NodeJS PDFKit

I am trying to get a very excellent NodeJS PDFKit to use custom OpenSans and Roboto fonts from Google Fonts . My code is as follows:

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

I printed the path - he found the correct file. I get 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 OpenSans, it worked at least, but everything looked awful. The letters were bleeding and almost blurred.

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

Why are these fonts not working?

0
node-pdfkit
03 Oct '17 at 23:20
source share
1 answer

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

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

This will create a new TTF file, which should contain all the text inside. If you use an external service, make sure there is no packaging. It should be one continuous block of text.

Then in the 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 appear crystal clear. Reinforces this answer for giving me the keys I need.

0
03 Oct '17 at 23:20
source share



All Articles