diff --git a/pypdf/_writer.py b/pypdf/_writer.py index cf50ca0bf..904cdd829 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -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, @@ -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): @@ -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 @@ -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(): 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 diff --git a/tests/test_writer.py b/tests/test_writer.py index 97acb2a90..b186d4cee 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -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 + page = writer.insert_blank_page(height=100) + assert page.mediabox.width == 100 writer.insert_blank_page() # without parameters writer.remove_images()