示例#1
0
def test_fill_non_simple_mode_font_color_red(template_stream, pdf_samples, data_dict):
    with open(
        os.path.join(pdf_samples, "sample_filled_font_color_red.pdf"), "rb+"
    ) as f:
        obj = PyPDFForm(
            template_stream, simple_mode=False, global_font_color=(1, 0, 0)
        ).fill(
            data_dict,
        )

        expected = f.read()

        assert obj.stream == expected

        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 == ElementType.text:
                assert v.font == TextConstants().global_font
                assert v.font_size == TextConstants().global_font_size
                assert v.font_color == (1, 0, 0)
                assert v.text_x_offset == TextConstants().global_text_x_offset
                assert v.text_y_offset == TextConstants().global_text_y_offset
                assert v.text_wrap_length == TextConstants().global_text_wrap_length
示例#2
0
def test_fill_offset_100_v2(template_stream, pdf_samples, data_dict):
    with open(os.path.join(pdf_samples, "sample_filled_offset_100.pdf"),
              "rb+") as f:
        obj = PyPDFForm2(
            template_stream,
            global_text_x_offset=100,
            global_text_y_offset=-100,
        ).fill(data_dict, )

        expected = f.read()

        assert len(obj.read()) == len(expected)
        assert obj.stream == expected

        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 == ElementType.text:
                assert v.font == TextConstants().global_font
                assert v.font_size == TextConstants().global_font_size
                assert v.font_color == TextConstants().global_font_color
                assert v.text_x_offset == 100
                assert v.text_y_offset == -100
                assert v.text_wrap_length == TextConstants(
                ).global_text_wrap_length
示例#3
0
def test_fill_non_simple_mode_with_customized_elements(
    template_stream, pdf_samples, data_dict
):
    with open(
        os.path.join(pdf_samples, "sample_filled_customized_elements.pdf"), "rb+"
    ) as f:
        obj = PyPDFForm(template_stream, simple_mode=False)

        obj.elements["test"].font = "LiberationSerif-Italic"
        obj.elements["test"].font_size = 20
        obj.elements["test"].font_color = (1, 0, 0)
        obj.elements["test_2"].font_color = (0, 1, 0)
        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 obj.stream == expected

        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 == "LiberationSerif-Italic"
        assert obj.elements["test"].font_size == 20
        assert obj.elements["test"].font_color == (1, 0, 0)
        assert (
            obj.elements["test"].text_x_offset == TextConstants().global_text_x_offset
        )
        assert (
            obj.elements["test"].text_y_offset == TextConstants().global_text_y_offset
        )
        assert (
            obj.elements["test"].text_wrap_length
            == TextConstants().global_text_wrap_length
        )

        assert obj.elements["test_2"].font_size == TextConstants().global_font_size
        assert obj.elements["test_2"].font_color == (0, 1, 0)
        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 == TextConstants().global_font_size
        assert obj.elements["test_3"].font_color == TextConstants().global_font_color
        assert (
            obj.elements["test_3"].text_x_offset == TextConstants().global_text_x_offset
        )
        assert (
            obj.elements["test_3"].text_y_offset == TextConstants().global_text_y_offset
        )
        assert obj.elements["test_3"].text_wrap_length == 2
示例#4
0
def test_fill_non_simple_mode_font_liberation_serif_italic(
        template_stream, pdf_samples, font_samples, data_dict):
    with open(os.path.join(font_samples, "LiberationSerif-Italic.ttf"),
              "rb+") as _f:
        stream = _f.read()
        _f.seek(0)
        PyPDFForm.register_font(
            "LiberationSerif-Italic",
            random.choice([
                os.path.join(font_samples, "LiberationSerif-Italic.ttf"), _f,
                stream
            ]),
        )

    with open(
            os.path.join(pdf_samples,
                         "sample_filled_font_liberation_serif_italic.pdf"),
            "rb+",
    ) as f:
        obj = PyPDFForm(template_stream,
                        simple_mode=False,
                        global_font="LiberationSerif-Italic").fill(
                            data_dict, )

        expected = f.read()

        assert len(obj.read()) == len(expected)
        assert obj.stream == expected

        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 == ElementType.text:
                assert v.font == "LiberationSerif-Italic"
                assert v.font_size == TextConstants().global_font_size
                assert v.font_color == TextConstants().global_font_color
                assert v.text_x_offset == TextConstants().global_text_x_offset
                assert v.text_y_offset == TextConstants().global_text_y_offset
                assert v.text_wrap_length == TextConstants(
                ).global_text_wrap_length
