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 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def __init__(

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 @@ -590,7 +590,7 @@ def insert_page(
"""
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 +655,7 @@ def insert_blank_page(
"""
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 +672,12 @@ def insert_blank_page(
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():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this change when this is executed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionality is changed. If width is None we only want to branch if index < self.get_num_pages().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate why? Has this been a bug we need to fix or are there other reasons?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

width and height are kinda symmetric, I do not think they should be handled differently. It is slightly confusing they are handled differently. So this changes this, and also stops branching when width 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
4 changes: 4 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ def writer_operate(writer: PdfWriter) -> None:
writer.page_mode = NameObject("/UseOC")
assert writer._get_page_mode() == "/UseOC"
writer.insert_blank_page(width=100, height=100)
page = writer.insert_blank_page(width=100)
assert page.mediabox.height == 100
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a dedicated test method for this and add assertions for the width and height respectively as well to ensure that it is indeed working correctly.

page = writer.insert_blank_page(height=100)
assert page.mediabox.width == 100
writer.insert_blank_page() # without parameters

writer.remove_images()
Expand Down
Loading