Hopding / pdf-lib

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

Set BoundingBox for Images #1529

Open polizoto opened 1 year ago

polizoto commented 1 year ago

What were you trying to do?

I receive the "'Figure' element on a single page with no bounding box" (accessibility) error for every image in my PDF.

Bbox error

I am trying to loop through each image, capture its width and height, and set the /Bbox.

How did you attempt to do it?

I have tried using the following code:

pdfDoc.context.enumerateIndirectObjects().forEach(object => {
   const pdfObject = object[1]
   if (!(pdfObject instanceof PDFRawStream)) return
       const { dict } = pdfObject
       const type = dict.get(PDFName.of('Type'))
       if (type === PDFName.of('XObject')) {
           const subtype = dict.get(PDFName.of('Subtype'))
           if (subtype === PDFName.of('Image')) {
               const width = dict.get(PDFName.of('Width'))
               const height = dict.get(PDFName.of('Height'))
               pdfObject.dict.set(PDFName.of('BBox'), new PDFArray([new PDFNumber(0), new PDFNumber(0), new
               PDFNumber(width), new PDFNumber(height)]) )
            }
        }
})

What actually happened?

The error persists in the PDF after I try to set the Bounding Box.

What did you expect to happen?

What I would expect is for the Missing Bounding Box Error to go away.

How can we reproduce the issue?

Sample PDF:

How to Freeze Your Credit - NerdWallet.pdf

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response

polizoto commented 1 year ago

Note: I am checking the PDF using the PAC 2021 (accessibility) checker.