Skip to content

STY: Tweak PdfWriter #3337

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@

self._info_obj: Optional[PdfObject]
"""The PDF files's document information dictionary,
the Info entry in the PDF file's trailer dictionary."""
defined by Info in the PDF file's trailer dictionary."""

self._ID: Union[ArrayObject, None] = None
"""The PDF file identifier,
Expand Down Expand Up @@ -351,9 +351,8 @@
@xmp_metadata.setter
def xmp_metadata(self, value: Optional[XmpInformation]) -> None:
"""XMP (Extensible Metadata Platform) data."""
if value is None:
if "/Metadata" in self.root_object:
del self.root_object["/Metadata"]
if value is None and "/Metadata" in self.root_object:
del self.root_object["/Metadata"]

Check warning on line 355 in pypdf/_writer.py

View check run for this annotation

Codecov / codecov/patch

pypdf/_writer.py#L355

Added line #L355 was not covered by tests
else:
self.root_object[NameObject("/Metadata")] = value

Expand Down Expand Up @@ -590,7 +589,7 @@
"""
assert self.flattened_pages is not None, "mypy"
if index < 0:
index = len(self.flattened_pages) + index
index += len(self.flattened_pages)
if index < 0:
raise ValueError("Invalid index value")
if index >= len(self.flattened_pages):
Expand Down Expand Up @@ -655,7 +654,7 @@
"""
Insert a blank page to this PDF file and return it.

If no page size is specified, use the size of the last page.
If no page size is specified for a dimension, use the size of the last page.

Args:
width: The width of the new page expressed in default user
Expand All @@ -672,10 +671,12 @@
and previous page does not exist.

"""
if width is None or (height is None and index < self.get_num_pages()):
if (width is None or height is None) and index < self.get_num_pages():
oldpage = self.pages[index]
width = oldpage.mediabox.width
height = oldpage.mediabox.height
if width is None:
width = oldpage.mediabox.width
if height is None:
height = oldpage.mediabox.height
page = PageObject.create_blank_page(self, width, height)
self.insert_page(page, index)
return page
Expand Down
Loading