Skip to content

Commit

Permalink
Merge pull request #494 from chinapandaman/PPF-493
Browse files Browse the repository at this point in the history
PPF-493: allow users to specify grid view margin
  • Loading branch information
chinapandaman authored Feb 13, 2024
2 parents 220c520 + 6548e2e commit 0dda421
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion PyPDFForm/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"l": "\u25CF",
}

COORDINATE_GRID_MARGIN = 100
COORDINATE_GRID_FONT_SIZE_MARGIN_RATIO = DEFAULT_FONT_SIZE / 100
29 changes: 15 additions & 14 deletions PyPDFForm/core/coordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from reportlab.pdfbase.pdfmetrics import stringWidth

from ..middleware.text import Text
from .constants import (ANNOTATION_RECTANGLE_KEY, COORDINATE_GRID_MARGIN,
DEFAULT_FONT, DEFAULT_FONT_SIZE)
from .constants import (ANNOTATION_RECTANGLE_KEY, COORDINATE_GRID_FONT_SIZE_MARGIN_RATIO,
DEFAULT_FONT)
from .template import (get_char_rect_width, get_widget_alignment,
is_text_multiline)
from .utils import stream_to_io
Expand Down Expand Up @@ -158,7 +158,7 @@ def get_text_line_x_coordinates(
return None


def generate_coordinate_grid(pdf: bytes, color: Tuple[float, float, float]) -> bytes:
def generate_coordinate_grid(pdf: bytes, color: Tuple[float, float, float], margin: float) -> bytes:
"""Creates a grid view for the coordinates of a PDF."""

pdf_file = PdfReader(stream_to_io(pdf))
Expand All @@ -174,34 +174,35 @@ def generate_coordinate_grid(pdf: bytes, color: Tuple[float, float, float]) -> b

r, g, b = color

current = COORDINATE_GRID_MARGIN
current = margin
while current < width:
lines_by_page[i + 1].append([current, 0, current, height, r, g, b])
current += COORDINATE_GRID_MARGIN
current += margin

current = COORDINATE_GRID_MARGIN
current = margin
while current < height:
lines_by_page[i + 1].append([0, current, width, current, r, g, b])
current += COORDINATE_GRID_MARGIN
current += margin

x = COORDINATE_GRID_MARGIN
x = margin
while x < width:
y = COORDINATE_GRID_MARGIN
y = margin
while y < height:
value = f"({x}, {y})"
font_size = margin * COORDINATE_GRID_FONT_SIZE_MARGIN_RATIO
text = Text("new_coordinate", value)
text.font = DEFAULT_FONT
text.font_size = DEFAULT_FONT_SIZE
text.font_size = font_size
text.font_color = color
texts_by_page[i + 1].append(
[
text,
x - stringWidth(value, DEFAULT_FONT, DEFAULT_FONT_SIZE),
y - DEFAULT_FONT_SIZE,
x - stringWidth(value, DEFAULT_FONT, font_size),
y - font_size,
]
)
y += COORDINATE_GRID_MARGIN
x += COORDINATE_GRID_MARGIN
y += margin
x += margin

for page, lines in lines_by_page.items():
watermarks.append(
Expand Down
3 changes: 2 additions & 1 deletion PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def preview(self) -> bytes:
)

def generate_coordinate_grid(
self, color: Tuple[float, float, float] = (1, 0, 0)
self, color: Tuple[float, float, float] = (1, 0, 0), margin: float = 100
) -> PdfWrapper:
"""Inspects a coordinate grid of the PDF."""

Expand All @@ -146,6 +146,7 @@ def generate_coordinate_grid(
remove_all_widgets(self.read()), widget_rect_watermarks(self.read())
),
color,
margin,
)

return self
Expand Down
7 changes: 4 additions & 3 deletions docs/coordinate.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ from PyPDFForm import PdfWrapper

grid_view_pdf = PdfWrapper(
"sample_template.pdf"
).generate_coordinate_grid(color=(1, 0, 0))
).generate_coordinate_grid(color=(1, 0, 0), margin=100)

with open("output.pdf", "wb+") as output:
output.write(grid_view_pdf.read())
```

The `generate_coordinate_grid` method takes one optional parameter `color` which allows you to pick a color for
the grid view. The default color is red.
The `generate_coordinate_grid` method takes two optional parameters. The first one is `color` which allows you to pick
a color for the grid view. The default color is red. The second one is `margin` which allows you to change the
coordinate grid view's margin in points. The default margin is 100 points.
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,17 @@ def test_generate_coordinate_grid(template_stream, pdf_samples, request):

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


def test_generate_coordinate_grid_margin_50(template_stream, pdf_samples, request):
expected_path = os.path.join(pdf_samples, "test_generate_coordinate_grid_margin_50.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(template_stream).generate_coordinate_grid((1, 0, 1), margin=50)

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

0 comments on commit 0dda421

Please sign in to comment.