Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF metadata not encrypted #865

Closed
lfavole opened this issue Jul 25, 2023 · 7 comments · Fixed by #867
Closed

PDF metadata not encrypted #865

lfavole opened this issue Jul 25, 2023 · 7 comments · Fixed by #867

Comments

@lfavole
Copy link

lfavole commented Jul 25, 2023

Error details
The title of the PDF file doesn't appear correctly because it is not encrypted.
I have tested the code snippet below with the master branch (beb40d4).

Minimal code

from fpdf import FPDF

pdf = FPDF()
pdf.set_title("Title")
pdf.set_encryption("")
pdf.add_page()

pdf.set_font("helvetica", size=12)
pdf.write(txt="Hello world!")
pdf.output("test_enc.pdf")

Environment

  • Operating System: Windows 11
  • Python version: 3.11.4
  • fpdf2 version used: 2.7.4

Proposed solution
Update the file fpdf/output.py at line 81:

class PDFInfo(PDFObject):
    def __init__(
        self,
        title,
        subject,
        author,
        keywords,
        creator,
        producer,
        creation_date: PDFDate,
    ):
        super().__init__()
        self.title = PDFString(title, encrypt=True) if title else None
        self.subject = PDFString(subject, encrypt=True) if subject else None
        self.author = PDFString(author, encrypt=True) if author else None
        self.keywords = PDFString(keywords, encrypt=True) if keywords else None
        self.creator = PDFString(creator, encrypt=True) if creator else None
        self.producer = PDFString(producer, encrypt=True) if producer else None
        self.creation_date = creation_date

I have added encrypt=True to all metadata.

@lfavole lfavole added the bug label Jul 25, 2023
@Tolker-KU
Copy link

Tolker-KU commented Jul 25, 2023

Hi!

Have you tried enabling the encrypt_metadata flag in the call to set_encryption()?

https://github.com/PyFPDF/fpdf2/blob/beb40d4ff0e0d0813b2ed5453286f9454bba5de7/fpdf/fpdf.py#L363

@andersonhc
Copy link
Collaborator

I can see the same problem on our reference files.

https://github.com/PyFPDF/fpdf2/blob/master/test/encryption/encryption_rc4.pdf

The document properties are not being correctly encrypted since #736

@Tolker-KU
Copy link

@andersonhc
Copy link
Collaborator

That file is also generated with encrypt_metdata=False

encrypy_metadata only applies to XMP metadata.
The regular PDF Info always has to be encrypted as a string.

@lfavole
Copy link
Author

lfavole commented Jul 25, 2023

Hello @Tolker-KU,
I have the same problem as in test_encryption_rc4.

I have tried this code snippet:

from fpdf import FPDF

pdf = FPDF()
pdf.set_title("Title")
pdf.set_encryption("", encrypt_metadata=True)
pdf.add_page()

pdf.set_font("helvetica", size=12)
pdf.write(txt="Hello world!")
pdf.output("test_enc.pdf")

but the metadata is still not encrypted correctly.

If I apply my fix, the metadata is correctly encrypted whether encrypt_metadata is True or False.

Moreover, I need the DocumentInformation metadata because it is supported by all the PDF readers.
XMP metadata is not supported by browsers (Chrome and Firefox).

@Lucas-C
Copy link
Member

Lucas-C commented Jul 25, 2023

The document properties are not being correctly encrypted since #736

Damn, that was me... 😅
Sorry about this

So this is indeed a bug.
Is anyone willing to submit a PR to fix it?

@Lucas-C Lucas-C closed this as completed Jul 25, 2023
Lucas-C added a commit that referenced this issue Jul 26, 2023
@Lucas-C
Copy link
Member

Lucas-C commented Jul 26, 2023

Thank you @lfavole for fixing this in PR #867! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants