Is there a way to check if a PDF is flat/flattened, using Python or Node.js

462 Views Asked by At

I have a large set of PDFs that are created in different devices and applications. I just need to know if a PDF is flat/flattened or not. I'd prefere solutions that are implementable using Python or Node.js, but any posix CLI tool would also be helpful.

I would appreciate any suggestions even if it works most of the times.

Update

Since it's asked in the comments about my definition of a flat PDF, I'd add two definitions:

  1. Definition 1: a PDF is flat if it only has one layer.
  2. Definition 2: a PDF is flat if it doesn't have any interactive elements.

Any solution that solves the problem either for definition 1 or 2 is fine.

1

There are 1 best solutions below

1
On

Use PyPDF2 library

import PyPDF2

reader = PyPDF2.PdfReader(file)
has_annotations = any(page.annots for page in reader.pages)

if has_annotations:
  print("pdf is not flattened")

else:
  print("pdf is flattened")