Formularze 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
Formularze PDF

Formularze PDF

IronPDF for Python może zarówno tworzyć interaktywne formularze PDF z HTML, jak i odczytywać lub zapisywać wartości pól istniejących formularzy PDF programowo.

Pierwsze kroki

Aby wlaczyc tworzenie formularzy, nalezy ustawic RenderingOptions.CreatePdfFormsFromHtml = True przed renderowaniem HTML, ktory zawiera elementy <input>, <select> lub <textarea>. Aby odczytac lub zapisac wartosci pol w istniejacym formularzu, zaladuj plik PDF i uzyj Form.FindFormField.

Zrozumienie kodu

Tworzenie formularzy

  • CreatePdfFormsFromHtml = True: Konwertuje elementy formularza HTML (<input>, <radio>, <checkbox>, itp.) na interaktywne pola formularza PDF podczas renderowania.
  • RenderHtmlAsPdf(form_html): Renderuje formularz HTML jako PDF z interaktywnymi, wypelnialnymi polami. SaveAs("BasicForm.pdf") zapisuje go na dysku.

Odczytywanie i zapisywanie wartości pól

  • PdfDocument.FromFile("BasicForm.pdf"): Laduje zapisany formularz PDF do manipulacji polami.
  • form_document.Form.FindFormField(name): Zwraca obiekt pola formularza dla pola z wartoscia atrybutu name z oryginalnego HTML.
  • field.Value = "text": Ustawia wartosc pola formularza programowo.
  • field.Value: Odczytuje aktualna wartosc pola formularza.
  • SaveAs("FilledForm.pdf"): Zapisuje PDF ze wszystkimi wypelnionymi wartosciami pol.

Obsługiwane typy pól formularzy

IronPDF obsluguje tekstowe pola wejsciowe, przyciski radiowe, pola wyboru, listy rozwijane (<select>) i obszary tekstowe. Wszystkie sa tworzone automatycznie z odpowiadajacych im elementow HTML, gdy CreatePdfFormsFromHtml jest wlaczone.

Dowiedz się, jak tworzyć i wypełniać formularze PDF z IronPDF for Python!

Gotowy, aby rozpocząć?

Wersja:2026.11właśnie wydany