示例#5
0
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)
示例#6
0
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)
示例#7
0
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)
示例#8
0
def text_element():
    new_element = ElementMiddleware("new", ElementType.text)
    new_element.value = "drawn_text"
    new_element.font = TextConstants().global_font
    new_element.font_size = TextConstants().global_font_size
    new_element.font_color = TextConstants().global_font_color
    new_element.text_x_offset = TextConstants().global_text_x_offset
    new_element.text_y_offset = TextConstants().global_text_y_offset
    new_element.text_wrap_length = TextConstants().global_text_wrap_length
    new_element.validate_constants()
    new_element.validate_value()
    new_element.validate_text_attributes()

    return new_element
示例#9
0
def test_draw_text_on_one_page_v2(template_stream, pdf_samples):
    with open(os.path.join(pdf_samples, "sample_pdf_with_drawn_text.pdf"),
              "rb+") as f:
        obj = PyPDFForm2(template_stream).draw_text(
            "drawn_text",
            1,
            300,
            225,
            font=TextConstants().global_font,
            font_size=20,
            font_color=(1, 0, 0),
            text_x_offset=50,
            text_y_offset=50,
            text_wrap_length=4,
        )

        expected = f.read()

        assert len(obj.stream) == len(expected)
        assert obj.stream == expected
示例#10
0
def test_draw_text_on_one_page(template_stream, pdf_samples):
    with open(os.path.join(pdf_samples, "sample_pdf_with_drawn_text.pdf"),
              "rb+") as f:
        obj = PyPDFForm(template_stream).draw_text(
            "drawn_text",
            1,
            300,
            225,
            TextConstants().global_font,
            20,
            (1, 0, 0),
            50,
            50,
            4,
        )

        expected = f.read()

        assert len(obj.stream) == len(expected)
        assert obj.stream == expected
示例#11
0
def test_validate_constructor_inputs(pdf_samples, template_stream):
    bad_inputs = [
        "", "True", 1, "12", ("0", "0", "0"), "0", "0", "100", "True"
    ]

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidTemplateError:
        assert True

    bad_inputs[0] = b"bad_stream"

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidModeError:
        assert True

    bad_inputs[1] = False

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidModeError:
        assert True

    bad_inputs[8] = False

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidTemplateError:
        assert True

    bad_inputs[0] = template_stream

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidFontError:
        assert True

    bad_inputs[2] = "bad_font"

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidFontError:
        assert True

    bad_inputs[2] = TextConstants().global_font

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidFontSizeError:
        assert True

    bad_inputs[3] = 12

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidFontColorError:
        assert True

    bad_inputs[4] = (0, 0, 0)

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidTextOffsetError:
        assert True

    bad_inputs[5] = 0

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidTextOffsetError:
        assert True

    bad_inputs[6] = 0

    try:
        PyPDFForm(*bad_inputs)
        assert False
    except InvalidWrapLengthError:
        assert True

    bad_inputs[7] = 100
    bad_inputs[0] = b""
    obj = PyPDFForm(*bad_inputs)
    assert obj.elements == {}

    path = os.path.join(pdf_samples, "sample_template.pdf")
    bad_inputs[0] = path
    obj = PyPDFForm(*bad_inputs)
    assert obj.read() == template_stream

    with open(path, "rb+") as f:
        bad_inputs[0] = f
        obj = PyPDFForm(*bad_inputs)
        assert obj.read() == template_stream
