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

Incorrect result for PDFDocument#getProducer #1571

Open madtrick opened 7 months ago

madtrick commented 7 months ago

What were you trying to do?

I'm reading the document producer after setting a custom value for it.

How did you attempt to do it?

First:

pdf.setProducer('html2read2pdf')

Then, in a different place, after loading the file from disk:

pdf.getProducer()

What actually happened?

If I load the document and read its Producer metadata property the value I get is pdf-lib (https://github.com/Hopding/pdf-lib) instead of the one I set with PDFDocument#setProducer

However if I open the resulting PDF with a viewer like Mac Preview or PDF Expert I see the expected custom value

image image

What did you expect to happen?

I see the Producer I set

How can we reproduce the issue?

import { readFile, writeFile } from 'fs/promises'
import { PDFDocument } from 'pdf-lib'
import { resolve } from 'path'

async function main(): Promise<void> {
  const bytes = await readFile(
    resolve(__dirname, './sample.pdf'),
  )
  const pdf = await PDFDocument.load(bytes)
  pdf.setProducer('html2read2pdf/v1')

  await writeFile(
    resolve(__dirname, './document-with-metadata.pdf'),
    await pdf.save(),
  )

  const pdf2 = await PDFDocument.load(
    await readFile(resolve(__dirname, './document-with-metadata.pdf')),
  )

  console.log(pdf2.getProducer())
}

void main()

sample.pdf

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response