diff --git a/PyPDFForm/wrapper.py b/PyPDFForm/wrapper.py index a1fcb72b..645b1a98 100644 --- a/PyPDFForm/wrapper.py +++ b/PyPDFForm/wrapper.py @@ -181,6 +181,10 @@ def fill( if key in self.widgets: self.widgets[key].value = value + for key, widget in self.widgets.items(): + if isinstance(widget, Text) and isinstance(widget.value, (int, float)): + self.widgets[key].value = str(widget.value) + for key, value in self.widgets.items(): if isinstance(value, Dropdown): self.widgets[key] = dropdown_to_text(value) diff --git a/pdf_samples/sample_filled_boolean_and_ints.pdf b/pdf_samples/sample_filled_boolean_and_ints.pdf new file mode 100644 index 00000000..73a7900a Binary files /dev/null and b/pdf_samples/sample_filled_boolean_and_ints.pdf differ diff --git a/pdf_samples/sample_filled_large_and_small_ints.pdf b/pdf_samples/sample_filled_large_and_small_ints.pdf new file mode 100644 index 00000000..ed484aa3 Binary files /dev/null and b/pdf_samples/sample_filled_large_and_small_ints.pdf differ diff --git a/pdf_samples/sample_filled_negative_and_positive_floats_and_int.pdf b/pdf_samples/sample_filled_negative_and_positive_floats_and_int.pdf new file mode 100644 index 00000000..00ceb2c3 Binary files /dev/null and b/pdf_samples/sample_filled_negative_and_positive_floats_and_int.pdf differ diff --git a/pdf_samples/sample_filled_varied_float_values.pdf b/pdf_samples/sample_filled_varied_float_values.pdf new file mode 100644 index 00000000..9ed8e8a6 Binary files /dev/null and b/pdf_samples/sample_filled_varied_float_values.pdf differ diff --git a/pdf_samples/sample_filled_varied_ints.pdf b/pdf_samples/sample_filled_varied_ints.pdf new file mode 100644 index 00000000..2a03329f Binary files /dev/null and b/pdf_samples/sample_filled_varied_ints.pdf differ diff --git a/pdf_samples/sample_filled_with_empty_string_and_int.pdf b/pdf_samples/sample_filled_with_empty_string_and_int.pdf new file mode 100644 index 00000000..72e16fe0 Binary files /dev/null and b/pdf_samples/sample_filled_with_empty_string_and_int.pdf differ diff --git a/tests/test_fill_method.py b/tests/test_fill_method.py new file mode 100644 index 00000000..5fb00df0 --- /dev/null +++ b/tests/test_fill_method.py @@ -0,0 +1,122 @@ +import os +from PyPDFForm import PdfWrapper + + +def test_fill_with_varied_int_values(pdf_samples, request): + expected_path = os.path.join(pdf_samples, "sample_filled_varied_ints.pdf") + with open(expected_path, "rb+") as f: + obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill( + { + "test": 100, + "test_2": -250, + "test_3": 0, + } + ) + + request.config.results["expected_path"] = expected_path + request.config.results["stream"] = obj.read() + + expected = f.read() + + assert len(obj.read()) == len(expected) + assert obj.read() == expected + + +def test_fill_with_boolean_and_int_values(pdf_samples, request): + expected_path = os.path.join(pdf_samples, "sample_filled_boolean_and_ints.pdf") + with open(expected_path, "rb+") as f: + obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill( + { + "test": 42, + "test_2": True, + "test_3": False, + } + ) + + request.config.results["expected_path"] = expected_path + request.config.results["stream"] = obj.read() + + expected = f.read() + + assert len(obj.read()) == len(expected) + assert obj.read() == expected + + +def test_fill_with_empty_string_and_int(pdf_samples, request): + expected_path = os.path.join(pdf_samples, "sample_filled_with_empty_string_and_int.pdf") + with open(expected_path, "rb+") as f: + obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill( + { + "test": 42, + "test_2": "", + "test_3": 33, + } + ) + + request.config.results["expected_path"] = expected_path + request.config.results["stream"] = obj.read() + + expected = f.read() + + assert len(obj.read()) == len(expected) + assert obj.read() == expected + + +def test_fill_with_large_and_small_ints(pdf_samples, request): + expected_path = os.path.join(pdf_samples, "sample_filled_large_and_small_ints.pdf") + with open(expected_path, "rb+") as f: + obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill( + { + "test": 999999999999, + "test_2": -999999999999, + "test_3": 1, + } + ) + + request.config.results["expected_path"] = expected_path + request.config.results["stream"] = obj.read() + + expected = f.read() + + assert len(obj.read()) == len(expected) + assert obj.read() == expected + + +def test_fill_with_varied_float_values(pdf_samples, request): + expected_path = os.path.join(pdf_samples, "sample_filled_varied_float_values.pdf") + with open(expected_path, "rb+") as f: + obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill( + { + "test": 1.5, + "test_2": 13.8, + "test_3": 543, + } + ) + + request.config.results["expected_path"] = expected_path + request.config.results["stream"] = obj.read() + + expected = f.read() + + assert len(obj.read()) == len(expected) + assert obj.read() == expected + + +def test_fill_with_negative_and_positive_floats_and_int(pdf_samples, request): + expected_path = os.path.join(pdf_samples, "sample_filled_negative_and_positive_floats_and_int.pdf") + with open(expected_path, "rb+") as f: + obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill( + { + "test": -1.5, + "test_2": -6.9, + "test_3": 22, + } + ) + + request.config.results["expected_path"] = expected_path + request.config.results["stream"] = obj.read() + + expected = f.read() + + assert len(obj.read()) == len(expected) + assert obj.read() == expected