Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

311
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda