Detect blank space in pdf

def get_last_text_position(pdf_path, page_number):
    doc = fitz.open(pdf_path)
    page = doc[page_number]

    print("Page width:", page.rect.width, "Page height:", page.rect.height)
    # Extract text
    text = page.get_text("text")

    # Get the position of the last line from the bottom
    last_line_index = len(text.splitlines()) - 1
    last_line = text.splitlines()[last_line_index]
    # print("Last line:", last_line)

    # Coordinates of the last text line
    last_line_bbox = page.search_for(last_line)[-1]

    x, y = last_line_bbox.x0, last_line_bbox.y0
    return x, y


def calculate_remaining_space(pdf_path, page_number, required_width, required_height):
    doc = fitz.open(pdf_path)
    page = doc[page_number]

    last_text_bbox = get_last_text_position(pdf_path, page_number, )

    # print(last_text_bbox)

    if last_text_bbox:
        page_width, page_height = page.rect.width, page.rect.height

        # Calculate remaining space based on the last text position
        remaining_width = page_width  # - last_text_bbox[0]
        remaining_height = page_height - last_text_bbox[1]

        # Check if there is enough space for your data
        has_blank_space = (remaining_width >= required_width) and (remaining_height >= required_height)

        return has_blank_space, remaining_width, remaining_height

    return False, None, None

if __name__ == "__main__":
    pdf_path = "test-page.pdf"
    pdf_page = PdfReader(open(pdf_path, "rb"))
    num_of_pages = len(pdf_page.pages)

    for page_number in range(0, num_of_pages):
        print(f"Page: {page_number}")
        required_width = 100  # Replace with the width of the data you want to insert
        required_height = 60  # Replace with the height of the data you want to insert

        blank_space_exists, remaining_width, remaining_height = calculate_remaining_space(
            pdf_path, page_number, required_width, required_height
        )

        if blank_space_exists:
            print(f"Blank space exists. Remaining space: {remaining_width} x {remaining_height}")
        else:
            print("No blank space at the last text position.")

Last updated