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)
doc.end()
const buffers = []
doc.on('data', buffers.push.bind(buffers))
doc.on('end', () => {
const pdfData = Buffer.concat(buffers)
resolve(pdfData)
})
})
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.
Thanks!
Lee