Загрузка данных



$dir = Join-Path ([Environment]::GetFolderPath("Desktop")) "LR4"
$script = Join-Path $dir "fix_pdf.py"

@'
from pathlib import Path
from shutil import copy2
from pypdf import PdfReader, PdfWriter

folder = Path.home() / "Desktop" / "LR4"
pdf = folder / "LR4_PDF_encrypted.pdf"
backup = folder / "LR4_PDF_unprotected_backup.pdf"
temp = folder / "LR4_PDF_new.pdf"

if not pdf.exists():
    raise FileNotFoundError(f"PDF not found: {pdf}")

if PdfReader(str(pdf)).is_encrypted:
    raise RuntimeError("PDF already has encryption. Check its password.")

# Save original PDF
if not backup.exists():
    copy2(pdf, backup)

# Encrypt PDF
writer = PdfWriter(clone_from=str(pdf))
writer.encrypt(
    user_password="P7d!",
    owner_password="LR4Owner2026",
    algorithm="AES-128"
)
writer.write(str(temp))

# Verify encryption before replacing the file
reader = PdfReader(str(temp))
assert reader.is_encrypted, "Encryption failed"
assert reader.decrypt("") == 0, "Empty password accepted"
assert reader.decrypt("P7d!") != 0, "Password verification failed"

temp.replace(pdf)

print("SUCCESS!")
print("Encryption: AES-128")
print("Password: P7d!")
print("Empty password: REJECTED")
print("Correct password: ACCEPTED")
print("File:", pdf)
'@ | Set-Content -Path $script -Encoding UTF8

py $script