示例#12
0
def test_validate_draw_text_inputs(template_stream):
    bad_inputs = [1, "1", "300", "225", 1, "20", [1, 0, 0], "50", "50", "4"]

    try:
        obj = PyPDFForm(b"bad_stream")
        obj.draw_text(*bad_inputs)
        assert False
    except InvalidTemplateError:
        assert True

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidTextError:
        assert True

    bad_inputs[0] = "drawn_text"

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidPageNumberError:
        assert True

    bad_inputs[1] = 1

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidCoordinateError:
        assert True

    bad_inputs[2] = 300

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidCoordinateError:
        assert True

    bad_inputs[3] = 225

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidFontError:
        assert True

    bad_inputs[4] = "bad_font"

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidFontError:
        assert True

    bad_inputs[4] = TextConstants().global_font

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidFontSizeError:
        assert True

    bad_inputs[5] = 20

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidFontColorError:
        assert True

    bad_inputs[6] = (1, 0, 0)

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidTextOffsetError:
        assert True

    bad_inputs[7] = 50

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidTextOffsetError:
        assert True

    bad_inputs[8] = 50

    try:
        PyPDFForm(template_stream).draw_text(*bad_inputs)
        assert False
    except InvalidWrapLengthError:
        assert True

    bad_inputs[9] = 4
    PyPDFForm(template_stream).draw_text(*bad_inputs)
    assert True
示例#13
0
def test_validate_text_attributes(text_element):
    text_element.font = 0

    try:
        text_element.validate_text_attributes()
        assert False
    except InvalidFontError:
        assert True

    text_element.font = "bad_font"

    try:
        text_element.validate_text_attributes()
        assert False
    except InvalidFontError:
        assert True

    text_element.font = TextConstants().global_font

    text_element.font_size = ""

    try:
        text_element.validate_text_attributes()
        assert False
    except InvalidFontSizeError:
        assert True

    text_element.font_size = 12.5

    text_element.font_color = ""

    try:
        text_element.validate_text_attributes()
        assert False
    except InvalidFontColorError:
        assert True

    text_element.font_color = (1, 0, 0)

    text_element.text_x_offset = ""

    try:
        text_element.validate_text_attributes()
        assert False
    except InvalidTextOffsetError:
        assert True

    text_element.text_x_offset = 100

    text_element.text_y_offset = ""

    try:
        text_element.validate_text_attributes()
        assert False
    except InvalidTextOffsetError:
        assert True

    text_element.text_y_offset = 100

    text_element.text_wrap_length = ""

    try:
        text_element.validate_text_attributes()
        assert False
    except InvalidWrapLengthError:
        assert True

    text_element.text_wrap_length = 100.5

    try:
        text_element.validate_text_attributes()
        assert False
    except InvalidWrapLengthError:
        assert True

    text_element.text_wrap_length = 50

    text_element.validate_text_attributes()
    assert True
示例#14
0
def checkbox_radio_to_draw():
    new_checkbox_element = Element("foo", ElementType.checkbox, True)
    new_radio_element = Element("bar", ElementType.radio, 0)

    new_checkbox_to_draw = Utils().checkbox_radio_to_draw(new_checkbox_element)
    new_radio_to_draw = Utils().checkbox_radio_to_draw(new_radio_element)

    assert new_checkbox_to_draw.value == u"\u2713"
    assert new_checkbox_to_draw.type == ElementType.text
    assert new_checkbox_to_draw.name == "foo"
    assert new_checkbox_to_draw.font == TextConstants().global_font
    assert new_checkbox_to_draw.font_size == TextConstants().global_font_size
    assert new_checkbox_to_draw.font_color == TextConstants().global_font_color
    assert new_checkbox_to_draw.text_x_offset == TextConstants(
    ).global_text_x_offset
    assert new_checkbox_to_draw.text_y_offset == TextConstants(
    ).global_text_y_offset
    assert (new_checkbox_to_draw.text_wrap_length ==
            TextConstants().global_text_wrap_length)

    assert new_radio_to_draw.value == u"\u25CF"
    assert new_radio_to_draw.type == ElementType.text
    assert new_radio_to_draw.name == "bar"
    assert new_radio_to_draw.font == TextConstants().global_font
    assert new_radio_to_draw.font_size == TextConstants().global_font_size
    assert new_radio_to_draw.font_color == TextConstants().global_font_color
    assert new_radio_to_draw.text_x_offset == TextConstants(
    ).global_text_x_offset
    assert new_radio_to_draw.text_y_offset == TextConstants(
    ).global_text_y_offset
    assert new_radio_to_draw.text_wrap_length == TextConstants(
    ).global_text_wrap_length