Skip to content
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

ENH: Sort computed /ProcSet in a merged page, for reproducibility #1542

Merged
merged 2 commits into from
Jan 22, 2023
Merged
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
12 changes: 7 additions & 5 deletions pypdf/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,14 @@ def _merge_page(
new_resources[NameObject(res)] = new
rename.update(newrename)

# Combine /ProcSet sets.
# Combine /ProcSet sets, making sure there's a consistent order
new_resources[NameObject(RES.PROC_SET)] = ArrayObject(
frozenset(
original_resources.get(RES.PROC_SET, ArrayObject()).get_object()
).union(
frozenset(page2resources.get(RES.PROC_SET, ArrayObject()).get_object())
sorted(
set(
original_resources.get(RES.PROC_SET, ArrayObject()).get_object()
).union(
set(page2resources.get(RES.PROC_SET, ArrayObject()).get_object())
)
)
)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import random
from copy import deepcopy
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -874,6 +875,23 @@ def test_no_resources():
page_one.merge_page(page_two)


def test_merge_page_reproducible_with_proc_set():
page1 = PageObject.create_blank_page(width=100, height=100)
page2 = PageObject.create_blank_page(width=100, height=100)

ordered = sorted(NameObject(f"/{x}") for x in range(20))

shuffled = list(ordered)
random.shuffle(shuffled)

# each page has some overlap in their /ProcSet, and they're in a weird order
page1[NameObject("/Resources")][NameObject("/ProcSet")] = ArrayObject(shuffled[:15])
page2[NameObject("/Resources")][NameObject("/ProcSet")] = ArrayObject(shuffled[5:])
page1.merge_page(page2)

assert page1[NameObject("/Resources")][NameObject("/ProcSet")] == ordered


@pytest.mark.parametrize(
("page1", "page2", "expected_result", "expected_renames"),
[
Expand Down