Skip to content

fix(pdf): preserve document metadata when rebuilding an uploaded PDF - #2341

Open
officialasishkumar wants to merge 1 commit into
OpenSignLabs:stagingfrom
officialasishkumar:fix/2140-preserve-pdf-metadata
Open

officialasishkumar wants to merge 1 commit into
OpenSignLabs:stagingfrom
officialasishkumar:fix/2140-preserve-pdf-metadata

Conversation

@officialasishkumar

@officialasishkumar officialasishkumar commented Nov 7, 2026 •

Copy link
Copy Markdown

Fixes #2140

Problem

A PDF signed through OpenSign comes back with its document information blank: keywords, title, author, subject and so on.

The loss happens at upload rather than at signing. Form.jsx always builds the stored file with PDFDocument.create() and copyPages(), even for a single file, and that only carries over the pages. reorderPdfPages rebuilds the document the same way. The later steps (flattenPdf, processPdf, the signature placeholder) load and save the existing document, so they keep the metadata.

Fix

  • copyPdfMetadata(sourceDoc, targetDoc) copies the information dictionary to the rebuilt document, including custom entries and values stored as indirect objects.
  • mergePdfBuffers(pdfBuffers) replaces the inline merge in Form.jsx, which also makes that path testable.
  • reorderPdfPages now calls copyPdfMetadata.

Behaviour to review

  • Merging: a merged document keeps the metadata of the first file. The form already takes the document title from the first file, so the two now agree. If the first file is an image, there are no source fields to keep.
  • Producer and modification date: pdf-lib still sets these when it loads a file, as it does today. The original creator and creation date are kept.
  • Encrypted documents: metadata is skipped if a document is still encrypted when it's loaded, because its strings would be ciphertext. Password-protected uploads are decrypted first, so they keep their metadata.
  • XMP: the /Metadata XMP stream isn't copied. It can make claims such as PDF/A conformance that may no longer hold for a rebuilt file. The issue is about the document information fields, so I left this out.

Tests

The frontend has vitest as a dev dependency but no test script, so I added "test": "vitest run" and src/utils/pdfDocumentUtils.test.js. It covers:

  • single-file upload and multi-file merge
  • custom entries and indirect values
  • a dangling reference in the dictionary
  • encrypted sources and PDFs with no metadata
  • reorderPdfPages, including the resulting page order

With the copy disabled, 6 of the 8 tests fail. The other two (no metadata, encrypted source) don't depend on the copy.

Manual verification

I ran a PDF with keywords and a title through mergePdfBuffers, reorderPdfPages, the server's flattenPdf, the processPdf steps (form flatten and placeholder) and SignPdf. This used a script calling those functions directly, not the browser UI or the running server.

pdfinfo: Title: Contract, Keywords: alpha beta, Pages: 2
pdfsig:  Signature Validation: Signature is Valid.

vite build also succeeds.

Keywords, title, author and other document information were lost as soon
as a PDF was uploaded. The upload form always builds the stored file with
PDFDocument.create() and copyPages(), even for a single file, and that
only carries over the pages. Reordering pages drops the metadata in the
same way. Later steps, including flattening and signing, keep the
metadata, so the signed PDF came back with these fields blank.

Add copyPdfMetadata, which copies the information dictionary to the new
document, including custom entries. Use it when reordering pages, and in
a new mergePdfBuffers helper that replaces the inline merge in the upload
form. A merged document keeps the metadata of the first file, matching
how the form already derives the document title from the first file.

Add vitest coverage and a "test" script, using the vitest dev dependency
that is already declared.

Fixes OpenSignLabs#2140
@vercel

vercel Bot commented Nov 7, 2026

Copy link
Copy Markdown

@officialasishkumar is attempting to deploy a commit to the prafull 's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Preserve PDF Keywords Metadata Field

1 participant