Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added support to only flatten some fields#1758

Open
betabites wants to merge 3 commits intoHopding:masterfrom
betabites:master
Open

Added support to only flatten some fields#1758
betabites wants to merge 3 commits intoHopding:masterfrom
betabites:master

Conversation

Copy link

betabites commented Sep 6, 2025

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.

github-actions bot added the needs-triage label Sep 6, 2025
betabites marked this pull request as ready for review September 6, 2025 02:45
Maxwbh pushed a commit to Maxwbh/pdf-lib that referenced this pull request Jan 31, 2026
Analyzed popular forks and pending PRs to identify potential improvements:
- SVG support (from cantoo-scribe/pdf-lib fork)
- PDF encryption/decryption (from pdf-lib-plus-encrypt fork)
- Incremental save for digital signatures (from pdf-lib-incremental-save)
- Rounded rectangles (from pdfme/pdf-lib fork)
- Hyperlinks support (PR Hopding#1691)
- Form field improvements (PRs Hopding#1748, Hopding#1758)

https://claude.ai/code/session_01T85UagjFeYdf4vUW9pzeyn
Maxwbh added a commit to Maxwbh/pdf-lib that referenced this pull request Mar 2, 2026
Analyzed popular forks and pending PRs to identify potential improvements:
- SVG support (from cantoo-scribe/pdf-lib fork)
- PDF encryption/decryption (from pdf-lib-plus-encrypt fork)
- Incremental save for digital signatures (from pdf-lib-incremental-save)
- Rounded rectangles (from pdfme/pdf-lib fork)
- Hyperlinks support (PR Hopding#1691)
- Form field improvements (PRs Hopding#1748, Hopding#1758)

https://claude.ai/code/session_01T85UagjFeYdf4vUW9pzeyn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant