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

Add overwriteWarnings logging capabilities to PdfFileMerger. #243

Merged
merged 1 commit into from Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions PyPDF2/merger.py
Expand Up @@ -65,16 +65,20 @@ class PdfFileMerger(object):
:param bool strict: Determines whether user should be warned of all
problems and also causes some correctable problems to be fatal.
Defaults to ``True``.
:param bool overwriteWarnings: Determines whether to override Python's
``warnings.py`` module with a custom implementation (defaults to
``True``).
"""

def __init__(self, strict=True):
def __init__(self, strict=True, overwriteWarnings=True):
self.inputs = []
self.pages = []
self.output = PdfFileWriter()
self.bookmarks = []
self.named_dests = []
self.id_count = 0
self.strict = strict
self.overwriteWarnings = overwriteWarnings

def merge(self, position, fileobj, bookmark=None, pages=None, import_bookmarks=True):
"""
Expand Down Expand Up @@ -130,7 +134,7 @@ def merge(self, position, fileobj, bookmark=None, pages=None, import_bookmarks=T

# Create a new PdfFileReader instance using the stream
# (either file or BytesIO or StringIO) created above
pdfr = PdfFileReader(fileobj, strict=self.strict)
pdfr = PdfFileReader(fileobj, strict=self.strict, overwriteWarnings=self.overwriteWarnings)
if decryption_key is not None:
pdfr._decryption_key = decryption_key

Expand Down
20 changes: 10 additions & 10 deletions PyPDF2/pdf.py
Expand Up @@ -250,17 +250,17 @@ def addAttachment(self, fname, fdata):

:param str fname: The filename to display.
:param str fdata: The data in the file.

Reference:
https://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
Section 7.11.3
"""

# We need 3 entries:
# * The file's data
# * The /Filespec entry
# * The file's name, which goes in the Catalog


# The entry for the file
""" Sample:
Expand All @@ -272,7 +272,7 @@ def addAttachment(self, fname, fdata):
stream
Hello world!
endstream
endobj
endobj
"""
file_entry = DecodedStreamObject()
file_entry.setData(fdata)
Expand All @@ -291,14 +291,14 @@ def addAttachment(self, fname, fdata):
"""
efEntry = DictionaryObject()
efEntry.update({ NameObject("/F"):file_entry })

filespec = DictionaryObject()
filespec.update({
NameObject("/Type"): NameObject("/Filespec"),
NameObject("/F"): createStringObject(fname), # Perhaps also try TextStringObject
NameObject("/EF"): efEntry
})

# Then create the entry for the root, as it needs a reference to the Filespec
""" Sample:
1 0 obj
Expand All @@ -309,13 +309,13 @@ def addAttachment(self, fname, fdata):
/Names << /EmbeddedFiles << /Names [(hello.txt) 7 0 R] >> >>
>>
endobj

"""
embeddedFilesNamesDictionary = DictionaryObject()
embeddedFilesNamesDictionary.update({
NameObject("/Names"): ArrayObject([createStringObject(fname), filespec])
})

embeddedFilesDictionary = DictionaryObject()
embeddedFilesDictionary.update({
NameObject("/EmbeddedFiles"): embeddedFilesNamesDictionary
Expand All @@ -329,7 +329,7 @@ def appendPagesFromReader(self, reader, after_page_append=None):
"""
Copy pages from reader to writer. Includes an optional callback parameter
which is invoked after pages are appended to the writer.

:param reader: a PdfFileReader object from which to copy page
annotations to this writer object. The writer's annots
will then be updated
Expand Down Expand Up @@ -373,7 +373,7 @@ def updatePageFormFieldValues(self, page, fields):
def cloneReaderDocumentRoot(self, reader):
'''
Copy the reader document root to the writer.

:param reader: PdfFileReader from the document root should be copied.
:callback after_page_append
'''
Expand Down