Skip to content

Commit

Permalink
Merge pull request #375 from chinapandaman/PPF-374
Browse files Browse the repository at this point in the history
PPF-374: deprecate user input text offset
  • Loading branch information
chinapandaman authored Jul 17, 2023
2 parents e01a00a + 2d12922 commit c37eb91
Show file tree
Hide file tree
Showing 10 changed files with 6 additions and 118 deletions.
3 changes: 0 additions & 3 deletions PyPDFForm/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ def checkbox_radio_to_draw(
new_element.font = "Helvetica"
new_element.font_size = font_size
new_element.font_color = (0, 0, 0)
new_element.text_x_offset = 0
new_element.text_y_offset = 0
new_element.text_wrap_length = 100

if isinstance(element, Checkbox):
new_element.value = constants.CHECKBOX_TO_DRAW
Expand Down
12 changes: 6 additions & 6 deletions PyPDFForm/core/watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ def draw_text(
if element.comb is True:
for i, char in enumerate(text_to_draw):
canv.drawString(
coordinate_x + element.character_paddings[i] + element.text_x_offset,
coordinate_y + element.text_y_offset,
coordinate_x + element.character_paddings[i],
coordinate_y,
char,
)
elif (
element.text_wrap_length is None or len(text_to_draw) < element.text_wrap_length
):
canv.drawString(
coordinate_x + element.text_x_offset,
coordinate_y + element.text_y_offset,
coordinate_x,
coordinate_y,
text_to_draw,
)
else:
Expand All @@ -76,8 +76,8 @@ def draw_text(

canv.saveState()
canv.translate(
coordinate_x + element.text_x_offset,
coordinate_y + element.text_y_offset,
coordinate_x,
coordinate_y,
)
canv.drawText(text_obj)
canv.restoreState()
Expand Down
2 changes: 0 additions & 2 deletions PyPDFForm/middleware/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@
GLOBAL_FONT = "Helvetica"
GLOBAL_FONT_SIZE = 12
GLOBAL_FONT_COLOR = (0, 0, 0)
GLOBAL_TEXT_X_OFFSET = 0
GLOBAL_TEXT_Y_OFFSET = 0

ELEMENT_TYPES = Union[Text, Checkbox, Radio, Dropdown]
2 changes: 0 additions & 2 deletions PyPDFForm/middleware/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ def dropdown_to_text(dropdown: Dropdown) -> Text:

result.font = constants.GLOBAL_FONT
result.font_color = constants.GLOBAL_FONT_COLOR
result.text_x_offset = constants.GLOBAL_TEXT_X_OFFSET
result.text_y_offset = constants.GLOBAL_TEXT_Y_OFFSET

if dropdown.value is not None:
result.value = (
Expand Down
2 changes: 0 additions & 2 deletions PyPDFForm/middleware/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def __init__(
self.font = None
self.font_size = None
self.font_color = None
self.text_x_offset = None
self.text_y_offset = None
self.text_wrap_length = None
self.max_length = None
self.comb = None
Expand Down
12 changes: 0 additions & 12 deletions PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ def __init__(
each.font_color = kwargs.get(
"global_font_color", constants.GLOBAL_FONT_COLOR
)
each.text_x_offset = kwargs.get(
"global_text_x_offset", constants.GLOBAL_TEXT_X_OFFSET
)
each.text_y_offset = kwargs.get(
"global_text_y_offset", constants.GLOBAL_TEXT_Y_OFFSET
)

def read(self) -> bytes:
"""Reads the file stream of a PDF form."""
Expand Down Expand Up @@ -102,12 +96,6 @@ def draw_text(
new_element.font = kwargs.get("font", constants.GLOBAL_FONT)
new_element.font_size = kwargs.get("font_size", constants.GLOBAL_FONT_SIZE)
new_element.font_color = kwargs.get("font_color", constants.GLOBAL_FONT_COLOR)
new_element.text_x_offset = kwargs.get(
"text_x_offset", constants.GLOBAL_TEXT_X_OFFSET
)
new_element.text_y_offset = kwargs.get(
"text_y_offset", constants.GLOBAL_TEXT_Y_OFFSET
)

watermarks = watermark_core.create_watermarks_and_draw(
self.stream,
Expand Down
41 changes: 0 additions & 41 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,45 +131,6 @@ with open(PATH_TO_FILLED_PDF_FORM, "wb+") as output:
)
```

## Offset texts globally

This example offsets all texts printed on the PDF form by 100 horizontally
and -100 vertically.

```python
import os

from PyPDFForm import PyPDFForm

PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM = os.path.join(
os.path.expanduser("~/Downloads"), "sample_template.pdf"
) # Change this to where you downloaded the sample PDF form

PATH_TO_FILLED_PDF_FORM = os.path.join(
os.path.expanduser("~"), "output.pdf"
) # Change this to where you wish to put your filled PDF form

with open(PATH_TO_FILLED_PDF_FORM, "wb+") as output:
output.write(
PyPDFForm(
PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM,
global_text_x_offset=100,
global_text_y_offset=-100,
)
.fill(
{
"test": "test_1",
"check": True,
"test_2": "test_2",
"check_2": False,
"test_3": "test_3",
"check_3": True,
},
)
.read()
)
```

## Fill with customized elements

A lot of times you may want one or more elements' details like font size and
Expand Down Expand Up @@ -200,8 +161,6 @@ with open(PATH_TO_FILLED_PDF_FORM, "wb+") as output:

pdf_form.elements["test"].font_size = 20
pdf_form.elements["test"].font_color = (1, 0, 0)
pdf_form.elements["test_2"].text_x_offset = 50
pdf_form.elements["test_2"].text_y_offset = -50
pdf_form.elements["test_2"].font_color = (0, 1, 0)
pdf_form.elements["test_3"].font = "LiberationSerif-Italic"
pdf_form.elements["test_3"].font_color = (0, 0, 1)
Expand Down
Binary file modified pdf_samples/sample_filled_customized_elements.pdf
Binary file not shown.
Binary file modified pdf_samples/sample_pdf_with_drawn_text.pdf
Binary file not shown.
50 changes: 0 additions & 50 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ def test_fill_font_liberation_serif_italic(
if isinstance(v, Text):
assert v.font == "LiberationSerif-Italic"
assert v.font_color == constants.GLOBAL_FONT_COLOR
assert v.text_x_offset == constants.GLOBAL_TEXT_X_OFFSET
assert v.text_y_offset == constants.GLOBAL_TEXT_Y_OFFSET


def test_fill_font_20(template_stream, pdf_samples, data_dict, request):
Expand All @@ -108,8 +106,6 @@ def test_fill_font_20(template_stream, pdf_samples, data_dict, request):
assert v.font == constants.GLOBAL_FONT
assert v.font_size == 20
assert v.font_color == constants.GLOBAL_FONT_COLOR
assert v.text_x_offset == constants.GLOBAL_TEXT_X_OFFSET
assert v.text_y_offset == constants.GLOBAL_TEXT_Y_OFFSET


def test_fill_font_color_red(template_stream, pdf_samples, data_dict, request):
Expand All @@ -135,39 +131,6 @@ def test_fill_font_color_red(template_stream, pdf_samples, data_dict, request):
if isinstance(v, Text):
assert v.font == constants.GLOBAL_FONT
assert v.font_color == (1, 0, 0)
assert v.text_x_offset == constants.GLOBAL_TEXT_X_OFFSET
assert v.text_y_offset == constants.GLOBAL_TEXT_Y_OFFSET


def test_fill_offset_100(template_stream, pdf_samples, data_dict, request):
expected_path = os.path.join(pdf_samples, "sample_filled_offset_100.pdf")
with open(expected_path, "rb+") as f:
obj = PyPDFForm(
template_stream,
global_text_x_offset=100,
global_text_y_offset=-100,
).fill(
data_dict,
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

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 isinstance(v, Text):
assert v.font == constants.GLOBAL_FONT
assert v.font_color == constants.GLOBAL_FONT_COLOR
assert v.text_x_offset == 100
assert v.text_y_offset == -100


def test_fill_with_customized_elements(
Expand All @@ -181,8 +144,6 @@ def test_fill_with_customized_elements(
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.fill(data_dict)

Expand All @@ -202,16 +163,7 @@ def test_fill_with_customized_elements(
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 == constants.GLOBAL_TEXT_X_OFFSET
assert obj.elements["test"].text_y_offset == constants.GLOBAL_TEXT_Y_OFFSET

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_3"].font_color == constants.GLOBAL_FONT_COLOR
assert obj.elements["test_3"].text_x_offset == constants.GLOBAL_TEXT_X_OFFSET
assert obj.elements["test_3"].text_y_offset == constants.GLOBAL_TEXT_Y_OFFSET


def test_fill_radiobutton(pdf_samples, template_with_radiobutton_stream, request):
Expand Down Expand Up @@ -263,8 +215,6 @@ def test_draw_text_on_one_page(template_stream, pdf_samples, request):
font=constants.GLOBAL_FONT,
font_size=20,
font_color=(1, 0, 0),
text_x_offset=50,
text_y_offset=50,
)

request.config.results["expected_path"] = expected_path
Expand Down

0 comments on commit c37eb91

Please sign in to comment.