diff --git a/PyPDFForm/core/constants.py b/PyPDFForm/core/constants.py index 72f736cb..4c204f73 100644 --- a/PyPDFForm/core/constants.py +++ b/PyPDFForm/core/constants.py @@ -39,4 +39,4 @@ "l": "\u25CF", } -COORDINATE_GRID_MARGIN = 100 +COORDINATE_GRID_FONT_SIZE_MARGIN_RATIO = DEFAULT_FONT_SIZE / 100 diff --git a/PyPDFForm/core/coordinate.py b/PyPDFForm/core/coordinate.py index 7c5dd960..5b2f8902 100644 --- a/PyPDFForm/core/coordinate.py +++ b/PyPDFForm/core/coordinate.py @@ -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 @@ -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)) @@ -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( diff --git a/PyPDFForm/wrapper.py b/PyPDFForm/wrapper.py index df1a6a4f..68e1f511 100644 --- a/PyPDFForm/wrapper.py +++ b/PyPDFForm/wrapper.py @@ -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.""" @@ -146,6 +146,7 @@ def generate_coordinate_grid( remove_all_widgets(self.read()), widget_rect_watermarks(self.read()) ), color, + margin, ) return self diff --git a/docs/coordinate.md b/docs/coordinate.md index b14cdabe..acbb1b8f 100644 --- a/docs/coordinate.md +++ b/docs/coordinate.md @@ -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. diff --git a/pdf_samples/test_generate_coordinate_grid_margin_50.pdf b/pdf_samples/test_generate_coordinate_grid_margin_50.pdf new file mode 100644 index 00000000..6f42eb20 Binary files /dev/null and b/pdf_samples/test_generate_coordinate_grid_margin_50.pdf differ diff --git a/tests/test_functional.py b/tests/test_functional.py index 7f5574e2..54ec19be 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -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