pdf-lib
pdf-lib copied to clipboard
error using pdf-lib with scriptable
What were you trying to do?
hello, i'm trying to use pdf-lib in the ios app scriptable (a javascript environment for creating scripts on ios devices). I am trying to load a pdf (actually, the example "with_update_sections.pdf") but am getting an odd error: "Error on line 8038:28: TypeError: 'pdf' must be of type 'string' or 'Uint8Array' or 'ArrayBuffer', but was actually of type 'NaN'"
I'm not sure what is causing the issue. From my understanding, the scriptable app provides a complete javascript environment, but I'm just getting started using it.. I don't think it's a memory issue, because using either the "minified" or regular versions of the pdf-lib result in the same error..
Thanks in advance for any help!
How did you attempt to do it?
here's the basic script
let pdf_lib=importModule('pdf-lib-1.4.0');
const iCloudFileMgr=FileManager.iCloud(); const myFilePath = iCloudFileMgr.documentsDirectory()+"/with_update_sections.pdf";
await iCloudFileMgr.downloadFileFromiCloud (myFilePath);
const myPDFfileData = await iCloudFileMgr.read(myFilePath); const byteAry = myPDFfileData.getBytes();
var myUint8Array = Uint8Array.from (byteAry);
const myPDFfacilityTO = await pdf_lib.PDFDocument.load (myUint8Array);
What actually happened?
Error message as above: "Error on line 8038:28: TypeError: 'pdf' must be of type 'string' or 'Uint8Array' or 'ArrayBuffer', but was actually of type 'NaN'"
What did you expect to happen?
no error message, and myPDFfacilityTO would contain the loaded PDF
How can we reproduce the issue?
source code for scriptable provided above
Version
1.4.0
What environment are you running pdf-lib in?
Other
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
same thing happens with minified version
I have the same issue in a nestjs app. The odd thing is that it works in the app itself but I get this error when I try to add unit tests using jest. I checked and before using the .load
function, the param I'm sending is a "Buffer" (class Buffer extends Uint8Array
).
const filePath = join(process.cwd(), 'relative-path-to-my-pdf.pdf');
const buffer = fs.readFileSync(filePath);
const pdfDocument = await PDFDocument.load(buffer);
Ran into this error as well writing tests for a CRA app. I've reproduced it with node versions 14,16, 18, and 20. It appears as if the type checking routines are somehow coercing Uint8Array to NaN by using isNaN
. I believe replacing all the usages of isNaN
with Number.isNaN
will fix that problem, but so far I'm struggling to reproduce the library build to verify this.
As a workaround, I've found the following works:
const filePath = join(process.cwd(), 'relative-path-to-my-pdf.pdf');
const buffer = fs.readFileSync(filePath).toString('base64');
const pdfDocument = await PDFDocument.load(buffer);
PDFDocument - load method has:
assertIs(pdf, 'pdf', ['string', Uint8Array, ArrayBuffer]); and those Uint8Array, ArrayBuffer - passed as Uint8ArrayConstructor, ArrayBufferConstructor. and in browser Its never passing validation