简体   繁体   中英

How to Download Input Text from user as PDF using HTML/Javascript

I want to take input from the user using the < input > tag and when the user clicks on download button, then then the input gets downloaded as pdf. How Can I implement it using HTML, CSS and Javascript on the client side.

You can use the jsPDF library to generate PDFs.

You can add text via the text() method and save via the save() method.

To ensure the text wraps, we can use splitTextToSize to convert it to an array and pass it as a parameter to the text() method:

download.addEventListener('click', function() {
  var doc = new jsPDF()
  doc.text(10, 10, doc.splitTextToSize(text.value, 180));
  doc.save('doc.pdf');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>

<input id="text"><button id="download">Download as PDF</button>

Try it on JsFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM