0

I currently have a html textarea that saves to a textfile on a button press (using javascript)

This works fine but i have been told that they now need page numbers and page totals and if possible a header on each page. If i can work out the total line numbers for a printed A4 page then i can insert a header for every nth line.

This is the javascript code im currently using.

 function saveTextAsFile()
{
    var textToWrite = "School Centre Number: " + document.getElementById("centernumber").value + "\nStudent Name:" + document.getElementById("studentname").value + "\r\n" + "Date and Time: " + document.getElementById("date").value+" " + document.getElementById("time").value+ "\r\n"+ "Exam Title: "+ document.getElementById("examtitle").value+ "\r\n\r\n"+document.getElementById("inputText").value;
    textToWrite = textToWrite.replace(/\n/g, "\r\n"); 

var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});

    var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;

    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    if (window.webkitURL != null)
    {
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    }
    else
    {
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
    }
    downloadLink.click();
}

Thankyou for any feedback

ps preferably with no external libraries using html and javascript

1 Answers1

0

Used this method

https://stackoverflow.com/a/45252132/7805456

Works perfectly and accurately with new lines, returns and wrapped text.