diff --git a/pypdf/generic/_base.py b/pypdf/generic/_base.py index 6c3e41647..a50bb5faf 100644 --- a/pypdf/generic/_base.py +++ b/pypdf/generic/_base.py @@ -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 + + class FloatObject(float, PdfObject): def __new__( cls, value: Union[str, Any] = "0.0", context: Optional[Any] = None @@ -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))) + s = f"{self:.{max(1,nb)}f}".rstrip("0").rstrip(".") return s def __repr__(self) -> str: diff --git a/resources/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf b/resources/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf index 365637b94..0e9633ac1 100644 Binary files a/resources/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf and b/resources/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf differ diff --git a/tests/test_generic.py b/tests/test_generic.py index 5b0dcd6a1..0e0fff677 100644 --- a/tests/test_generic.py +++ b/tests/test_generic.py @@ -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", diff --git a/tests/test_writer.py b/tests/test_writer.py index 70352833f..358fdb1cf 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -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() @@ -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.