Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"fs is not defined" in the prebuild version

See original GitHub issue

Hi, I don’t use NodeJS so I use the prebuild version of PDFKit to generate the PDF. I imported the library in the HTML file `<script src="Scripts/blob-stream.js" type="text/javascript"></script>

<script src="Scripts/pdfkit.js" type="text/javascript"></script>`

It worked well with built-in fonts. When I registered a font from a file and used it doc.registerFont('OpenSans-Regular', 'fonts/OpenSans-Regular.ttf', 'OpenSans-Regular'); doc.font('OpenSans-Regular'); I got this error Uncaught ReferenceError: fs is not defined at Object.fontkit.openSync (pdfkit.js:16213) at (pdfkit.js:12888) at PDFDocument.font (pdfkit.js:14673) Can anyone please guide me how to solve this problem? Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

kelvin200commented, Mar 7, 2017

Hi, You are right, I didn’t load the font data correclty. Here’s the code for those who need it (jQuery doesn’t support binary datatype on ajax call)

var xhr = new XMLHttpRequest();'GET', font.url, true);
xhr.responseType = 'arraybuffer';

xhr.onload = function (e) {
  if (this.status == 200) {
    doc.registerFont(, xhr.response);
    // Do something...

kelvin200commented, Mar 16, 2019

@ninbit It has been a long time since I worked with that project so I can’t remember. However, I guess font is just my local object to define font name and url. You can define it the way you like, just make sure that you can pass it to the above function to load it and render your text.

You get CORS error because you dev site and your font are not on the same domain, I guess. I used localhost for my dev site so I had no problem with it.

It’s not relevant to this topic though but I’m willing to help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"fs is not defined" in the prebuild version #715 - foliojs/pdfkit
Hi, I'm trying to load custom font ttf files in the prebuilt version but currently getting the error: "Uncaught ReferenceError: fs is not...
Read more >
Node script ReferenceError: fs is not defined error in test script
I get this error: fs = require('fs'); ^ ReferenceError: fs is not defined at Object.<anonymous> ...
Read more >
Getting Started - ESBuild
Getting Started. #Install esbuild. First, download and install the esbuild command locally. A prebuilt native executable can be installed using npm (which ...
Read more >
pkg - npm
Package your Node.js project into an executable. Latest version: 5.8.0, last published: 6 months ago. Start using pkg in your project by ...
Read more >
Built-in Functions — Python 3.11.1 documentation
The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found