Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
crear un PDF desde un área de texto

Estoy tratando de exportar el valor de un área de texto

 let text = document.getElementById("textarea").value

como PDF. Estoy usando jsPDF. Mi primer intento fue:

 function createPDF(){ let text = document.getElementById("textarea").value; var doc = new jsPDF(); doc.text(20, 20, text); doc.save('document.pdf'); }

Sin embargo, aunque con este método se crea un pdf, el pdf no se crea con una "vista de barra de desplazamiento", básicamente solo imprime 1 línea.

Mi segundo intento fue:

 function createPdf() { let text = document.getElementById("textarea").value; // let titolo = document.getElementById("input-fileName").value; var doc = new jsPDF(); var splitText = doc.splitTextToSize(text, 250); var pageHight = doc.internal.pageSize.hight; doc.setFontSize(11); var y = 20; for(var i = 0; i < splitText.length; i++){ if (y > 275){ y = 20; doc.addPage(); } doc.text(20, y, splitText[i]); y = y + 5 } doc.save(title + '.pdf'); }

Sin embargo, con este método, ni siquiera se crea un pdf :(

Así que intenté usar html2pdf de la siguiente manera:

 function createPdf(){ var element = document.getElementById('textarea'); element.style.width = '800px'; element.style.height = '1000px'; html2PDF().from(element).toPdf().save('ilmiopdf.pdf'); }

Este método tampoco produce pdf. ¿Alguien tiene sugerencias?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Me las arreglé para encontrar una manera:

 //download pdf // function creaPdf() { // Long text var titolo = document.querySelector('#input-fileName').value; var text = document.querySelector('#textarea').value; var repeat = parseInt(document.querySelector('#repeat').value) || 0; for (var i=0; i<repeat; i++) text += text; // Create doc var ori = document.querySelector('#ori').checked ? "portrait":"landscape"; var doc = new jsPDF(ori, 'mm', 'a4'); doc.setFontSize(parseInt(document.querySelector('#fontsize').value) || 20) // Page size var pageSize = { h: doc.internal.pageSize.height, w: doc.internal.pageSize.width }; var margin = {top:20,left:15,bottom:15,right:15}; var lineHeight = doc.getTextDimensions("M").h / 2.54;// in to mm console.log("SIZE: ", pageSize, lineHeight); // Pages handler var linePos=0, nbPage=0; function addPage(doc) { if (nbPage) doc.addPage(); else nbPage = 0; nbPage++; linePos = margin.top; // Header // var title = document.querySelector('#input-fileName').value; // var twidth = doc.getTextDimensions(title).w / 2.54; // doc.text(pageSize.w/2 -twidth/2, margin.top, title); // doc.rect(margin.left, margin.top -lineHeight, // pageSize.w -margin.left - margin.right, // 1.5*lineHeight) // Footer var title = document.querySelector('#input-fileName').value; var numPage = 'page '+nbPage; var nwidth = doc.getTextDimensions(numPage).w / 2.54; doc.text(pageSize.w -margin.right -nwidth, pageSize.h -margin.bottom -lineHeight, numPage); doc.line(margin.left, pageSize.h -margin.bottom -2*lineHeight, pageSize.w - margin.right, pageSize.h -margin.bottom -2*lineHeight) // linePos = margin.top + 2*lineHeight; return nbPage; } function addLine(doc, text) { doc.text(margin.left, linePos, text); linePos += lineHeight; if (linePos > pageSize.h-2*margin.bottom) { addPage(doc); } } // Split text to page width text = doc.splitTextToSize(text,pageSize.w-margin.left-margin.right); addPage(doc); for (var i=0; i<text.length; i++){ addLine(doc, text[i]); } doc.save(titolo + '.pdf') }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!