def test_get_elements_by_page(template_with_radiobutton_stream): expected = { 1: { "test": False, "check": False, "radio_1": False, }, 2: { "test_2": False, "check_2": False, "radio_2": False, }, 3: { "test_3": False, "check_3": False, "radio_3": False, }, } for page, elements in (TemplateCore().get_elements_by_page( template_with_radiobutton_stream).items()): for each in elements: expected[page][TemplateCore().get_element_key(each)] = True for page, elements in expected.items(): for k in elements.keys(): assert expected[page][k]
def test_fill(template_stream, data_dict): elements = TemplateMiddleware().build_elements(template_stream) for k, v in data_dict.items(): if k in elements: elements[k].value = v if elements[k].type == ElementType.text: elements[k].font = TextConstants().global_font elements[k].font_size = TextConstants().global_font_size elements[k].font_color = TextConstants().global_font_color elements[k].text_x_offset = TextConstants( ).global_text_x_offset elements[k].text_y_offset = TextConstants( ).global_text_y_offset elements[k].text_wrap_length = TextConstants( ).global_text_wrap_length elements[k].validate_constants() elements[k].validate_value() elements[k].validate_text_attributes() result_stream = Filler().fill(template_stream, elements) assert result_stream != template_stream for element in TemplateCore().iterate_elements(result_stream): key = TemplateCore().get_element_key(element) assert element[ TemplateConstants().field_editable_key] == pdfrw.PdfObject(1) if isinstance(data_dict[key], bool): assert element[TemplateConstants().checkbox_field_value_key] == ( pdfrw.PdfName.Yes if data_dict[key] else pdfrw.PdfName.Off)
def test_iterate_elements_and_get_element_key(template_with_radiobutton_stream, data_dict): for each in TemplateCore().iterate_elements( template_with_radiobutton_stream): data_dict[TemplateCore().get_element_key(each)] = True for k in data_dict.keys(): assert data_dict[k]
def test_iterate_elements_and_get_element_key_sejda(sejda_template, sejda_data): data_dict = {key: False for key in sejda_data.keys()} for each in TemplateCore().iterate_elements(sejda_template, sejda=True): data_dict[TemplateCore().get_element_key(each, sejda=True)] = True for k in data_dict.keys(): assert data_dict[k]
def test_fill_with_radiobutton(template_with_radiobutton_stream, data_dict): elements = TemplateMiddleware().build_elements( template_with_radiobutton_stream) data_dict["radio_1"] = 0 data_dict["radio_2"] = 1 data_dict["radio_3"] = 2 radio_button_tracker = {} for k, v in data_dict.items(): if k in elements: elements[k].value = v if elements[k].type == ElementType.text: elements[k].font = TextConstants().global_font elements[k].font_size = TextConstants().global_font_size elements[k].font_color = TextConstants().global_font_color elements[k].text_x_offset = TextConstants( ).global_text_x_offset elements[k].text_y_offset = TextConstants( ).global_text_y_offset elements[k].text_wrap_length = TextConstants( ).global_text_wrap_length elements[k].validate_constants() elements[k].validate_value() elements[k].validate_text_attributes() result_stream = Filler().fill(template_with_radiobutton_stream, elements) assert result_stream != template_with_radiobutton_stream for element in TemplateCore().iterate_elements(result_stream): key = TemplateCore().get_element_key(element) if isinstance(data_dict[key], bool) or isinstance(data_dict[key], str): assert element[ TemplateConstants().field_editable_key] == pdfrw.PdfObject(1) else: assert element[TemplateConstants().parent_key][ TemplateConstants().field_editable_key] == pdfrw.PdfObject(1) if isinstance(data_dict[key], bool): assert element[TemplateConstants().checkbox_field_value_key] == ( pdfrw.PdfName.Yes if data_dict[key] else pdfrw.PdfName.Off) elif isinstance(data_dict[key], int): if key not in radio_button_tracker: radio_button_tracker[key] = 0 radio_button_tracker[key] += 1 if data_dict[key] == radio_button_tracker[key] - 1: assert element[TemplateConstants(). checkbox_field_value_key] == BasePdfName( "/" + str(data_dict[key]), False) else: assert (element[TemplateConstants().checkbox_field_value_key] == pdfrw.PdfName.Off)
def test_get_draw_text_coordinates(template_stream): for element in TemplateCore().iterate_elements(template_stream): assert TemplateCore().get_draw_text_coordinates(element) == ( float( element[TemplateCoreConstants().annotation_rectangle_key][0]), (float(element[TemplateCoreConstants().annotation_rectangle_key] [1]) + float(element[TemplateCoreConstants(). annotation_rectangle_key][3])) / 2 - 2, )
def test_get_draw_checkbox_radio_coordinates(sejda_template): for element in TemplateCore().iterate_elements(sejda_template): assert TemplateCore().get_draw_checkbox_radio_coordinates(element) == ( (float(element[TemplateCoreConstants().annotation_rectangle_key] [0]) + float(element[TemplateCoreConstants(). annotation_rectangle_key][2])) / 2 - 5, (float(element[TemplateCoreConstants().annotation_rectangle_key] [1]) + float(element[TemplateCoreConstants(). annotation_rectangle_key][3])) / 2 - 4, )
def test_merge_two_pdfs(template_stream, data_dict): template = TemplateCore().get_elements_by_page(template_stream) result = TemplateCore().get_elements_by_page(Utils().merge_two_pdfs( template_stream, template_stream)) page_count = len(template.keys()) merged_page_count = len(result.keys()) for elements in result.values(): for element in elements: assert TemplateCore().get_element_key(element) in data_dict assert page_count * 2 == merged_page_count
def test_generate_stream(template_stream, data_dict): template = TemplateCore().get_elements_by_page(template_stream) result = TemplateCore().get_elements_by_page( Utils.generate_stream(pdfrw.PdfReader(fdata=template_stream))) page_count = len(template.keys()) result_page_count = len(result.keys()) for elements in result.values(): for element in elements: assert TemplateCore().get_element_key(element) in data_dict assert page_count == result_page_count
def test_assign_uuid(template_with_radiobutton_stream, data_dict): for element in TemplateCore().iterate_elements( TemplateCore().assign_uuid(template_with_radiobutton_stream)): key = TemplateCore().get_element_key(element) assert MergeCoreConstants().separator in key key, _uuid = key.split(MergeCoreConstants().separator) assert len(_uuid) == len(uuid.uuid4().hex) data_dict[key] = True for k in data_dict.keys(): assert data_dict[k]
def test_addition_operator_3_times_sejda(sejda_template, pdf_samples, sejda_data): with open(os.path.join(pdf_samples, "sample_added_3_copies_sejda.pdf"), "rb+") as f: result = PyPDFForm() expected = f.read() for i in range(3): result += PyPDFForm(sejda_template, sejda=True).fill(sejda_data) page_count = len(TemplateCore().get_elements_by_page(expected).keys()) result_page_count = len( TemplateCore().get_elements_by_page(result.stream).keys() ) assert page_count == result_page_count assert result.read() == expected
def test_simple_fill(template_stream, data_dict): result_stream = Filler().simple_fill(template_stream, data_dict, False) assert result_stream != template_stream for element in TemplateCore().iterate_elements(result_stream): key = TemplateCore().get_element_key(element) if isinstance(data_dict[key], bool): assert element[TemplateConstants().checkbox_field_value_key] == ( pdfrw.PdfName.Yes if data_dict[key] else pdfrw.PdfName.Off) else: assert (element[TemplateConstants().text_field_value_key][1:-1] == data_dict[key]) assert element[ TemplateConstants().field_editable_key] == pdfrw.PdfObject(1)
def test_fill_sejda(sejda_template, sejda_data): elements = TemplateMiddleware().build_elements(sejda_template, sejda=True) for k, v in elements.items(): if k in sejda_data: v.value = sejda_data[k] if elements[k].type == ElementType.text: elements[k].font = TextConstants().global_font elements[k].font_size = TextConstants().global_font_size elements[k].font_color = TextConstants().global_font_color elements[k].text_x_offset = TextConstants().global_text_x_offset elements[k].text_y_offset = TextConstants().global_text_y_offset elements[k].text_wrap_length = TextConstants( ).global_text_wrap_length elements[k].validate_constants() elements[k].validate_value() elements[k].validate_text_attributes() result_stream = Filler().fill(sejda_template, elements, sejda=True) assert result_stream != template_stream for element in TemplateCore().iterate_elements(result_stream): assert element[TemplateConstants().parent_key][ TemplateConstants().field_editable_key] == pdfrw.PdfObject(1)
def test_get_elements_by_page_sejda(sejda_template): expected = { 1: { "date": False, "year": False, "buyer_name": False, "buyer_address": False, "seller_name": False, "seller_address": False, "make": False, "model": False, "caliber": False, "serial_number": False, "purchase_option": False, "date_of_this_bill": False, "at_future_date": False, "other": False, "other_reason": False, "future_date": False, "future_year": False, "exchange_for": False, }, 2: { "buyer_name_printed": False, "seller_name_printed": False, "buyer_signed_date": False, "seller_signed_date": False, "buyer_dl_number": False, "seller_dl_number": False, "buyer_dl_state": False, "seller_dl_state": False, }, } for page, elements in (TemplateCore().get_elements_by_page( sejda_template, sejda=True).items()): for each in elements: expected[page][TemplateCore().get_element_key(each, sejda=True)] = True for page, elements in expected.items(): for k in elements.keys(): assert expected[page][k]
def test_addition_operator_3_times(template_stream, pdf_samples, data_dict): with open(os.path.join(pdf_samples, "sample_added_3_copies.pdf"), "rb+") as f: result = PyPDFForm() for i in range(3): result += PyPDFForm(template_stream).fill(data_dict, editable=True) page_count = len(TemplateCore().get_elements_by_page(f.read()).keys()) result_page_count = len( TemplateCore().get_elements_by_page(result.stream).keys() ) assert page_count == result_page_count for elements in TemplateCore().get_elements_by_page(result.stream).values(): for element in elements: assert ( TemplateCore() .get_element_key(element) .split(MergeConstants().separator)[0] in data_dict )
def test_fill_v2(template_stream, pdf_samples, data_dict): with open(os.path.join(pdf_samples, "sample_filled.pdf"), "rb+") as f: obj = PyPDFForm2(template_stream).fill(data_dict, ) assert len(obj.read()) == len(obj.stream) assert obj.read() == obj.stream expected = f.read() assert len(obj.stream) == len(expected) assert obj.stream == expected for page, elements in (TemplateCore().get_elements_by_page_v2( obj.read()).items()): assert not elements
def test_simple_fill_with_radiobutton(template_with_radiobutton_stream, data_dict): data_dict["radio_1"] = 0 data_dict["radio_2"] = 1 data_dict["radio_3"] = 2 radio_button_tracker = {} result_stream = Filler().simple_fill(template_with_radiobutton_stream, data_dict, True) assert result_stream != template_with_radiobutton_stream for element in TemplateCore().iterate_elements(result_stream): key = TemplateCore().get_element_key(element) if isinstance(data_dict[key], bool): assert element[TemplateConstants().checkbox_field_value_key] == ( pdfrw.PdfName.Yes if data_dict[key] else pdfrw.PdfName.Off) elif isinstance(data_dict[key], int): if key not in radio_button_tracker: radio_button_tracker[key] = 0 radio_button_tracker[key] += 1 if data_dict[key] == radio_button_tracker[key] - 1: assert element[TemplateConstants(). checkbox_field_value_key] == BasePdfName( "/" + str(data_dict[key]), False) else: assert (element[TemplateConstants().checkbox_field_value_key] == pdfrw.PdfName.Off) else: assert (element[TemplateConstants().text_field_value_key][1:-1] == data_dict[key]) assert element[ TemplateConstants().field_editable_key] != pdfrw.PdfObject(1)
def test_get_element_type(template_stream): type_mapping = { "test": ElementType.text, "check": ElementType.checkbox, "test_2": ElementType.text, "check_2": ElementType.checkbox, "test_3": ElementType.text, "check_3": ElementType.checkbox, } for each in TemplateCore().iterate_elements(template_stream): assert type_mapping[TemplateCore().get_element_key( each)] == TemplateCore().get_element_type(each) read_template_stream = pdfrw.PdfReader(fdata=template_stream) for each in TemplateCore().iterate_elements(read_template_stream): assert type_mapping[TemplateCore().get_element_key( each)] == TemplateCore().get_element_type(each)
def test_get_element_type_sejda(sejda_template): type_mapping = { "date": ElementType.text, "year": ElementType.text, "buyer_name": ElementType.text, "buyer_address": ElementType.text, "seller_name": ElementType.text, "seller_address": ElementType.text, "make": ElementType.text, "model": ElementType.text, "caliber": ElementType.text, "serial_number": ElementType.text, "purchase_option": ElementType.radio, "date_of_this_bill": ElementType.checkbox, "at_future_date": ElementType.checkbox, "other": ElementType.checkbox, "other_reason": ElementType.text, "payment_amount": ElementType.text, "future_date": ElementType.text, "future_year": ElementType.text, "exchange_for": ElementType.text, "buyer_name_printed": ElementType.text, "seller_name_printed": ElementType.text, "buyer_signed_date": ElementType.text, "seller_signed_date": ElementType.text, "buyer_dl_number": ElementType.text, "seller_dl_number": ElementType.text, "buyer_dl_state": ElementType.text, "seller_dl_state": ElementType.text, } for each in TemplateCore().iterate_elements(sejda_template, sejda=True): assert type_mapping[TemplateCore().get_element_key( each, sejda=True)] == TemplateCore().get_element_type(each, sejda=True) read_template_stream = pdfrw.PdfReader(fdata=sejda_template) for each in TemplateCore().iterate_elements(read_template_stream): assert type_mapping[TemplateCore().get_element_key( each, sejda=True)] == TemplateCore().get_element_type(each, sejda=True)
def test_remove_all_elements(template_stream): result = TemplateCore().remove_all_elements(template_stream) assert not TemplateCore().iterate_elements(result)