def test_fill_with_customized_colors(template_stream, pdf_samples, comparing_size): with open(os.path.join(pdf_samples, "sample_filled_customized_colors.pdf"), "rb+") as f: data_dict = { "test": "test_1", "check": True, "test_2": "test_2", "check_2": False, "test_3": "test_3", "check_3": True, } obj = PyPDFForm(template_stream, simple_mode=False) obj.elements["test"].font_color = (1, 0, 0) obj.elements["test_2"].font_color = (0, 1, 0) obj.elements["test_3"].font_color = (0, 0, 1) obj.fill(data_dict) expected = f.read() assert len(obj.stream) == len(expected) assert obj.stream[:comparing_size] == expected[:comparing_size] for k, v in obj.elements.items(): assert k in data_dict assert v.name in data_dict assert v.value == data_dict[k] assert obj.elements["test"].font_color == (1, 0, 0) assert obj.elements["test_2"].font_color == (0, 1, 0) assert obj.elements["test_3"].font_color == (0, 0, 1)
def test_invalid_text_offset_error(template_stream): obj = PyPDFForm(template_stream, simple_mode=False) try: obj.fill({}, text_x_offset="100", text_y_offset=100) assert False except InvalidTextOffsetError: assert True try: obj.fill({}, text_x_offset=100, text_y_offset="100") assert False except InvalidTextOffsetError: assert True try: obj.elements["test"].text_x_offset = "100" obj.fill({}) assert False except InvalidTextOffsetError: assert True try: obj.elements["test"].text_y_offset = "100" obj.fill({}) assert False except InvalidTextOffsetError: assert True
def test_invalid_image_dimension_error(template_stream, image_stream): try: PyPDFForm(template_stream).draw_image(image_stream, 1, 100, 100, "400", 225) assert False except InvalidImageDimensionError: assert True try: PyPDFForm(template_stream).draw_image(image_stream, 1, 100, 100, 400, "225") assert False except InvalidImageDimensionError: assert True
def test_invalid_image_coordinate_error(template_stream, image_stream): try: PyPDFForm(template_stream).draw_image(image_stream, 1, "100", 100, 400, 225) assert False except InvalidCoordinateError: assert True try: PyPDFForm(template_stream).draw_image(image_stream, 1, 100, "100", 400, 225) assert False except InvalidCoordinateError: assert True
def test_fill_with_customized_elements(template_stream, pdf_samples, comparing_size): with open( os.path.join(pdf_samples, "sample_filled_customized_elements.pdf"), "rb+") as f: data_dict = { "test": "test_1", "check": True, "test_2": "test_2", "check_2": False, "test_3": "test_3", "check_3": True, } obj = PyPDFForm(template_stream, simple_mode=False) obj.elements["test"].font_size = 20 obj.elements["test_2"].text_x_offset = 50 obj.elements["test_2"].text_y_offset = -50 obj.elements["test_2"].text_wrap_length = 1 obj.elements["test_3"].text_wrap_length = 2 obj.fill(data_dict) expected = f.read() assert len(obj.stream) == len(expected) assert obj.stream[:comparing_size] == expected[:comparing_size] for k, v in obj.elements.items(): assert k in data_dict assert v.name in data_dict assert v.value == data_dict[k] assert obj.elements["test"].font_size == 20 assert obj.elements["test"].text_x_offset == 0 assert obj.elements["test"].text_y_offset == 0 assert obj.elements["test"].text_wrap_length == 100 assert obj.elements["test_2"].font_size == 12 assert obj.elements["test_2"].text_x_offset == 50 assert obj.elements["test_2"].text_y_offset == -50 assert obj.elements["test_2"].text_wrap_length == 1 assert obj.elements["test_3"].font_size == 12 assert obj.elements["test_3"].text_x_offset == 0 assert obj.elements["test_3"].text_y_offset == 0 assert obj.elements["test_3"].text_wrap_length == 2
def test_fill_font_color_red(template_stream, pdf_samples, comparing_size): with open(os.path.join(pdf_samples, "sample_filled_font_color_red.pdf"), "rb+") as f: data_dict = { "test": "test_1", "check": True, "test_2": "test_2", "check_2": False, "test_3": "test_3", "check_3": True, } obj = PyPDFForm(template_stream, simple_mode=False).fill( data_dict, font_color=(1, 0, 0), ) expected = f.read() assert len(obj.stream) == len(expected) assert obj.stream[:comparing_size] == expected[:comparing_size] for k, v in obj.elements.items(): assert k in data_dict assert v.name in data_dict assert v.value == data_dict[k] if v.type == "text": assert v.font_size == 12 assert v.font_color == (1, 0, 0) assert v.text_x_offset == 0 assert v.text_y_offset == 0 assert v.text_wrap_length == 100
def test_addition_operator_3_times(template_stream, image_stream, pdf_samples, comparing_size): with open(os.path.join(pdf_samples, "sample_added_3_copies.pdf"), "rb+") as f: result = PyPDFForm() for i in range(3): obj = (PyPDFForm(template_stream).fill( { "test": "{}_test_1".format(i), "check": True, "test_2": "{}_test_2".format(i), "check_2": False, "test_3": "{}_test_3".format(i), "check_3": True, }, ).draw_image(image_stream, i + 1, 100, 100, 400, 225, 0)) result = result + obj expected = f.read() assert result.stream[:comparing_size] == expected[:comparing_size]
def test_fill_simple_mode(template_stream, pdf_samples, comparing_size): with open(os.path.join(pdf_samples, "sample_filled_simple_mode.pdf"), "rb+") as f: obj = PyPDFForm(template_stream).fill( { "test": "test_1", "check": True, "test_2": "test_2", "check_2": False, "test_3": "test_3", "check_3": True, }, ) expected = f.read() assert len(obj.stream) == len(expected) assert obj.stream[:comparing_size] == expected[:comparing_size]
def test_invalid_wrap_length_error(template_stream): obj = PyPDFForm(template_stream, simple_mode=False) try: obj.fill({}, text_wrap_length="100") assert False except InvalidWrapLengthError: assert True try: obj.elements["test"].text_wrap_length = "100" obj.fill({}) except InvalidWrapLengthError: assert True
def test_invalid_font_color_error(template_stream): obj = PyPDFForm(template_stream, simple_mode=False) try: obj.fill({}, font_color=1) assert False except InvalidFontColorError: assert True try: obj.elements["test"].font_color = ("1", 0, 0) obj.fill({}) assert False except InvalidFontColorError: assert True
def test_invalid_font_size_error(template_stream): obj = PyPDFForm(template_stream, simple_mode=False) try: obj.fill({}, font_size="12") assert False except InvalidFontSizeError: assert True try: obj.elements["test"].font_size = "50" obj.fill({}) assert False except InvalidFontSizeError: assert True
def test_draw_text_on_one_page(template_stream, pdf_samples, comparing_size): with open(os.path.join(pdf_samples, "sample_pdf_with_drawn_text.pdf"), "rb+") as f: obj = ( PyPDFForm(template_stream) .fill( { "test": "test_1", "check": True, "test_2": "test_2", "check_2": False, "test_3": "test_3", "check_3": True, }, ) .draw_text("drawn_text", 1, 300, 225) ) expected = f.read() assert len(str(obj.stream)) == len(str(expected)) assert str(obj.stream)[:comparing_size] == str(expected)[:comparing_size]
def test_draw_image_on_one_page( template_stream, image_stream, pdf_samples, comparing_size ): with open(os.path.join(pdf_samples, "sample_pdf_with_image.pdf"), "rb+") as f: obj = ( PyPDFForm(template_stream) .fill( { "test": "test_1", "check": True, "test_2": "test_2", "check_2": False, "test_3": "test_3", "check_3": True, }, ) .draw_image(image_stream, 2, 100, 100, 400, 225, 0) ) expected = f.read() assert obj.stream[:comparing_size] == expected[:comparing_size]
def test_invalid_form_data_error(template_stream): try: PyPDFForm(template_stream).fill("NOT_A_DICT") assert False except InvalidFormDataError: assert True
def test_invalid_image_rotation_angle_error(template_stream, image_stream): try: PyPDFForm(template_stream).draw_image(image_stream, 1, 100, 100, 400, 225, "90") assert False except InvalidImageRotationAngleError: assert True
def test_invalid_page_number_error(template_stream, image_stream): try: PyPDFForm(template_stream).draw_image(image_stream, 1.1, 100, 100, 400, 225) assert False except InvalidPageNumberError: assert True
def test_invalid_editable_parameter_error(template_stream): try: PyPDFForm(template_stream, simple_mode=True).fill({}, editable=1) assert False except InvalidEditableParameterError: assert True
def test_invalid_text_error(template_stream): try: PyPDFForm(template_stream, simple_mode=True).draw_text(1, 1, 0, 0) assert False except InvalidTextError: assert True
def test_invalid_template_error(): try: PyPDFForm(b"BAD_TEMPLATE").fill({}) assert False except InvalidTemplateError: assert True
def test_invalid_image_error(template_stream): try: PyPDFForm(template_stream).draw_image(b"BAD_IMAGE", 1, 100, 100, 400, 225) assert False except InvalidImageError: assert True
def test_invalid_mode_error(template_stream): try: PyPDFForm(template_stream, simple_mode=1).fill({}) assert False except InvalidModeError: assert True