You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python设置PDF文档权限(含编辑、打印权限及防修改)

How to Set PDF Permissions (Editing/Printing Restrictions) with Python

Got it, let's break down how to lock down your PDF's permissions beyond just the open password using Python. You mentioned you're using PyPDF2, which does support granular permission controls—you just need to leverage the right parameters in the encrypt() method.

First, a Quick Primer on PDF Passwords

PDFs have two types of passwords, and they serve different purposes:

  • User Password: What you've already set—this lets someone open the document.
  • Owner Password: This is the key to controlling permissions. Even if someone has the user password, they can't modify restrictions unless they have the owner password. You must set this to enforce editing/printing limits.

Setting Restricted Permissions (Code Example)

Here's a full example that takes a PDF, sets a user password (for opening), an owner password (for permissions), and restricts editing while allowing only low-resolution printing:

from PyPDF2 import PdfWriter, PdfReader

# Load your input PDF
reader = PdfReader("your_input.pdf")
writer = PdfWriter()

# Copy all pages from the input to the writer
for page in reader.pages:
    writer.add_page(page)

# Define which permissions to allow (all others are blocked)
# We'll only allow low-resolution printing here
allowed_permissions = PdfWriter.ALLOW_PRINTING

# Encrypt the PDF with both passwords and apply permissions
writer.encrypt(
    user_pwd="user_open_pass123",  # Password to open the PDF
    owner_pwd="owner_control_pass456",  # Password to change permissions
    permissions=allowed_permissions,
    use_128bit=True  # Use stronger 128-bit encryption (recommended)
)

# Save the restricted PDF
with open("restricted_output.pdf", "wb") as output_file:
    writer.write(output_file)

Common Permission Scenarios

PyPDF2 uses bitmask constants to define allowed actions. Here are some useful combinations:

  • No editing, only high-quality printing allowed:
    allowed_permissions = PdfWriter.ALLOW_PRINTING_HIGH
    
  • Allow screen readers and low-quality printing, block all edits:
    allowed_permissions = PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_SCREENREADERS
    
  • Completely lock down (only allow viewing, no printing/editing):
    allowed_permissions = 0  # No permissions granted besides opening
    
  • Allow form filling but no content edits:
    allowed_permissions = PdfWriter.ALLOW_FILL_FORM
    

Key PyPDF2 Permission Constants to Know

Here's a quick reference for the most relevant constants:

  • ALLOW_PRINTING: Low-resolution printing
  • ALLOW_PRINTING_HIGH: High-resolution printing
  • ALLOW_MODIFY_CONTENTS: Edit document content
  • ALLOW_COPY: Copy text/images
  • ALLOW_MODIFY_ANNOTATIONS: Edit comments/annotations
  • ALLOW_FILL_FORM: Fill in interactive forms
  • ALLOW_SCREENREADERS: Access for screen reader tools
  • ALLOW_ASSEMBLY: Insert/delete/reorder pages

Important Notes

  1. Owner Password is Mandatory: You can't enforce permissions without setting an owner password. If you only set a user password, all editing/printing actions will be allowed by default.
  2. Encryption Strength: Always set use_128bit=True for stronger encryption (the alternative is 40-bit, which is easily bypassed).
  3. Reader Compatibility: These permissions work with Adobe Acrobat and most modern PDF readers, as they follow the PDF 1.4 security standard.
  4. Bypass Limitations: Keep in mind that no PDF permission system is 100% unbreakable, but this will block casual users and comply with standard security workflows.

内容的提问来源于stack exchange,提问作者Applex007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:58:10