
html2pdf 是一個(gè) JavaScript 包,允許開發(fā)人員將 html 轉(zhuǎn)換為 canvas、pdf、圖像等。它將 html 作為參數(shù)并將其添加到 pdf 或所需文檔中。此外,它還允許用戶在添加 html 內(nèi)容后下載該文檔。
在這里,我們將訪問表單并使用html2pdf npm包將其添加到pdf中。我們將看到不同的示例,以向pdf添加表單數(shù)據(jù)。
語法
用戶可以按照以下語法將 html 表單數(shù)據(jù)作為文本并將其發(fā)送到 html2pdf。
var element = document.getElementById('form');
html2pdf().from(element).save();
在上面的語法中,我們使用 id 訪問表單并使用 html2pdf() 方法將其添加到 pdf 中。
示例1(將整個(gè)表單添加到pdf中)
在下面的示例中,我們創(chuàng)建了表單并指定“from”作為 id。此外,我們還在表單中添加了一些輸入元素。在 JavaScript 中,我們使用表單的 id 訪問表單并將其存儲在“element”變量中。
之后,我們使用html2pdf()方法將一個(gè)表單添加到pdf中。在輸出中,用戶可以觀察到當(dāng)他們點(diǎn)擊提交按鈕時(shí),它會下載帶有表單輸入標(biāo)簽和值的pdf。
<html>
<head>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"> </script>
</head>
<body>
<h3> 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(僅將表單內(nèi)容添加到pdf)
在下面的示例中,我們將把表單的內(nèi)容添加到PDF中,而不是整個(gè)表單。我們已經(jīng)創(chuàng)建了表單,并像在第一個(gè)示例中那樣添加了一些輸入字段。
在 JavaScript 中,我們使用每個(gè)輸入的 id 來訪問其值。之后,我們使用 JavaScript 創(chuàng)建了“div”元素,并使用“innerHTML”屬性將表單內(nèi)容添加到 div 元素的 HTML 中。
接下來,我們使用div元素的內(nèi)容來創(chuàng)建pdf。
<html>
<head>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"> </script>
</head>
<body>
<h3> 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>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>
用戶學(xué)會了使用 html2pdf 將表單數(shù)據(jù)添加到 pdf 中。我們只需要調(diào)用 html2pdf() 方法,它將處理所有事情。在第一個(gè)示例中,我們在 pdf 中添加了帶有輸入值的整個(gè)表單,在第二個(gè)示例中,我們提取了表單數(shù)據(jù)并將其添加到 pdf 中。
以上是如何將HTML表單數(shù)據(jù)作為文本并發(fā)送到html2pdf?的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!