Skip to content

Commit

Permalink
BUG: Switch from trimbox to cropbox when merging pages (#1622)
Browse files Browse the repository at this point in the history
While the old behavior can be considered a bug, people might rely on trimbox being used.
To allow them to switch back from cropbox to trimbox, they can set

    pypdf._page.MERGE_CROP_BOX = "trimbox"

See discussions in #879 and #1426
  • Loading branch information
MartinThoma committed Feb 11, 2023
1 parent f5ac79b commit c70dcd0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 14 additions & 0 deletions docs/user/cropping-and-transforming.md
Expand Up @@ -191,3 +191,17 @@ writer = PdfWriter()
writer.add_page(page)
writer.write("out-pg-transform.pdf")
```

### pypdf._page.MERGE_CROP_BOX

`pypdf<=3.4.0` used to merge the other page with `trimbox`.

`pypdf>3.4.0` changes this behavior to `cropbox`.

In case anybody has good reasons to use/expect `trimbox`, please let me know via
info@martin-thoma.de or via https://github.com/py-pdf/pypdf/pull/1622
In the mean time, you can add the following code to get the old behavior:

```python
pypdf._page.MERGE_CROP_BOX = "trimbox"
```
5 changes: 3 additions & 2 deletions pypdf/_page.py
Expand Up @@ -78,6 +78,7 @@
CUSTOM_RTL_MIN: int = -1
CUSTOM_RTL_MAX: int = -1
CUSTOM_RTL_SPECIAL_CHARS: List[int] = []
MERGE_CROP_BOX = "cropbox" # pypdf<=3.4.0 used 'trimbox'


def set_custom_rtl(
Expand Down Expand Up @@ -844,7 +845,7 @@ def _merge_page(
page2content = page2.get_contents()
if page2content is not None:
page2content = ContentStream(page2content, self.pdf)
rect = page2.trimbox
rect = getattr(page2, MERGE_CROP_BOX)
page2content.operations.insert(
0,
(
Expand Down Expand Up @@ -972,7 +973,7 @@ def _merge_page_writer(
page2content = page2.get_contents()
if page2content is not None:
page2content = ContentStream(page2content, self.pdf)
rect = page2.trimbox
rect = getattr(page2, MERGE_CROP_BOX)
page2content.operations.insert(
0,
(
Expand Down

0 comments on commit c70dcd0

Please sign in to comment.