From c6df14ea80d858cf4e8c1440c4b2e45753a8a76d Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Wed, 6 Jul 2022 19:47:04 +0200 Subject: [PATCH] BUG: Page.scale now also scales the cropbox Closes #272 Co-authored-by: Brian Painter --- PyPDF2/_page.py | 13 +++++-------- PyPDF2/generic.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/PyPDF2/_page.py b/PyPDF2/_page.py index 39e04472f..4f85fb15b 100644 --- a/PyPDF2/_page.py +++ b/PyPDF2/_page.py @@ -897,18 +897,15 @@ def scale(self, sx: float, sy: float) -> None: Scale a page by the given factors by applying a transformation matrix to its content and updating the page size. + This updates the mediabox, the cropbox, and the contents + of the page. + :param float sx: The scaling factor on horizontal axis. :param float sy: The scaling factor on vertical axis. """ self.add_transformation((sx, 0, 0, sy, 0, 0)) - self.mediabox = RectangleObject( - ( - float(self.mediabox.left) * sx, - float(self.mediabox.bottom) * sy, - float(self.mediabox.right) * sx, - float(self.mediabox.top) * sy, - ) - ) + self.mediabox = self.mediabox.scale(sx, sy) + self.cropbox = self.cropbox.scale(sx, sy) if PG.VP in self: viewport = self[PG.VP] if isinstance(viewport, ArrayObject): diff --git a/PyPDF2/generic.py b/PyPDF2/generic.py index ecf6ec211..6fa7e16f4 100644 --- a/PyPDF2/generic.py +++ b/PyPDF2/generic.py @@ -1360,6 +1360,16 @@ def _ensure_is_number(self, value: Any) -> Union[FloatObject, NumberObject]: value = FloatObject(value) return value + def scale(self, sx: float, sy: float) -> "RectangleObject": + return RectangleObject( + ( + float(self.left) * sx, + float(self.bottom) * sy, + float(self.right) * sx, + float(self.top) * sy, + ) + ) + def ensureIsNumber( self, value: Any ) -> Union[FloatObject, NumberObject]: # pragma: no cover