Skip to content

PyPDF2 to pypdf copying over original document passwords using protected attributes of PDFWriter #2079

Answered by exiledkingcc
mmalik1234 asked this question in Q&A
Discussion options

You must be logged in to vote

@mmalik1234 i have time to verify it today. my bad to ignore the ID and the reference changed in my last reply. i have figured out how to make it work. you can try this one:

def test_reuse_encrypt():
    curr_dir = Path(__file__).parent.resolve()
    with open(curr_dir / "x.pdf", "rb") as ff:
        pdf = ff.read()
    reader = PdfReader(io.BytesIO(pdf))
    reader.decrypt("ownerpass")

    e_cls = reader._encryption
    e_entry = reader.trailer["/Encrypt"]
    # need keep ID, which is used to auth the passwords
    e_id = reader.trailer["/ID"]

    writer = PdfWriter()
    for p in reader.pages:
        writer.add_page(p)

    # writer.encrypt("ownerpass")
    writer._encryption = e_cls

Replies: 8 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@pubpub-zz
Comment options

@mmalik1234
Comment options

Answer selected by mmalik1234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants