### Install FillPDF Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Install the FillPDF library using pip. ```APIDOC ## Install FillPDF ### Description Install the fillpdf library using pip. ### Command ```bash pip install fillpdf -U ``` ``` -------------------------------- ### Install fillpdf Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Install the fillpdf library and its dependencies using pip. The -U flag ensures you get the latest version. ```bash pip install fillpdf -U ``` -------------------------------- ### Install fillpdf Source: https://context7.com/t-houssian/fillpdf/llms.txt Install the fillpdf library using pip. The poppler library is optionally required for image-based flattening. ```bash pip install fillpdf # Optional: Required only for flatten_pdf with as_images=True conda install -c conda-forge poppler ``` -------------------------------- ### Example Form Field Data Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb This is an example of the data structure expected for filling PDF forms. Keys should match the form field names obtained from get_form_fields. ```python data_dict = {'Given Name Text Box': '', 'Family Name Text Box': '', 'Address 1 Text Box': '', 'House nr Text Box': '', 'Address 2 Text Box': '', 'Postcode Text Box': '', 'City Text Box': '', 'Country Combo Box': 'Finland', 'Gender List Box': 'Woman', 'Height Formatted Field': '150', 'Driving License Check Box': 'Yes', 'Language 1 Check Box': 'Off', 'Language 2 Check Box': 'Yes', 'Language 3 Check Box': 'Yes', 'Language 4 Check Box': 'Off', 'Language 5 Check Box': 'Off', 'Favourite Colour List Box': 'Blue'} ``` ```python # data_dict = {'Name': '', # 'Dropdown2': 'Feb', # 'Dropdown1': '6', # 'Dropdown3': '2025', # 'Address': 'iejijiejfi', # 'Check Box1': '', # 'Check Box2': 'Yes', # 'Check Box3': '', # 'Check Box4': '', # 'Text5': 'Yo', # 'Group6': '3', # 'Text6': 'Man', # 'Button7': None} # data_dict = {'Address': '', # 'City': '', # 'State': '', # 'Zip': '', # 'Opt in': 'Yes', # 'Gender': 'Female-Value', # 'Radio': 'Radio-2', # 'Colors': ['Green-Value', 'Blue Value']} ``` -------------------------------- ### String Decoding Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Attempts to decode a string. This specific example will raise an AttributeError because 's' is already a string and does not have a 'decode' method. ```python s.decode('utf-8') ``` -------------------------------- ### Insert PDF Data Help Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Display help information for the `insertfillpdf` command, including positional and optional arguments. ```bash insertfillpdf --help ``` ```bash insertfillpdf -h ``` -------------------------------- ### Get PDF Form Fields Source: https://context7.com/t-houssian/fillpdf/llms.txt Retrieves all form fields from a PDF and returns them as a dictionary. Use the 'sort' parameter to get fields alphabetically and 'page_number' to specify a page. ```python from fillpdf import fillpdfs # Get all form fields from a PDF fields = fillpdfs.get_form_fields('application_form.pdf') print(fields) # Output: {'First Name': '', 'Last Name': '', 'Email': '', 'Agree to Terms': 'Off'} # Get fields sorted alphabetically sorted_fields = fillpdfs.get_form_fields('application_form.pdf', sort=True) # Get fields from a specific page only (page numbers are 1-indexed) page2_fields = fillpdfs.get_form_fields('multi_page_form.pdf', page_number=2) ``` -------------------------------- ### Basic PDF Form Filling Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Demonstrates loading JSON data and filling a PDF form. Ensure 'basic-data.json' and 'sample_pdf.pdf' exist in the same directory. ```python import os import json with open('basic-data.json') as f: data = json.load(f) data['Dropdown2']=b'A' single_form_fill('sample_pdf.pdf', data, 'newSamp.pdf') ``` -------------------------------- ### Help Command Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst Display help information for both `extractfillpdf` and `insertfillpdf` commands. ```APIDOC ## Help Command ### Description Displays the help menu for the `extractfillpdf` and `insertfillpdf` commands, outlining their arguments and options. ### Method Command Line Tool ### Endpoint N/A ### Parameters None ### Request Example ```bash extractfillpdf --help ``` ```bash extractfillpdf -h ``` ```bash insertfillpdf --help ``` ```bash insertfillpdf -h ``` ### Response Displays the help menu in the console. ``` -------------------------------- ### Get Form Fields Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Retrieve the form fields from a PDF document. ```APIDOC ## Get Form Fields ### Description This function retrieves all the form fields from a given PDF file and returns them as a dictionary. ### Method ``` get_form_fields(input_pdf_path: str) ``` ### Parameters #### Path Parameters - **input_pdf_path** (str) - Required - Path to the input PDF file. ### Response Example ```json { "Field Name 1": "", "Field Name 2": "Default Value", "Checkbox Field": "/Yes" } ``` ``` -------------------------------- ### Get Coordinate Map Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst Generates a coordinate map for fields within a specified PDF page. ```APIDOC ## POST /api/fillpdf/get_coordinate_map ### Description Generates a coordinate map for fields within a specified PDF page. ### Method POST ### Endpoint /api/fillpdf/get_coordinate_map ### Parameters #### Path Parameters - **input_pdf_path** (string) - Required - Path to the pdf you want the fields from. - **output_map_path** (string) - Required - Path of the new pdf that is generated. #### Query Parameters - **page_number** (integer) - Optional - Number of the page to get the map of. Defaults to 1. ### Request Example ```json { "input_pdf_path": "template.pdf", "output_map_path": "output_map.pdf", "page_number": 1 } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message indicating the map was generated. #### Response Example ```json { "message": "Coordinate map generated successfully." } ``` ``` -------------------------------- ### Get Form Fields Source: https://github.com/t-houssian/fillpdf/blob/main/example.ipynb Extracts all the form field names and their current values from a given PDF file. ```APIDOC ## GET /api/form/fields ### Description Retrieves a dictionary of all form field names and their corresponding values from a fillable PDF. ### Method GET ### Endpoint /api/form/fields ### Parameters #### Query Parameters - **pdf_path** (string) - Required - The path to the PDF file. ### Response #### Success Response (200) - **fields** (object) - A dictionary where keys are form field names and values are their current content. ### Response Example ```json { "fields": { "Text3": "", "Text2": "", "Patient name": "" } } ``` ``` -------------------------------- ### Initialize pdfrw PDF Reader Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Import the `pdfrw` library and create a `PdfReader` object to access PDF content. This is useful for inspecting PDF structure. ```python import pdfrw ``` ```python pdf = pdfrw.PdfReader('samp2.pdf') ``` ```python print(pdf) ``` -------------------------------- ### Complete PDF Form Filling Workflow Source: https://context7.com/t-houssian/fillpdf/llms.txt Python script demonstrating a full workflow: discovering fields, preparing data, filling a PDF, adding an image and text, and flattening the PDF. ```python from fillpdf import fillpdfs # Step 1: Discover available fields print("Available form fields:") fillpdfs.print_form_fields('application.pdf') # Step 2: Get fields as dictionary for reference fields = fillpdfs.get_form_fields('application.pdf') # Step 3: Prepare data to fill application_data = { 'Applicant Name': 'Jane Smith', 'Date of Birth': '1990-05-15', 'Email Address': 'jane.smith@email.com', 'Phone': '555-123-4567', 'Address Line 1': '456 Oak Avenue', 'City': 'San Francisco', 'State': 'California', 'ZIP Code': '94102', 'US Citizen': 'Yes', 'Employment Status': 'Employed', 'Agree to Terms': 'Yes' } # Step 4: Fill the form (keep editable for review) fillpdfs.write_fillable_pdf('application.pdf', 'filled_application.pdf', application_data) # Step 5: Add company logo fillpdfs.place_image('logo.png', 450, 30, 'filled_application.pdf', 'branded_application.pdf', 1, width=100, height=40) # Step 6: Add date stamp fillpdfs.place_text('Received: 2024-01-15', 400, 750, 'branded_application.pdf', 'stamped_application.pdf', 1, font_size=10, font_name='helvetica') # Step 7: Flatten for final distribution fillpdfs.flatten_pdf('stamped_application.pdf', 'final_application.pdf') ``` -------------------------------- ### Get Form Fields Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst Retrieves all form fields from a PDF and returns them as a dictionary. Supports sorting and specifying a page number. ```APIDOC ## GET /api/get_form_fields ### Description Retrieves form field data from a PDF. ### Method GET ### Endpoint /api/get_form_fields ### Parameters #### Query Parameters - **input_pdf_path** (string) - Required - Path to the input PDF file. - **sort** (boolean) - Optional - If True, sorts the dictionary alphabetically. Defaults to False. - **page_number** (integer) - Optional - The page number to retrieve fields from. If not specified, fields from all pages are retrieved. ### Response #### Success Response (200) - **data_dict** (object) - A dictionary containing the form field names as keys and their current values as values. #### Response Example ```json { "data_dict": { "Address 1 Text Box": "", "Driving License Check Box": "Off", "Language 1 Check Box": "Off", "Dropdown List": "Option 1" } } ``` ``` -------------------------------- ### Extract PDF Fields Help Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Display help information for the `extractfillpdf` command, including positional and optional arguments. ```bash extractfillpdf --help ``` ```bash extractfillpdf -h ``` -------------------------------- ### Create a Blank PDF Page Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Generates a blank PDF page with specified width and height in inches, converted to PDF points. ```python def _blank_page(w, h): blank = pdfrw.PageMerge() blank.mbox = [0, 0, w * 72, h * 72] blank = blank.render() return blank ``` -------------------------------- ### Get PDF Form Fields with pdfrw Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Retrieves form fields from a PDF and returns them as a dictionary. Requires the pdfrw library. ```python import pdfrw from pdf2image import convert_from_path # Needs conda install -c conda-forge poppler from PIL import Image from collections import OrderedDict ANNOT_KEY = '/Annots' # key for all annotations within a page ANNOT_FIELD_KEY = '/T' # Name of field. i.e. given ID of field ANNOT_FORM_type = '/FT' # Form type (e.g. text/button) ANNOT_FORM_button = '/Btn' # ID for buttons, i.e. a checkbox ANNOT_FORM_text = '/Tx' # ID for textbox SUBTYPE_KEY = '/Subtype' WIDGET_SUBTYPE_KEY = '/Widget' ANNOT_FIELD_PARENT_KEY = '/Parent' # Parent key for older pdf versions ANNOT_FIELD_KIDS_KEY = '/Kids' # Kids key for older pdf versions ANNOT_VAL_KEY = '/V' ANNOT_RECT_KEY = '/Rect' def get_form_fields(input_pdf_path): """ Retrieves the form fields from a pdf to then be stored as a dictionary and passed to the write_fillable_pdf() function. Uses pdfrw. Parameters --------- input_pdf_path: str Path to the pdf you want the fields from. Returns --------- A dictionary of form fields and their filled values. """ data_dict = {} pdf = pdfrw.PdfReader(input_pdf_path) for page in pdf.pages: annotations = page[ANNOT_KEY] if annotations: for annotation in annotations: if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY: if annotation[ANNOT_FIELD_KEY]: key = annotation[ANNOT_FIELD_KEY][1:-1] data_dict[key] = '' if annotation[ANNOT_VAL_KEY]: value = annotation[ANNOT_VAL_KEY] data_dict[key] = annotation[ANNOT_VAL_KEY] try: if type(annotation[ANNOT_VAL_KEY]) == pdfrw.objects.pdfstring.PdfString: data_dict[key] = pdfrw.objects.PdfString.decode(annotation[ANNOT_VAL_KEY]) except: pass return data_dict ``` -------------------------------- ### Import FillPDF Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Import the necessary functions from the fillpdf library. ```APIDOC ## Import FillPDF ### Description Import the fillpdfs module from the fillpdf library to use its functions. ### Code ```python from fillpdf import fillpdfs ``` ``` -------------------------------- ### Get Coordinate Map Source: https://context7.com/t-houssian/fillpdf/llms.txt Generates a coordinate grid overlay on a PDF page to help determine precise placement coordinates for elements. ```APIDOC ## get_coordinate_map Generates a coordinate grid overlay on a PDF page to help determine precise placement coordinates. ### Method `fillpdfs.get_coordinate_map(input_pdf, output_pdf, page_number=1)` ### Parameters - **input_pdf** (string) - Required - Path to the input PDF file. - **output_pdf** (string) - Required - Path for the output PDF file with the coordinate map. - **page_number** (integer) - Optional - The page number to generate the map for (default is 1). ### Request Example ```python fillpdfs.get_coordinate_map('template.pdf', 'template_with_grid.pdf', page_number=1) ``` ``` -------------------------------- ### Import FillPDF Library Source: https://github.com/t-houssian/fillpdf/blob/main/example.ipynb Import the necessary library for PDF manipulation. ```python from fillpdf import fillpdfs ``` -------------------------------- ### Get PDF Form Fields Source: https://github.com/t-houssian/fillpdf/blob/main/example.ipynb Extract all form fields from a given PDF file. This is useful for understanding the structure of a PDF form before populating it. ```python fillpdfs.get_form_fields("ex.pdf") ``` -------------------------------- ### Flatten PDF Fields Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst Flattens all fields in a PDF, making them uneditable. The 'as_images=True' option converts fields to images, which may require 'poppler' to be installed. ```python from fillpdf import fillpdfs fillpdfs.flatten_pdf('new.pdf', 'newflat.pdf') ``` -------------------------------- ### String Declaration Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb A simple Python string declaration. ```python s = "p" ``` -------------------------------- ### Get Form Fields from PDF Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Retrieves form field data from a PDF. Use 'sort=True' to sort fields alphabetically or specify 'page_number' for a specific page. ```python import fillpdf from fillpdf import fillpdfs fillpdfs.get_form_fields('blank.pdf') ``` -------------------------------- ### Place Dropdown Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst Creates a dropdown menu on a specified page of a PDF with customizable options and appearance. ```APIDOC ## POST /api/fillpdf/place_dropdown ### Description Creates a dropdown menu on a specified page of a PDF with customizable options and appearance. ### Method POST ### Endpoint /api/fillpdf/place_dropdown ### Parameters #### Path Parameters - **field_name** (string) - Required - The name you want attached to the field. - **values** (tuple) - Required - The values for the dropdown menu. The first value becomes the default. - **x** (number) - Required - The x coordinate of the top left corner of the text. - **y** (number) - Required - The y coordinate of the top right corner of the text. - **input_pdf_path** (string) - Required - Path to the pdf you want the fields from. - **output_map_path** (string) - Required - Path of the new pdf that is generated. - **page_number** (integer) - Required - Number of the page to get the map of. #### Query Parameters - **width** (number) - Optional - The width of the image. Defaults to 10. - **height** (number) - Optional - The height of the image. Defaults to 10. - **font_size** (number) - Optional - Size of the text being inserted. Defaults to 12. - **font_name** (string) - Optional - The name of the font type you are using. See [Fonts](https://github.com/t-houssian/fillpdf/blob/main/README.md#fonts). - **fill_color** (tuple) - Optional - The color to use for filling. (0,0,0) = white, (1,1,1) = black. Defaults to (0.8,0.8,0.8). - **font_color** (tuple) - Optional - The color to use for the font. (0,0,0) = white, (1,1,1) = black. Defaults to (0,0,0). ### Request Example ```json { "field_name": "dropdown1", "values": ["Option 1", "Option 2", "Option 3"], "x": 150, "y": 250, "input_pdf_path": "template.pdf", "output_map_path": "output.pdf", "page_number": 1, "width": 100, "height": 20, "font_size": 16, "font_name": "helvetica", "fill_color": [0.8, 0.8, 0.8], "font_color": [0, 0, 0] } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example ```json { "message": "Dropdown placed successfully." } ``` ``` -------------------------------- ### Get PDF Coordinate Map Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Use this Python function to generate a map of field coordinates from a PDF. Specify input and output paths, and optionally the page number. ```python fillpdfs.get_coordinate_map('template.pdf', 'template-2.pdf') ``` -------------------------------- ### Define data for PDF fields Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Prepare a dictionary containing the data to be written into the PDF form fields. Keys should match the field names obtained from `print_form_fields`. ```python data_dict = {'Given Name Text Box': '', 'Family Name Text Box': 'dd', 'Address 1 Text Box': '', 'House nr Text Box': '', 'Address 2 Text Box': '', 'Postcode Text Box': '', 'City Text Box': '', 'Country Combo Box': '', 'Gender List Box': 'Woman', 'Height Formatted Field': '150', 'Driving License Check Box': '/Off', 'Language 1 Check Box': '/Off', 'Language 2 Check Box': '/Yes', 'Language 3 Check Box': '/Off', 'Language 4 Check Box': '/Off', 'Language 5 Check Box': '/Off', 'Favourite Colour List Box': 'Red'} ``` -------------------------------- ### Get PDF form fields Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Retrieve all form fields from a specified PDF file. This function returns a dictionary where keys are field names and values are their current content. ```python p = fillpdfs.print_form_fields('ex2.pdf') ``` -------------------------------- ### Place Dropdown on PDF Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Use to place a dropdown menu on a specific page of a PDF. Requires field name, values, coordinates, input/output paths, and page number. The first value in the tuple becomes the default selection. Optional parameters control size, font, and colors. ```python fillpdfs.place_dropdown('dropField-1', ("Frankfurt", "Hamburg", "Stuttgart", "Hannover", "Berlin", "München", "Köln", "Potsdam",), 0, 0, 'sample_pdf.pdf', 'template-3.pdf', 1, width=100, height=20, font_size=16) ``` -------------------------------- ### Get PDF Form Information Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Retrieves information about form fields in a PDF document. This function is useful for inspecting the structure and types of fields before filling them. Requires the pdfrw library. ```python pdf_form_info(pdfrw.PdfReader('sample_pdf.pdf')) ``` -------------------------------- ### Write Data to Fillable PDF Source: https://github.com/t-houssian/fillpdf/blob/main/example.ipynb Populate a fillable PDF form with data from a dictionary and save it to a new file. ```python fillpdfs.write_fillable_pdf('ex.pdf', 'new.pdf', data_dict) ``` -------------------------------- ### Command Line Tool: insertfillpdf Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Inserts data from a JSON file into a PDF form and saves the result as a new PDF. ```APIDOC ## insertfillpdf ### Description Inserts data from a JSON file into a specified PDF form and saves the output to a new PDF file. If no output path is specified, the output PDF will have '_out.pdf' appended to the input PDF name. ### Method Command Line Tool ### Endpoint insertfillpdf ### Parameters #### Positional Arguments - **input_pdf_path** (string) - Required - Path to the input PDF file. #### Optional Arguments - **-j, --JSON** (string) - Required - Path to the input JSON file containing the data to insert. If not provided, defaults to the input PDF name with a .json extension. - **-o, --output** (string) - Optional - Path for the output PDF file. If not provided, defaults to the input PDF name with '_out.pdf' appended. - **-h, --help** - Optional - Displays the help message and exits. - **-v, --verbose** - Optional - Sets the log level to INFO. - **-vv, --very-verbose** - Optional - Sets the log level to DEBUG. - **--version** - Optional - Shows the program's version number and exits. ### Request Example ```bash insertfillpdf -j blank.json -o new.pdf blank.pdf ``` ### Response Generates a new PDF file with the data inserted into the form fields. ``` -------------------------------- ### Get Text Field Names from PDF Source: https://github.com/t-houssian/fillpdf/blob/main/fillpdf.egg-info/SOURCES.txt Use `get_text_fields` to list all available text field names within a PDF document. This helps in identifying the keys needed for data dictionaries. ```python from fillpdf.utils.text_fields import get_text_fields fields = get_text_fields("path/to/your/template.pdf") print(fields) ``` -------------------------------- ### Create Blank PDF Page Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Creates a blank PDF page with specified width and height in inches. Uses pdfrw.PageMerge for page creation. ```python import pdfrw def _blank_page(w, h): blank = pdfrw.PageMerge() blank.mbox = [0, 0, w * 72, h * 72] blank = blank.render() return blank ``` -------------------------------- ### Define Data Dictionary for PDF Source: https://github.com/t-houssian/fillpdf/blob/main/example.ipynb Create a dictionary where keys are the form field names and values are the data to be filled into those fields. ```python data_dict = {'Text3': '292929', 'Text2': 'Tyler', 'Text4': 'Houssian', 'Text6': 'blank', 'Text5': 'Enter', 'Text7': '292992', 'Text9': 'Yes', 'Text10': 'Hello', 'Text8': 'No', 'Text12': 'Show', 'Text11': 'Maybe', 'Text14': '', 'Text13': '', 'Text17': '', 'Text18': '', 'Text16': '', 'Text20': '', 'Text19': '', 'Text22': '', 'Text21': '', 'Text23': '', 'Text25': '', 'Text26': '', 'Text27': '', 'Text28': '', 'Text29': '', 'Text30': '', 'Text31': '', 'Text32': '', 'Text33': '', 'Text34': '', 'Text35': '', 'Text36': '', 'Kontrollkästchen1': '', 'Kontrollkästchen2': '', 'Kontrollkästchen3': '', 'Kontrollkästchen4': '', 'Text15': '', 'Currency': '', 'Text24': '', 'Patient name': 'None'} ``` -------------------------------- ### Fill PDF Form with Data Source: https://github.com/t-houssian/fillpdf/blob/main/fillpdf.egg-info/SOURCES.txt Use the `fillpdfs` function to populate a PDF form with provided data. Ensure the PDF has form fields and the data is provided as a dictionary. ```python from fillpdf import fillpdfs data_dict = { "field_1": "value_1", "field_2": "value_2" } fillpdfs.fillpdfs(data_dict, "path/to/your/template.pdf", "path/to/output.pdf") ``` -------------------------------- ### Place Text on PDF Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Use to place plain text onto a specific page of a PDF. Requires the text content, coordinates, input/output paths, and page number. Optional parameters control font size and name, and color. ```python fillpdfs.place_text('Yo', 50, 50, 'template-2.pdf', 'template-3.pdf', 1) ``` -------------------------------- ### Write Fillable PDF with PyPDF2 Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Use `write_fillable_pdf` to populate a PDF form with data. Ensure the input PDF and data dictionary are correctly formatted. ```python fillpdfs.write_fillable_pdf('new_samp.pdf', 'samp2.pdf', data_dict) ``` -------------------------------- ### Place Dropdown Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Adds a dropdown menu to a specified page of a PDF, allowing users to select from a list of predefined values. ```APIDOC ## place_dropdown ### Description Creates a dropdown menu on a specified page of a PDF document. ### Method `place_dropdown` ### Parameters #### Arguments - **field_name** (string) - The name to be attached to the dropdown field. - **values** (tuple) - A tuple of strings representing the options for the dropdown. The first value is the default. - **x** (float) - The x-coordinate of the top-left corner of the dropdown. - **y** (float) - The y-coordinate of the top-left corner of the dropdown. - **input_pdf_path** (string) - Path to the input PDF file. - **output_map_path** (string) - Path where the modified PDF will be saved. - **page_number** (integer) - The page number on which to place the dropdown. - **width** (float, optional) - The width of the dropdown. Defaults to 10. - **height** (float, optional) - The height of the dropdown. Defaults to 10. - **font_size** (integer, optional) - The font size for the dropdown text. Defaults to 12. - **font_name** (string, optional) - The name of the font to use. Defaults to None. - **fill_color** (tuple, optional) - The fill color of the dropdown (RGB, 0-1). Defaults to (0.8, 0.8, 0.8). - **font_color** (tuple, optional) - The font color for the dropdown text (RGB, 0-1). Defaults to (0, 0, 0). ### Request Example ```python fillpdfs.place_dropdown('country', ('USA', 'Canada', 'Mexico'), 150, 250, 'input.pdf', 'output.pdf', 2, width=120, height=25, font_size=14) ``` ``` -------------------------------- ### Place Image in PDF Source: https://context7.com/t-houssian/fillpdf/llms.txt Places an image at specified coordinates on a PDF page. Requires input and output PDF filenames, page number, and optionally image dimensions. ```python fillpdfs.place_image('logo.png', 450, 30, 'filled_application.pdf', 'branded_application.pdf', 1, width=100, height=40) ``` -------------------------------- ### Place Image on PDF Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Use to place an image file onto a specific page of a PDF. Requires the image file name, coordinates, input/output paths, and page number. Optional parameters control the image width and height. ```python fillpdfs.place_image('mush.png', 50, 50, 'template-2.pdf', 'template-3.pdf', 1, width=200, height=200) ``` -------------------------------- ### Place Image on PDF Page Source: https://context7.com/t-houssian/fillpdf/llms.txt Inserts an image at specified coordinates on a PDF page. Use get_coordinate_map to determine precise placement coordinates. ```python from fillpdf import fillpdfs ``` -------------------------------- ### Generate PDF Coordinate Map Source: https://context7.com/t-houssian/fillpdf/llms.txt Creates a visual coordinate grid overlay on a specified PDF page to aid in determining precise placement coordinates for elements. The grid displays X and Y coordinates at 50-point intervals. ```python fillpdfs.get_coordinate_map('template.pdf', 'template_with_grid.pdf', page_number=1) ``` -------------------------------- ### Fill PDF Form from JSON Data Source: https://context7.com/t-houssian/fillpdf/llms.txt Command-line tool to fill a PDF form using data from a JSON file. Auto-detects filenames or allows explicit specification with -j and -o. ```bash insertfillpdf blank_form.pdf ``` ```bash insertfillpdf blank_form.pdf -j form_data.json -o filled_form.pdf ``` ```bash insertfillpdf blank_form.pdf -j data.json -o output.pdf -v ``` ```json { "First Name": "John", "Last Name": "Doe", "Email": "john.doe@example.com", "Agree to Terms": "Yes", "Country": "United States" } ``` ```bash insertfillpdf --help ``` -------------------------------- ### Read PDF Form Fields Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Demonstrates how to read and print the type and value of form fields within a PDF. This is useful for debugging and understanding the structure of PDF form elements. ```python pdf = pdfrw.PdfReader('ex2.pdf') for page in pdf.pages: annotations = page['/Annots'] if annotations is None: continue for annotation in annotations: if annotation['/Subtype']=='/Widget': if not annotation['/T']: annotation=annotation['/Parent'] if annotation['/T']: print(type(annotation['/T'])) key = annotation['/T'].to_unicode() print(key) ``` -------------------------------- ### Place Static Text on PDF Page Source: https://context7.com/t-houssian/fillpdf/llms.txt Inserts static text at specified coordinates. Supports basic text placement and styled text with custom font size, name, and color. ```python fillpdfs.place_text( 'CONFIDENTIAL', 50, 50, 'document.pdf', 'stamped_document.pdf', page_number=1 ) ``` ```python fillpdfs.place_text( 'Approved: 2024-01-15', 400, 750, 'report.pdf', 'approved_report.pdf', page_number=1, font_size=14, font_name='helvetica-bold', # See available fonts below color=(0, 0, 1) # RGB tuple: blue color ) ``` -------------------------------- ### Place Dropdown Widget on PDF Page Source: https://context7.com/t-houssian/fillpdf/llms.txt Creates an interactive dropdown or listbox widget on a PDF page with a specified set of options. Supports custom styling for background and font colors. ```python cities = ("New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia") fillpdfs.place_dropdown( 'city_select', cities, 100, 150, 'form.pdf', 'form_with_dropdown.pdf', page_number=1, width=150, height=25, font_size=12 ) ``` ```python countries = ("United States", "Canada", "Mexico", "United Kingdom", "Germany", "France") fillpdfs.place_dropdown( 'country_select', countries, 100, 200, 'form.pdf', 'form_with_country.pdf', page_number=1, width=180, height=25, font_size=11, fill_color=(1, 1, 1), font_color=(0, 0, 0) ) ``` -------------------------------- ### Write Fillable PDF Source: https://github.com/t-houssian/fillpdf/blob/main/example.ipynb Fills a PDF form with provided data and saves it as a new PDF file. ```APIDOC ## POST /api/form/write ### Description Fills the fields of a fillable PDF form with the data provided in a dictionary and saves the output to a new PDF file. ### Method POST ### Endpoint /api/form/write ### Parameters #### Request Body - **input_pdf_path** (string) - Required - The path to the original fillable PDF file. - **output_pdf_path** (string) - Required - The path where the new filled PDF will be saved. - **data** (object) - Required - A dictionary containing the field names and their corresponding values to fill. ### Request Example ```json { "input_pdf_path": "ex.pdf", "output_pdf_path": "new.pdf", "data": { "Text2": "Tyler", "Text4": "Houssian", "Patient name": "None" } } ``` ### Response #### Success Response (200) - **message** (string) - A confirmation message indicating the PDF was successfully filled. #### Response Example ```json { "message": "Successfully filled PDF saved to new.pdf" } ``` ``` -------------------------------- ### Place Image on PDF Page Source: https://context7.com/t-houssian/fillpdf/llms.txt Use to place an image at specific coordinates on a PDF page. Specify the image path, coordinates, input/output PDF files, and optional page number, width, and height. ```python fillpdfs.place_image( 'company_logo.png', # Image file path 50, # X coordinate (from left) 50, # Y coordinate (from top) 'template.pdf', # Input PDF 'with_logo.pdf', # Output PDF page_number=1, width=200, # Image width in points height=100 # Image height in points ) ``` ```python fillpdfs.place_image( 'signature.png', 350, 700, 'contract.pdf', 'signed_contract.pdf', page_number=1, width=150, height=50 ) ``` -------------------------------- ### Batch Fill PDF Forms from Template Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Fills multiple PDF forms from a template file using a list of data records. Handles double-sided printing and page splicing. Requires the pdfrw library and template file. ```python def batch_form_fill(template, data, out_file, double_sided=False, splice=-1): # deepcopy does not work on PdfDict so we keep the raw inputs # so we can may copies of the Pdf structures by reparsing the # raw data with open(template, 'rb') as f: pdf_data = f.read() writer = pdfrw.PdfWriter() for idx, record in enumerate(data): pdf = pdfrw.PdfReader(fdata=pdf_data) # Create a new instance each time out_pdf = fill_form(pdf, record, f'{-idx:04d}') if len(out_pdf.pages) % 2 == 1 and double_sided: if splice != 0: writer.addpages(out_pdf.pages[0:splice]) writer.addpage(_blank_page(8.5, 11)) if splice != -1: writer.addpages(out_pdf.pages[splice:]) else: writer.addpages(out_pdf.pages) writer.trailer.Root.AcroForm = pdfrw.PdfReader(fdata=pdf_data).Root.AcroForm writer.trailer.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))) writer.write(out_file) ``` -------------------------------- ### Place Dropdown on PDF Source: https://context7.com/t-houssian/fillpdf/llms.txt Places an interactive dropdown or listbox widget on a PDF page with predefined options. Supports styling. ```APIDOC ## place_dropdown Places an interactive dropdown/listbox widget on a PDF page with predefined options. ### Method `fillpdfs.place_dropdown(field_name, options, x_coord, y_coord, input_pdf, output_pdf, page_number=1, width=100, height=25, font_size=10, font_color=(0, 0, 0), fill_color=None)` ### Parameters - **field_name** (string) - Required - A unique name for the dropdown field. - **options** (tuple) - Required - A tuple of strings representing the available options in the dropdown. - **x_coord** (integer) - Required - X coordinate from the left edge of the page. - **y_coord** (integer) - Required - Y coordinate from the top edge of the page. - **input_pdf** (string) - Required - Path to the input PDF file. - **output_pdf** (string) - Required - Path for the output PDF file. - **page_number** (integer) - Optional - The page number to place the dropdown on (default is 1). - **width** (integer) - Optional - The width of the dropdown in points (default is 100). - **height** (integer) - Optional - The height of the dropdown in points (default is 25). - **font_size** (integer) - Optional - The font size for the text within the dropdown (default is 10). - **font_color** (tuple) - Optional - An RGB tuple for the text color (default is black (0, 0, 0)). - **fill_color** (tuple) - Optional - An RGB tuple for the background fill color of the dropdown (default is no fill). ### Request Example ```python cities = ("New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia") fillpdfs.place_dropdown( 'city_select', cities, 100, 150, 'form.pdf', 'form_with_dropdown.pdf', page_number=1, width=150, height=25, font_size=12 ) countries = ("United States", "Canada", "Mexico", "United Kingdom", "Germany", "France") fillpdfs.place_dropdown( 'country_select', countries, 100, 200, 'form.pdf', 'form_with_country.pdf', page_number=1, width=180, height=25, font_size=11, fill_color=(1, 1, 1), font_color=(0, 0, 0) ) ``` ``` -------------------------------- ### Main Form Filling Function Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb A dictionary mapping field types to their corresponding helper functions for filling PDF forms. ```python def fill_form(in_pdf, data, suffix=None): fillers = {'checkbox': _checkbox, 'list': _listbox, ``` -------------------------------- ### Place Text Box on PDF Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Use to place a text box with pre-filled text on a specific page of a PDF. Requires field name, pre-filled text, coordinates, input/output paths, and page number. Optional parameters control size, font, and colors. ```python fillpdfs.place_text_box('form text', 'textfield-1', 0, 0, 'sample_pdf.pdf', 'template-3.pdf', 1, width=100, height=20, font_size=16) ``` -------------------------------- ### Place Text in PDF Source: https://context7.com/t-houssian/fillpdf/llms.txt Places text at specified coordinates on a PDF page. Requires input and output PDF filenames, and the page number. ```python fillpdfs.place_text('Header Text', 250, 50, 'blank.pdf', 'with_header.pdf', 1) ``` -------------------------------- ### Write data to PDF and flatten Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Populate a PDF form with data and then flatten it, making all form fields uneditable. This is useful for creating a final, non-interactive version of the document. ```python write_fillable_pdf('ex2.pdf', 'new.pdf', data_dict, flatten=True) ``` -------------------------------- ### Available Fonts for Place Functions Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst A dictionary mapping internal font names to their corresponding PDF font names. Use these names when specifying the 'font_name' parameter in place functions. ```python { 'courier': 'Courier', 'courier-oblique': 'Courier-Oblique', 'courier-bold': 'Courier-Bold', 'courier-boldoblique': 'Courier-BoldOblique', 'helvetica': 'Helvetica', 'helvetica-oblique': 'Helvetica-Oblique', 'helvetica-bold': 'Helvetica-Bold', ``` -------------------------------- ### Place Image on PDF Source: https://context7.com/t-houssian/fillpdf/llms.txt Allows placing an image file at specific coordinates on a PDF page. Supports setting image dimensions and page number. ```APIDOC ## place_image Places an image file at specified coordinates on a PDF page. ### Method `fillpdfs.place_image(image_path, x_coord, y_coord, input_pdf, output_pdf, page_number=1, width=None, height=None)` ### Parameters - **image_path** (string) - Required - Path to the image file. - **x_coord** (integer) - Required - X coordinate from the left edge of the page. - **y_coord** (integer) - Required - Y coordinate from the top edge of the page. - **input_pdf** (string) - Required - Path to the input PDF file. - **output_pdf** (string) - Required - Path for the output PDF file. - **page_number** (integer) - Optional - The page number to place the image on (default is 1). - **width** (integer) - Optional - The desired width of the image in points. - **height** (integer) - Optional - The desired height of the image in points. ### Request Example ```python fillpdfs.place_image( 'company_logo.png', 50, 50, 'template.pdf', 'with_logo.pdf', page_number=1, width=200, height=100 ) ``` ``` -------------------------------- ### Insert Data into PDF Command Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst Use the insertfillpdf command to insert data into a PDF from a JSON file. Specify the input JSON path, output PDF path, and the input PDF path. If no output path is given, the output PDF will be named with '_out.pdf' appended to the input PDF name. ```bash insertfillpdf -j input_json_path -o output_pdf_path input_pdf_path ``` ```bash insertfillpdf -j blank.json -o new.pdf blank.pdf ``` -------------------------------- ### Write to Fillable PDF Source: https://github.com/t-houssian/fillpdf/blob/main/Test.ipynb Writes data to a fillable PDF form. Supports flattening the PDF after writing. Requires pdfrw. ```python def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict, flatten=False): """ ``` -------------------------------- ### Insert Data into PDF Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst Use the `insertfillpdf` command to populate a PDF form with data from a JSON file. You must specify the input JSON file and the output PDF file path. The original PDF file path is also required. ```APIDOC ## insertfillpdf ### Description Inserts data from a JSON file into a PDF form and saves the result as a new PDF file. ### Method Command Line Tool ### Endpoint N/A ### Parameters #### Positional Arguments - **input_pdf_path** (string) - Required - Path to the input PDF file to be filled. #### Optional Arguments - **-j input_json_path**, **--JSON input_json_path** (string) - Required - Path to the JSON file containing the data to insert. - **-o output_pdf_path**, **--output output_pdf_path** (string) - Required - Path to save the output PDF file. ### Request Example ```bash insertfillpdf -j blank.json -o new.pdf blank.pdf ``` ### Help Command ```bash insertfillpdf --help ``` ```bash insertfillpdf -h ``` #### Help Menu Example ``` positional arguments: test.pdf Input PDF file optional arguments: -h, --help show this help message and exit -j test.json, --JSON test.json Input JSON file, if none given, it will be the input file with the JSON extension -o test_out.pdf, --output test_out.pdf Output file to write result, if none given, it will be the input file with '_out.pdf' extension --version show program's version number and exit -v, --verbose set loglevel to INFO -vv, --very-verbose set loglevel to DEBUG ``` ``` -------------------------------- ### Extract PDF Fields Command Source: https://github.com/t-houssian/fillpdf/blob/main/index.rst Use the extractfillpdf command to extract fields from a PDF. Specify the input PDF path and optionally an output JSON path. If no output path is given, a JSON file with the same name as the input PDF will be created in the same directory. ```bash extractfillpdf input_pdf_path ``` ```bash extractfillpdf input_pdf_path -o output_json_path ``` ```bash extractfillpdf blank.pdf ``` ```bash extractfillpdf blank.pdf -o data.json ``` -------------------------------- ### Command Line Tool: extractfillpdf Source: https://github.com/t-houssian/fillpdf/blob/main/README.md Extracts form field data from a PDF and saves it as a JSON file. ```APIDOC ## extractfillpdf ### Description Extracts form field data from a PDF and saves it to a JSON file. If no output path is specified, the JSON file will have the same name as the input PDF but with a .json extension. ### Method Command Line Tool ### Endpoint extractfillpdf ### Parameters #### Positional Arguments - **input_pdf_path** (string) - Required - Path to the input PDF file. #### Optional Arguments - **-o, --output** (string) - Optional - Path to the output JSON file. If not provided, defaults to the input PDF name with a .json extension. - **-h, --help** - Optional - Displays the help message and exits. - **-v, --verbose** - Optional - Sets the log level to INFO. - **-vv, --very-verbose** - Optional - Sets the log level to DEBUG. - **--version** - Optional - Shows the program's version number and exits. ### Request Example ```bash extractfillpdf blank.pdf extractfillpdf blank.pdf -o data.json ``` ### Response Generates a JSON file containing the extracted form field data. ``` -------------------------------- ### Fill Single PDF Form Source: https://github.com/t-houssian/fillpdf/blob/main/WestHealth.ipynb Reads a PDF file, fills its form fields using the fill_form function, and writes the output to a new file. Ensure the input and output file paths are correct. ```python def single_form_fill(in_file, data, out_file): pdf = pdfrw.PdfReader(in_file) out_pdf = fill_form(pdf, data) pdfrw.PdfWriter().write(out_file, out_pdf) ```