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

BUG: PDF size increases because of too high float writing precision #2213

Merged
merged 1 commit into from
Sep 24, 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
7 changes: 5 additions & 2 deletions pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ def readFromStream(
return IndirectObject.read_from_stream(stream, pdf)


FLOAT_WRITE_PRECISION = 8 # shall be min 5 digits max, allow user adj
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some part of the comment seems to have got lost.



class FloatObject(float, PdfObject):
def __new__(
cls, value: Union[str, Any] = "0.0", context: Optional[Any] = None
Expand Down Expand Up @@ -409,8 +412,8 @@ def clone(
def myrepr(self) -> str:
if self == 0:
return "0.0"
nb = int(log10(abs(self)))
s = f"{self:.{max(1,16-nb)}f}".rstrip("0").rstrip(".")
nb = FLOAT_WRITE_PRECISION - int(log10(abs(self)))
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
s = f"{self:.{max(1,nb)}f}".rstrip("0").rstrip(".")
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
return s

def __repr__(self) -> str:
Expand Down
Binary file modified resources/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,9 @@ def test_create_string_object_force():
("99900000000000000123", "99900000000000000000"),
("99900000000000000123.456000", "99900000000000000000"),
("0.00000000000000000000123", "0.00000000000000000000123"),
("0.00000123", "0.00000123"),
("0.00000000000000000000123000", "0.00000000000000000000123"),
("-4.6", "-4.6"), # from #1910
# (
# "50032481330523882508234.00000000000000000000123000",
# "50032481330523882508234.00000000000000000000123",
Expand Down
5 changes: 4 additions & 1 deletion tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,9 @@ def test_colors_in_outline_item(pdf_file_path):
reader2 = PdfReader(pdf_file_path)
for outline_item in reader2.outline:
# convert float to string because of mutability
assert [str(c) for c in outline_item.color] == [str(p) for p in purple_rgb]
assert ["%.5f" % c for c in outline_item.color] == [
"%.5f" % p for p in purple_rgb
]


@pytest.mark.samples()
Expand Down Expand Up @@ -1619,6 +1621,7 @@ def test_watermark_rendering(tmp_path):
assert image_similarity(png_path, target_png_path) >= 0.95


@pytest.mark.skipif(GHOSTSCRIPT_BINARY is None, reason="Requires Ghostscript")
def test_watermarking_reportlab_rendering(tmp_path):
"""
This test is showing a rotated+mirrored watermark in pypdf==3.15.4.
Expand Down
Loading