0

Here is my output image

Here is my code


let input = document.querySelector('input'); var textarea = document.getElementsByClassName('area')[0]; var txtreplace = document.getElementsByClassName('demo')[0]; window.addEventListener('load', function () { input.addEventListener('change', () => { let files = input.files; if (files.length == 0) return; const file = files[0]; let reader = new FileReader(); const fruits = []; reader.onload = (e) => { const file = e.target.result; const lines = file.split(/\r\n|\n/); //console.log(lines.length); textarea.value = lines.join('\n'); lines.forEach(function (arrayItem) { //console.log(arrayItem.replace("\u001bE", "")); var arr1 = arrayItem.replace("\u001bE", ""); var arr2 = arr1.replace("\u001bF", ""); fruits.push(arr2); }); console.log(fruits.length); if (parseInt(fruits.length) > 0) { console.log(fruits); txtreplace.innerHTML = fruits; } };
                reader.onerror = (e) => alert(e.target.error.name);

                reader.readAsText(file);

            });
        }, false);
      
    function printDiv() {
        var divContents = document.getElementsByClassName("demo").innerHTML;
        var a = window.open('', '', 'height=500, width=500');
        a.document.write('<html>');
        a.document.write('<body > <h1>Print PDF <br>');
        a.document.write(divContents);
        a.document.write('</body></html>');
        a.document.close();
        a.print();
    }

</script> </body>

1 Answer 1

0

You can take a look at https://github.com/parallax/jsPDF.
It's js library allow you to build pdf using Javascript.

Otherwise have some solutions to convert html page to pdf ( https://www.html2pdf.co.uk/ ), but I don't think it's what you are looking for.

Exemple of code using JSPDF :

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>


var doc = new jsPDF();
doc.text(20, 20, 'Hello world!');
doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');

doc.save('Test.pdf');

Hope it will helps you ;)

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for you response but here i want to print my text content into pdf file
I think jspdf can helps you, I shared an exemple to write text into a pdf and then export it
here my problem is every time i have to upload text file in that text file some string replacing happen automatically in code then that that text file after changes it will download by clicking Download button
Okay, so I really think jspdf can do it. You read the text file => you do the remplacement => you get a text you want to export as pdf =>you click the button => it generate your pdf filled with the your text` then it export the pdf ` using jspdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.