Skillfarm / skills · skillfarm.ai / converted / pdf / scripts / fill_pdf_form_with_annotations.py
| 1 | import json |
| 2 | import sys |
| 3 | |
| 4 | from pypdf import PdfReader, PdfWriter |
| 5 | from pypdf.annotations import FreeText |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | def transform_from_image_coords(bbox, image_width, image_height, pdf_width, pdf_height): |
| 11 | x_scale = pdf_width / image_width |
| 12 | y_scale = pdf_height / image_height |
| 13 | |
| 14 | left = bbox[0] * x_scale |
| 15 | right = bbox[2] * x_scale |
| 16 | |
| 17 | top = pdf_height - (bbox[1] * y_scale) |
| 18 | bottom = pdf_height - (bbox[3] * y_scale) |
| 19 | |
| 20 | return left, bottom, right, top |
| 21 | |
| 22 | |
| 23 | def transform_from_pdf_coords(bbox, pdf_height): |
| 24 | left = bbox[0] |
| 25 | right = bbox[2] |
| 26 | |
| 27 | pypdf_top = pdf_height - bbox[1] |
| 28 | pypdf_bottom = pdf_height - bbox[3] |
| 29 | |
| 30 | return left, pypdf_bottom, right, pypdf_top |
| 31 | |
| 32 | |
| 33 | def fill_pdf_form(input_pdf_path, fields_json_path, output_pdf_path): |
| 34 | |
| 35 | with open(fields_json_path, "r") as f: |
| 36 | fields_data = json.load(f) |
| 37 | |
| 38 | reader = PdfReader(input_pdf_path) |
| 39 | writer = PdfWriter() |
| 40 | |
| 41 | writer.append(reader) |
| 42 | |
| 43 | pdf_dimensions = {} |
| 44 | for i, page in enumerate(reader.pages): |
| 45 | mediabox = page.mediabox |
| 46 | pdf_dimensions[i + 1] = [mediabox.width, mediabox.height] |
| 47 | |
| 48 | annotations = [] |
| 49 | for field in fields_data["form_fields"]: |
| 50 | page_num = field["page_number"] |
| 51 | |
| 52 | page_info = next(p for p in fields_data["pages"] if p["page_number"] == page_num) |
| 53 | pdf_width, pdf_height = pdf_dimensions[page_num] |
| 54 | |
| 55 | if "pdf_width" in page_info: |
| 56 | transformed_entry_box = transform_from_pdf_coords( |
| 57 | field["entry_bounding_box"], |
| 58 | float(pdf_height) |
| 59 | ) |
| 60 | else: |
| 61 | image_width = page_info["image_width"] |
| 62 | image_height = page_info["image_height"] |
| 63 | transformed_entry_box = transform_from_image_coords( |
| 64 | field["entry_bounding_box"], |
| 65 | image_width, image_height, |
| 66 | float(pdf_width), float(pdf_height) |
| 67 | ) |
| 68 | |
| 69 | if "entry_text" not in field or "text" not in field["entry_text"]: |
| 70 | continue |
| 71 | entry_text = field["entry_text"] |
| 72 | text = entry_text["text"] |
| 73 | if not text: |
| 74 | continue |
| 75 | |
| 76 | font_name = entry_text.get("font", "Arial") |
| 77 | font_size = str(entry_text.get("font_size", 14)) + "pt" |
| 78 | font_color = entry_text.get("font_color", "000000") |
| 79 | |
| 80 | annotation = FreeText( |
| 81 | text=text, |
| 82 | rect=transformed_entry_box, |
| 83 | font=font_name, |
| 84 | font_size=font_size, |
| 85 | font_color=font_color, |
| 86 | border_color=None, |
| 87 | background_color=None, |
| 88 | ) |
| 89 | annotations.append(annotation) |
| 90 | writer.add_annotation(page_number=page_num - 1, annotation=annotation) |
| 91 | |
| 92 | with open(output_pdf_path, "wb") as output: |
| 93 | writer.write(output) |
| 94 | |
| 95 | print(f"Successfully filled PDF form and saved to {output_pdf_path}") |
| 96 | print(f"Added {len(annotations)} text annotations") |
| 97 | |
| 98 | |
| 99 | if __name__ == "__main__": |
| 100 | if len(sys.argv) != 4: |
| 101 | print("Usage: fill_pdf_form_with_annotations.py [input pdf] [fields.json] [output pdf]") |
| 102 | sys.exit(1) |
| 103 | input_pdf = sys.argv[1] |
| 104 | fields_json = sys.argv[2] |
| 105 | output_pdf = sys.argv[3] |
| 106 | |
| 107 | fill_pdf_form(input_pdf, fields_json, output_pdf) |