扫描受保护的 pdf 文档

发布于 2024-10-03 08:25:15 字数 122 浏览 0 评论 0原文

我目前需要查找目录中的哪些 pdf 是“受保护的文档”。 所有 pdf应该都是不安全的,并且可以通过 xpdf 进行转换,但事实并非如此。如何扫描目录中的所有 pdf 文件以了解它们是否受到保护?

I've currently run into the need to find which pdfs within a directory are "Secured Documents".
All of the pdfs should be unsecured, and convertible via xpdf, however, this is not the case. How could I scan through all the pdfs in a directory to find out whether or not they are secured?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没︽人懂的悲伤 2024-10-10 08:25:15

pypdf 支持解密 PDF。它的 PdfReader 类具有 is_encrypted 属性。

import pypdf
if pypdf.PdfReader("file_name.pdf").is_encrypted:
    print("Rut ro, it's encrypted.")
    # skip file? Write to a log?
else:
    print("We're clear.")
    # Do stuff with the file.

pypdf supports decrypting PDFs. Its PdfReader class has an is_encrypted attribute.

import pypdf
if pypdf.PdfReader("file_name.pdf").is_encrypted:
    print("Rut ro, it's encrypted.")
    # skip file? Write to a log?
else:
    print("We're clear.")
    # Do stuff with the file.
半边脸i 2024-10-10 08:25:15

数字签名的 PDF 是另一种类型的“安全文档”。 pypdf 再次提供了一个解决方案:

import pypdf
def is_signed(pdf_file_path):
    reader=pypdf.PdfReader(pdf_file_path)
    root=reader.trailer['/Root'];l_signed=False
    if acroform:=root.get('/AcroForm'):
        if acroform and (sig:=acroform.get('/SigFlags')):
            l_signed=bool(sig & 1)
    return l_signed

该解决方案的灵感来自于 OCRmyPDF 代码。

Digitally signed PDFs are another type of ‘Secured Document’. pypdf once again provides a solution:

import pypdf
def is_signed(pdf_file_path):
    reader=pypdf.PdfReader(pdf_file_path)
    root=reader.trailer['/Root'];l_signed=False
    if acroform:=root.get('/AcroForm'):
        if acroform and (sig:=acroform.get('/SigFlags')):
            l_signed=bool(sig & 1)
    return l_signed

This solution was inspired by the OCRmyPDF code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文