2016-01-12 41 views
-1

我正在使用JavaScript和HTML爲Android構建一個Apache Cordova應用程序。我希望用戶點擊/點擊一個按鈕,然後用剛剛輸入到文本字段中的字符串生成一個PDF,然後打開一封電子郵件發送PDF附件。附件不需要保存到設備,但可以在發送之前進行。這是可能的,如果是的話是否有任何文件或教程如何做到這一點?使用Javascript創建PDF和電子郵件Apache Apache Cordova

謝謝

回答

0

好,實現我們需要增加一些投入,並把它的價值觀,以及一個按鈕,將觸發JS。這很容易。

HTML:

<input type="text" id="name" placeHolder="Enter Your name" /> 
<input type="text" id="lname" placeHolder="Enter Your last name" /> 
<input type="text" id="hobby" placeHolder="Enter Your hobby" /> 
<input type="text" id="skills" placeHolder="Enter Your skills" /> 

<button id="btn"> 
    Get results 
</button> 
<!--values will be shown inside the div#content--> 
<div id="content"></div> 

下一步是在這裏您將與<p></p>標籤顯示所有div#content內部的輸入值中的JavaScript部分。

的JavaScript:

function onload() { 
    var input, btn, p, content, input_array; 
    //grab the button by ID 
    btn = document.getElementById("btn"); 
    //grab all inputs 
    input = document.getElementsByTagName("INPUT"); 
    //grab the div by ID 
    content = document.getElementById("content"); 
    // Add some 'Data' inside the variable so that you show 
    // the user what he answered 
    input_array = [ 
    "Name:", 
    "Last name:", 
    "Hobby:", 
    "Skills:" 
    ]; 
    //attach the click event listener to the button 
    btn.addEventListener("click", function() { 
     //Using the for loop, we basically create 4 <p> elements inside 
    //the div#content 
    for (var i = 0; i <= 3; i++) { 
     p = content; 
     // we display all of the input values within a <p> tag 
     // with a class of 'testing' so that you cn change it later 
     // on for styling it etc.. 
     // Then we add the array and input values inside the <p> tag 
     p.innerHTML += "<p class='testing'>" + input_array[i] + " " + input[i].value + "</p>"; 
    } 

    }, false); 
} 
//call the function 
onload(); 

現在你已經顯示的值,你可以做很多東西。您可以將用戶寫入數組的所有內容存儲起來,做一些很酷的動畫並淡入其中。

要將結果生成爲PDF格式,您需要使用我在GitHub上找到的插件,該插件據說可以保存生成的PDF。

(請注意,我個人沒有這個插件之前,這樣的工作我沒有給你擔保)

鏈接:cordova-plugin-html2pdf

按照文件存在,一切都應該工作好。

演示:Jsfiddle

相關問題