Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
creating a PDF from a textarea

I am trying to export the value of a textarea

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

as a PDF. I am using jsPDF. My first try was:

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

However, even though with this method a pdf is created, the pdf is not created with a "scroll bar view", it only prints 1 line basically.

My second try was:

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');
}

However, with this method, a pdf is not even created :(

So I tried using html2pdf as following:

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

This method also produces no pdf. Does anyone have suggestions?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I managed to find a way:

    //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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda