PDF creation function returns RangeError when deployed

Hello Community,

I wrote a function which utilizes the PDFKit node library to generate a PDF. Everything worked fine when I tested locally using netlify dev but on deploy I receive the following error during invocation.

errorType: “RangeError”,
errorMessage: “Invalid typed array length: 4034658560”,
trace: [
“RangeError: Invalid typed array length: 4034658560”,
" at new Uint8Array ()",
" at new UnicodeTrie (/var/task/node_modules/unicode-trie/index.js:57:28)",
" at Object. (/var/task/node_modules/unicode-properties/unicode-properties.cjs.js:139:12)",
" at Module._compile (internal/modules/cjs/loader.js:955:30)",
" at Object.Module._extensions…js (internal/modules/cjs/loader.js:991:10)",
" at Module.load (internal/modules/cjs/loader.js:811:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:723:14)",
" at Module.require (internal/modules/cjs/loader.js:848:19)",
" at require (internal/modules/cjs/helpers.js:74:18)",
" at Object. (/var/task/node_modules/fontkit/index.js:24:31)"

Here’s the code I am using:

const PDFDocument = require('pdfkit')

exports.handler = async (event, context) => {
  const subject = event.queryStringParameters.name || 'World'

  const pdfBuffer = await new Promise(resolve => {
    const doc = new PDFDocument

    doc.text(`Hello ${subject}`, 100, 100)


    const buffers = []

    doc.on('data', buffers.push.bind(buffers))

    doc.on('end', () => {
      const pdfData = Buffer.concat(buffers)


  return {
    statusCode: 200,
    headers: {
      "Content-Type": "application/pdf"
    body: pdfBuffer.toString("base64"),
    isBase64Encoded: true

Any help on why this would succeed locally but fail on production is much appreciated.



hi there,

we would need a little bit more information before we can really say what the problem is.

Can you provide a link to some function logs or a x-nf-request-id? more on the latter here:

Thanks for the prompt reply Perry. The error I provided above comes right from the Netlify function logs and here’s a x-nf-request-id: 2c1d044f-79d7-454b-ac80-ebbbb0ceedb4-3035218

(Also happy to provide the function itself privately if that helps)

All I can see is that for that call, your function returned a 502. Sorry I don’t have any more insight!

I was able to get this function deployed via serverless by adding the following plugins:

  • serverless-apigw-binary
  • serverless-apigwy-binary

Both of these assist in adding binary file support to Amazon API Gateway. I’m wondering if there is a similar adjustment I can make via Netlify to make this function work properly?

Hi @leemartin,

Thanks for writing in. This is an unusual one. We don’t use AWS API Gateway for our functions, we use our own proxy and the Invoke API. I don’t mind filing an issue if there’s something we need to support, but I don’t know what we need to do in this case for this to work properly. Do you know what those two libraries are doing to get this to work? It’s possible you can replicate it within your function already.

Cheers, I’m curious about it too. According to the docs, these plugins:

“…automates the process of adding binary files support in API Gateway.”

In the case of AWS API Gateway, this is simply a switch on the interface. Not sure what is happening behind the scenes. My guess is the plugin is simply glue for a gap in the already exhaustive serverless configuration schema. Any thoughts about the RangeError itself?

hi @leemartin - thank you for your patience. We are still investigating and talking internally about this. We’ll respond here as soon as we have a definitive response!

1 Like

I found a github issue that seems related: https://github.com/nodejs/node/issues/24097, which seems to be version specific. Could you try setting a different node runtime verions as mentioned here? You do this by setting an ENV VAR on your site named AWS_LAMBDA_JS_RUNTIME with a nodejs version (like nodejs10.x). Let me know if that works.

Cheers Dennis. Once I make that adjustment, do I need to take another step for my functions to begin using that new Node version?

You’ll need to re-deploy your function for it to start using the newly configured version.

I pushed some changes to my functions and manually triggered a deploy but I’m not sure if its actually running on the new version I sent in ENV VAR. Is there a way to check this?

(The error persists, by the way)

You could try logging out process.version as mentioned in StackOverflow to find out what version of Node you are running on. With regards to PDFkit, I don’t have any other ideas since that error seems to be specific to the length of an Array in one of the dependencies, somehow.

Thanks for the help Dennis. Looks like I’m running the right version. I actually had to comment out the dependencies in order to get the function to run and log the version. This leads me believe one of those is the culprit. I’ll keep hacking on it.

1 Like

:+1: Hopefully, you’re able to find a solution or workaround.

Hi Lee did you have any success with this?

Negative. I ended up deploying the function with severless which worked fine.