import {PdfDocument} from "@ironsoftware/ironpdf";
(async () => {
// Define the HTML content with editable form fields
const formHtml = `
<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>
`;
// Configure render options
const options = {
createPdfFormsFromHtml: true,
};
// Render HTML content to a PDF with editable forms
const pdf = await PdfDocument.fromHtml(formHtml, { renderOptions: options });
// Save the new PDF
await pdf.saveAs("formField.pdf");
})();
PDF 表單
包含表單欄位的 HTML 亦可渲染為 PDF,同時完整保留表單欄位資訊與功能。 要從 HTML 內容建立可編輯的 PDF 表單,需先定義一個包含各種輸入欄位的 HTML 表單,例如文字欄位、單選按鈕和核取方塊。 這些表單元件可讓使用者輸入資料並進行選取。
為達成此目標,請將 createPdfFormsFromHtml 選項設定為 true。 此選項指定應將 HTML 表單轉換為可編輯的 PDF 表單。
接著,請使用 PdfDocument.fromHtml 方法將 HTML 內容渲染為 PDF 文件。 將指定的渲染選項作為物件參數傳入。
最後,請使用 saveAs 方法,將可編輯的表單欄位 PDF 匯出為名為"formField.pdf"的新 PDF 檔案。
若要進一步了解如何從 HTML 內容產生 PDF 表單,請造訪 IronPDF 產品頁面以獲取詳細資訊與範例。
<a href="https://github.com/iron-software/IronPdfNode.Examples/tree/main/examples/form-data" class="code_content__related-link__doc-cta-link">Explore Code Examples for Creating PDF Forms from HTML</a>
<a href="https://github.com/iron-software/IronPdfNode.Examples/tree/main/examples/form-data" class="code_content__related-link__doc-cta-link">Explore Code Examples for Creating PDF Forms from HTML</a>