Skip to content

Conversation

betabites
Copy link

What?

Makes a few line changes that allow users to (optionally) only flatten some fields when flattening a PDF form.

Why?

The purpose of this is to allow some fields to be pre-filled, without fully locking out all fields.

How?

The changes in this PR are intended to build upon the existing flattening logic. In the existing logic, one of the first steps flatten() takes is to fetch all fields in the form, then iterate through them.

  flatten(options: FlattenOptions = { updateFieldAppearances: true }) {
    // ...
    const fields = this.getFields(); // <-- Get all fields

    for (let i = 0, lenFields = fields.length; i < lenFields; i++) { <-- Iterate through them
    // ...

This PR simply allows the PDFField objects to be passed in as a part of the options argument

  flatten(options: FlattenOptions = { updateFieldAppearances: true }): void {
    // ...
    // If no fields were specified, recursively call this.flatten() with all fields 
    if (!options.fields) return this.flatten({...options, fields: this.getFields()});

    // If fields were specified, process them
    const fields = options.fields;

    for (let i = 0, lenFields = fields.length; i < lenFields; i++) {

Testing?

The existing test cases were used. Given that this is only a 3-liner change, and doesn't really add any new features, I thought the existing tests would be enough.

I'm not able to run the integration tests due to a build error with ttypescript? Might need some help with that one...

PS C:\...\pdf-lib> yarn build                
yarn run v1.22.22
$ yarn build:cjs && yarn build:es && yarn build:esm && yarn build:esm:min && yarn build:umd && yarn build:umd:min && yarn build:downlevel-dts
$ ttsc --module commonjs --outDir cjs
C:\...\pdf-lib\node_modules\ttypescript\lib\loadTypescript.js:11
        function __() { this.constructor = d; }
                                         ^

TypeError: Cannot set property constructor of [object Object] which has only a getter
    at new __ (C:\...\pdf-lib\node_modules\ttypescript\lib\loadTypescript.js:11:42)
    at __extends (C:\...\pdf-lib\node_modules\ttypescript\lib\loadTypescript.js:12:84)
    at C:\...\pdf-lib\node_modules\ttypescript\lib\loadTypescript.js:38:5
    at Object.<anonymous> (C:\...\pdf-lib\node_modules\ttypescript\lib\loadTypescript.js:73:2)
    at Module._compile (node:internal/modules/cjs/loader:1730:14)
    at Object..js (node:internal/modules/cjs/loader:1895:10)
    at Module.load (node:internal/modules/cjs/loader:1465:32)
    at Function._load (node:internal/modules/cjs/loader:1282:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)

Node.js v22.15.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

New Dependencies?

No

Screenshots

N/A

Suggested Reading?

No

Anything Else?

Checklist

  • I read CONTRIBUTING.md.
  • I read MAINTAINERSHIP.md#pull-requests.
  • I added/updated unit tests for my changes.
  • I added/updated integration tests for my changes.
  • I ran the integration tests.
  • I tested my changes in Node, Deno, and the browser.
  • I viewed documents produced with my changes in Adobe Acrobat, Foxit Reader, Firefox, and Chrome.
  • I added/updated doc comments for any new/modified public APIs.
  • My changes work for both new and existing PDF files.
  • I ran the linter on my changes.

@betabites betabites marked this pull request as ready for review September 6, 2025 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant