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")
PDF 양식
IronPDF for Python은 HTML에서 대화형 PDF 양식을 생성하거나 기존 PDF 양식의 필드 값을 프로그래밍 방식으로 읽고 쓸 수 있습니다.
시작하기
RenderingOptions.CreatePdfFormsFromHtml = True을 설정하여 폼 생성을 활성화하고 <input>, <select>, 또는 <textarea> 요소를 포함하는 HTML을 렌더링하기 전에 실행하십시오. 기존 양식의 필드 값을 읽거나 쓰기 위해, PDF를 로드하고 Form.FindFormField을 사용하세요.
코드 이해하기
양식 생성
CreatePdfFormsFromHtml = True: HTML 폼 요소(<input>, <radio>, <checkbox> 등)를 PDF 렌더링 시 상호작용 가능한 PDF 폼 필드로 변환합니다.
RenderHtmlAsPdf(form_html): HTML 폼을 PDF로 렌더링하여 대화형으로 작성 가능한 필드를 제공합니다. SaveAs("BasicForm.pdf") 를 디스크에 씁니다.
필드 값 읽기 및 쓰기
PdfDocument.FromFile("BasicForm.pdf"): 필드 조작을 위해 저장된 양식 PDF를 로드합니다.
form_document.Form.FindFormField(name): 원래 HTML에서 주어진 name 속성 값 필드의 폼 필드 개체를 반환합니다.
field.Value = "text": 폼 필드의 값을 프로그래밍 방식으로 설정합니다. field.Value: 양식 필드의 현재 값을 읽습니다. SaveAs("FilledForm.pdf"): 모든 필드 값이 채워진 상태로 PDF를 저장합니다.
지원되는 양식 필드 유형
IronPDF는 텍스트 입력, 라디오 버튼, 체크박스, 드롭다운(<select>), 및 텍스트 영역을 지원합니다. CreatePdfFormsFromHtml이 활성화되면 모든 것이 해당 HTML 요소에서 자동으로 생성됩니다.