Formularios PDF
from ironpdf import *

# Step 1.  Creating a PDF with editable forms from HTML using form and input tags
# Radio Button and Checkbox can also be implemented with input type 'radio' and 'checkbox'
form_html = """
    <html>
        <body>
            <h2>Editable PDF  Form</h2>
            <form>
              First name: <br> <input type='text' name='firstname' value=''> <br>
              Last name: <br> <input type='text' name='lastname' value=''> <br>
              <br>
              <p>Please specify your gender:</p>
              <input type='radio' id='female' name='gender' value= 'Female'>
                <label for='female'>Female</label> <br>
                <br>
              <input type='radio' id='male' name='gender' value='Male'>
                <label for='male'>Male</label> <br>
                <br>
              <input type='radio' id='non-binary/other' name='gender' value='Non-Binary / Other'>
                <label for='non-binary/other'>Non-Binary / Other</label>
              <br>
 
              <p>Please select all medical conditions that apply:</p>
              <input type='checkbox' id='condition1' name='Hypertension' value='Hypertension'>
              <label for='condition1'> Hypertension</label><br>
              <input type='checkbox' id='condition2' name='Heart Disease' value='Heart Disease'>
              <label for='condition2'> Heart Disease</label><br>
              <input type='checkbox' id='condition3' name='Stoke' value='Stoke'>
              <label for='condition3'> Stoke</label><br>
              <input type='checkbox' id='condition4' name='Diabetes' value='Diabetes'>
              <label for='condition4'> Diabetes</label><br>
              <input type='checkbox' id='condition5' name='Kidney Disease' value='Kidney Disease'>
              <label for='condition5'> Kidney Disease</label><br>
            </form>
        </body>
    </html>"""

# Instantiate Renderer
renderer = ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True
renderer.RenderHtmlAsPdf(form_html).SaveAs("BasicForm.pdf")

# Step 2. Reading and Writing PDF form values.
form_document = PdfDocument.FromFile("BasicForm.pdf")

# Set and Read the value of the "firstname" field
first_name_field = form_document.Form.FindFormField("firstname")
first_name_field.Value = "Minnie"
print("FirstNameField value: {}".format(first_name_field.Value))

# Set and Read the value of the "lastname" field
last_name_field = form_document.Form.FindFormField("lastname")
last_name_field.Value = "Mouse"
print("LastNameField value: {}".format(last_name_field.Value))

form_document.SaveAs("FilledForm.pdf")
pip install ironpdf
Formularios PDF

Formularios PDF

IronPDF for Python puede tanto crear formularios PDF interactivos a partir de HTML como leer o escribir los valores de campos de formularios PDF existentes de manera programática.

Empezando

Habilita la creación de formularios configurando RenderingOptions.CreatePdfFormsFromHtml = True antes de renderizar HTML que contiene elementos <input>, <select> o <textarea>. Para leer o escribir valores de campo en un formulario existente, carga el PDF y usa Form.FindFormField.

Entender el código

Creación de Formularios

  • CreatePdfFormsFromHtml = True: Convierte elementos de formulario HTML (<input>, <radio>, <checkbox>, etc.) en campos de formulario PDF interactivos al renderizar.
  • RenderHtmlAsPdf(form_html): Renderiza el formulario HTML como un PDF con campos rellenables e interactivos. SaveAs("BasicForm.pdf") lo escribe en el disco.

Lectura y Escritura de Valores de Campos

  • PdfDocument.FromFile("BasicForm.pdf"): Carga el formulario PDF guardado para la manipulación de campos.
  • form_document.Form.FindFormField(name): Devuelve un objeto de campo de formulario para el campo con el valor del atributo name dado del HTML original.
  • field.Value = "text": Establece el valor del campo de formulario programáticamente.
  • field.Value: Lee el valor actual del campo de formulario.
  • SaveAs("FilledForm.pdf"): Guarda el PDF con todos los valores de los campos poblados.

Tipos de Campos de Formularios Soportados

IronPDF admite entradas de texto, botones de opción, casillas de verificación, menús desplegables (<select>) y áreas de texto. Todos se crean automáticamente a partir de los elementos HTML correspondientes cuando CreatePdfFormsFromHtml está habilitado.

¡Aprende a crear y completar formularios PDF con IronPDF for Python!

¿Listo para empezar?

Versión:2026.11recién publicada