Bug in function node? pdf-lib

christopher-lambe
Wed, 17 Sep 2025 12:23:39 +0000
General

I have a function node with pdf-lib to extract the first page of a PDF file. The same code works great in VS Code / cmd prompt with Node.js v22.12 but produces this strange error in the Node-Red function node.

"TypeError: `indices` must be of type `Array`, but was actually of type `Array`"

I cannot see what I am doing wrong, and the code works fine Node.js so I can only conclude there is something wrong with the function node. Line 9 produces the error.

I hope someone can find a minute to show me what I am doing wrong. Cheers.

[{"id":"458e16e66760310f","type":"inject","z":"c996c166521b8b9e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":700,"wires":[["c66ff2ee12d45ab7"]]},{"id":"c66ff2ee12d45ab7","type":"file in","z":"c996c166521b8b9e","name":"","filename":"C:\\node-red\\A.pdf","filenameType":"str","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":490,"y":700,"wires":[["962912b07d8f265f"]]},{"id":"962912b07d8f265f","type":"function","z":"c996c166521b8b9e","name":"PDF extract page","func":"//import * as fs from 'fs';\n//import * as pdfLib from 'pdf-lib';\n\nasync function exportPdfPage(pdfBuffer) {\n  const pdfDoc = await pdfLib.PDFDocument.load(pdfBuffer);\n  node.warn(`Found ${pdfDoc.getPageCount() } pages in PDF file`);\n  //console.log(`Found ${pdfDoc.getPageCount() } pages in PDF file`);\n  const newPdfDoc = await pdfLib.PDFDocument.create();\n  const [copiedPage] = await newPdfDoc.copyPages(pdfDoc, [1]);\n  newPdfDoc.addPage(copiedPage);\n  const pdfBytes = await newPdfDoc.save();\n  return pdfBytes;\n}\n\nconst myPdfExportedPage = await exportPdfPage(msg.payload);\n//const myPdfExportedPage = await exportPdfPage(fs.readFileSync(\".\\\\A.pdf\"))\n//console.log(`Extracted PDF file is ${myPdfExportedPage.length} bytes`)\nmsg.payload = myPdfExportedPage;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"pdfLib","module":"pdf-lib"},{"var":"fs","module":"fs"}],"x":770,"y":540,"wires":[["849d6c06cf308c95"]]},{"id":"849d6c06cf308c95","type":"file","z":"c996c166521b8b9e","name":"","filename":"C:\\node-red\\pdfExport.pdf","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":1110,"y":700,"wires":[["d8fe6ee56689069d"]]},{"id":"d8fe6ee56689069d","type":"debug","z":"c996c166521b8b9e","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1340,"y":700,"wires":[]}]

Here is the same code as mjs for node.js

import * as fs from 'fs';
import * as pdfLib from 'pdf-lib';

async function exportPdfPage(pdfBuffer) {
  const pdfDoc = await pdfLib.PDFDocument.load(pdfBuffer);
  //node.warn(`Found ${pdfDoc.getPageCount()} pages in PDF file`);
  console.log(`Found ${pdfDoc.getPageCount()} pages in PDF file`);
  const newPdfDoc = await pdfLib.PDFDocument.create();
  const [copiedPage] = await newPdfDoc.copyPages(pdfDoc, [1]);
  newPdfDoc.addPage(copiedPage);
  const pdfBytes = await newPdfDoc.save();
  return pdfBytes;
}

//const myPdfExportedPage = await exportPdfPage(msg.payload);
const myPdfExportedPage = await exportPdfPage(fs.readFileSync(".\\A.pdf"))
console.log(`Extracted PDF file is ${myPdfExportedPage.length} bytes`)
//msg.payload = myPdfExportedPage;
//return msg;

10 posts - 5 participants