How to detect read-only files?

18 views
Skip to first unread message

Carl Johnson

unread,
Jan 31, 2017, 9:02:32 PM1/31/17
to PDF::Reader
I need to validate that PDFs uploaded by users don't have the read only flag set. Is there a way to do that with PDF-reader? I don't see anything about it in the documentation, although I do see that you can provide an owner password if you have one.

Thanks,

— Carl Johnson 

James Healy

unread,
Feb 4, 2017, 5:39:31 PM2/4/17
to pdf-r...@googlegroups.com
Hi Carl,

pdf-reader is a low-level library, and other than text extraction it
doesn't have a "porcelain" API for detecting features like encryption.

Still, it does provide a way to walk the PDF internal structure and
detect most features. In this case, every read-only PDF uses
Encryption, which is indicated by the 'Encrypt" key in the document
trailer:

pdf = PDF::Reader.new("my-file.pdf")
pdf.objects.trailer.key?(:Encrypt)

If the Encrypt key exists, then you can fetch the "P" param to see
what features are enabled/disabled:

pdf.objects.trailer.fetch(:Encrypt).fetch(:P)

The P value is an int where each bit position indicates a feature.
There's a table in section 7.6.3.2 of the spec [1] that describes each
bit.

James

[1] http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
> --
> You received this message because you are subscribed to the Google Groups
> "PDF::Reader" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pdf-reader+...@googlegroups.com.
> To post to this group, send email to pdf-r...@googlegroups.com.
> Visit this group at https://groups.google.com/group/pdf-reader.
> For more options, visit https://groups.google.com/d/optout.

Wayne Brissette

unread,
Feb 4, 2017, 5:41:50 PM2/4/17
to pdf-r...@googlegroups.com
James:

Thanks! This is very helpful information! While I currently have no need for this, I can certainly see a need for it.

Wayne
Reply all
Reply to author
Forward
0 new messages