Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.6k stars 635 forks source link

getFields() returns zero length array on fillable PDF #1620

Open stevehaneytrailblazer opened 3 months ago

stevehaneytrailblazer commented 3 months ago

What were you trying to do?

I was attempting to perform a form fill on a PDF that appears to have many form fields on it. I can type into them in Adobe or when loading via a browser

However, when using the getForm() > getFields() functions there are no fields recognized.

How did you attempt to do it?

See sample code below in reproduce steps section. If you take the attached file and then run through similar code that should recreate.

Using version 1.17.1 ACORD-130-133-Forms.pdf

What actually happened?

Result is field count 0

What did you expect to happen?

I'm not sure why the fields aren't recognized or if maybe they're a different "type" of field.

How can we reproduce the issue?

import { PDFDocument } from "pdf-lib"
import fs from "node:fs";

async function index() {
    try {
        const inputFileName = "path of sample file downloaded locally";
        const inputFile = fs.readFileSync(inputFileName);
        const pdfDoc = await PDFDocument.load(inputFile);
        const form = pdfDoc.getForm();

        const fields = form.getFields();
        console.log('field count', fields.length);
        for(const field of fields) {
            const type = field.constructor.name
            const name = field.getName()
            console.log(`${type}: ${name}`)    
            for(const widget of field.acroField.getWidgets()) {
                console.log('rectangle', widget.getRectangle());
            }
        }
    }
    catch (err) { 
        console.error(`Unexpected failure ${err.message}`, err);
    }
}
index();

export default index;

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response

sayo96 commented 2 months ago

Is there any update on this

akuten1298 commented 1 month ago

@stevehaneytrailblazer any workarounds on this?

stevehaneytrailblazer commented 1 month ago

@akuten1298 - we ended up going with a commercial library from Apryse as we had some other requirements around front end PDF creation and Word Doc template to PDF generation. This library did not have this issue for their Server SDK.