html2pdf 是一個 JavaScript 包,允許開發人員將 html 轉換為 canvas、pdf、映像等。它將 html 作為參數並將其添加到 pdf 或所需文件中。此外,它還允許用戶在添加 html 內容後下載該文件。
在這裡,我們將存取表單並使用html2pdf npm套件將其新增至pdf。我們將看到不同的範例,以向pdf添加表單資料。
文法
使用者可以按照以下語法將 html 表單資料作為文字並將其發送到 html2pdf。
var element = document.getElementById('form'); html2pdf().from(element).save();
在上面的語法中,我們使用 id 存取表單並使用 html2pdf() 方法將其新增至 pdf 。
範例1(將整個表單加入pdf)
在下面的範例中,我們建立了表單並指定「from」作為 id。此外,我們還在表單中加入了一些輸入元素。在 JavaScript 中,我們使用表單的 id 存取表單並將其儲存在「element」變數中。
之後,我們使用html2pdf()方法將一個表單加入pdf。在輸出中,使用者可以觀察到當他們點擊提交按鈕時,它會下載帶有表單輸入標籤和值的pdf。
<html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"> </script> </head> <body> <h3 id="Using-the-i-html-pdf-i-to-create-a-pdf-using-the-content-of-the-form"> Using the <i> html2pdf </i> to create a pdf using the content of the form </h3> <!-- creating a sample form with 3 to 4 input fields with labels --> <form id = "form"> <label for = "name"> Name: </label> <input type = "text" id = "name" name = "name"> <br> <br> <label for = "email"> Email: </label> <input type = "email" id = "email" name = "email"> <br> <br> <input type = "button" value = "Submit" onclick = "generatePDF()"> </form> <script> // function to generate a pdf from the form using html2pdf function generatePDF() { // get the form element var element = document.getElementById('form'); // create a new pdf using the form element html2pdf().from(element).save(); } </script> </body> </html>
範例2(僅將表單內容新增至pdf)
在下面的範例中,我們將把表單的內容加到PDF中,而不是整個表單。我們已經建立了表單,並像在第一個範例中那樣添加了一些輸入欄位。
在 JavaScript 中,我們使用每個輸入的 id 來存取其值。之後,我們使用 JavaScript 建立了「div」元素,並使用「innerHTML」屬性將表單內容新增到 div 元素的 HTML 中。
接下來,我們使用div元素的內容來建立pdf。
<html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"> </script> </head> <body> <h3 id="Using-the-i-html-pdf-i-to-create-a-pdf-using-the-content-of-the-form"> Using the <i> html2pdf </i> to create a pdf using the content of the form </h3> <!-- creating a sample form with 3 to 4 input fields with labels --> <form id = "form"> <label for = "name"> Name: </label> <input type = "text" id = "name" name = "name"> <br> <br> <label for = "comment"> Comment: </label> <input type = "comment" id = "comment" name = "comment"> <br> <br> <input type = "button" value = "Submit" onclick = "getContentInPDF()"> </form> <script> function getContentInPDF() { // access form elements one by one var name = document.getElementById('name').value; var comment = document.getElementById('comment').value; // create a single html element by adding form data var element = document.createElement('div'); element.innerHTML = '<h1 id="Form-Data">Form Data</h1>' + '<p>Name: ' + name + '</p>' + '<p>Comment: ' + comment + '</p>'; // create a new pdf using the form element html2pdf().from(element).save(); } </script> </body> </html>
使用者學會了使用 html2pdf 將表單資料加入 pdf 。我們只需要呼叫 html2pdf() 方法,它將處理所有事情。在第一個範例中,我們在 pdf 中新增了帶有輸入值的整個表單,在第二個範例中,我們提取了表單資料並將其新增至 pdf 。
以上是如何將HTML表單資料作為文字並傳送到html2pdf?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

一致的HTML編碼風格很重要,因為它提高了代碼的可讀性、可維護性和效率。 1)使用小寫標籤和屬性,2)保持一致的縮進,3)選擇並堅持使用單引號或雙引號,4)避免在項目中混合使用不同風格,5)利用自動化工具如Prettier或ESLint來確保風格的一致性。

在Bootstrap4中實現多項目輪播的解決方案在Bootstrap4中實現多項目輪播並不是一件簡單的事情。雖然Bootstrap...

如何實現鼠標滾動事件穿透效果?在我們瀏覽網頁時,經常會遇到一些特別的交互設計。比如在deepseek官網上,�...

無法直接通過CSS修改HTML視頻的默認播放控件樣式。 1.使用JavaScript創建自定義控件。 2.通過CSS美化這些控件。 3.考慮兼容性、用戶體驗和性能,使用庫如Video.js或Plyr可簡化過程。

在手機上使用原生select的潛在問題在開發移動端應用時,我們常常會遇到選擇框的需求。通常情況下,開發者傾...

在手機上使用原生select的弊端是什麼?在移動設備上開發應用時,選擇合適的UI組件是非常重要的。許多開發者�...

使用Three.js和Octree優化房間內第三人稱漫遊的碰撞處理在Three.js中使用Octree實現房間內的第三人稱漫遊並添加碰�...

使用原生select在手機上的問題在移動設備上開發應用時,我們經常會遇到需要用戶進行選擇的場景。雖然原生sel...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6
視覺化網頁開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。