Home
Support Forums

Using PDFKit in a netlify function

Netlify Site (function only)

I’m trying to build some functions that return PDFs using PDFkit. I’ve found a few examples for implementing this directly with AWS lambdas but I’m getting the following error when running the functions server with both netlify dev & netlify functions:serve

Error: ENOENT: no such file or directory, open '/Users/blackcatstudio1/code/ts-pdfgen-lamda/.netlify/functions-serve/pdfgen/src/functions/pdfgen/data.trie'
  Object.openSync (fs.js:476:3)
  Object.readFileSync (fs.js:377:35)
  node_modules/unicode-properties/unicode-properties.cjs.js (/Users/blackcatstudio1/code/ts-pdfgen-lamda/node_modules/unicode-properties/node.js:6:33)
  __require (/Users/blackcatstudio1/code/ts-pdfgen-lamda/.netlify/functions-serve/pdfgen/src/functions/pdfgen/index.js:9:44)
  node_modules/fontkit/index.js (/Users/blackcatstudio1/code/ts-pdfgen-lamda/.netlify/functions-serve/pdfgen/src/functions/pdfgen/index.js:136732:35)
  __require (/Users/blackcatstudio1/code/ts-pdfgen-lamda/.netlify/functions-serve/pdfgen/src/functions/pdfgen/index.js:9:44)
  node_modules/pdfkit/js/pdfkit.js (/Users/blackcatstudio1/code/ts-pdfgen-lamda/.netlify/functions-serve/pdfgen/src/functions/pdfgen/index.js:148922:19)
  __require (/Users/blackcatstudio1/code/ts-pdfgen-lamda/.netlify/functions-serve/pdfgen/src/functions/pdfgen/index.js:9:44)
  Object.<anonymous> (/Users/blackcatstudio1/code/ts-pdfgen-lamda/functions/pdfgen/index.ts:2:25)
  Module._compile (internal/modules/cjs/loader.js:1063:30)

Any idea what might be causing this? I have the repo here - it’s the only function in the project currently. Thanks!

Hi @stordahl

The cause is related to UnicodeTrie of which there are two references in the shipped function

var trie = new UnicodeTrie(fs.readFileSync(__dirname + "/data.trie"));

// more ...

var trie = new UnicodeTrie(require("fs").readFileSync(__dirname + "/data.trie"));

There is an issue here though it is related specifically to Webpack, and another here which doesn’t have a solution per se. unicode-trie is dependency of both fontkit and linebreak which are in turn both dependencies of pdfkit. I did try the code from this thread (which the OP appears to have gotten working) with the same result as well as trying this code.

1 Like