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

177
Visualizações
Error in download a div as PDF using Javascript on button click

I am trying to download cv as pdf on button click in my cv builder using JS but unfortunately, the function instead of being called on button click is called as soon as the page loads. I tried some other ways including the arrow function and onclick attribute of the button although after that downloading starts on the button click but the pdf turns out to be empty.

Code: HTML:

<div class="container profile-box" id="f">
<!-- Code of whole cv template -->
</div>
 <div class="col-md-12 text-center">
 <button type="button" class="btn btn-primary" id="pdf" >Download Pdf</button>
 </div>

JS:

var btnpdf=document.getElementById("pdf");


async function generatepdf(){
   
    var downloading=document.getElementById("f");
    let width1 = downloading.offsetWidth;
    let height1 = downloading.offsetHeight;
    var doc=new jsPDF('p','pt');
    await html2canvas(downloading,{
        allowTaint:true,
        useCORS:true,
        width:width1,
        height:height1
        
    }).then((canvas)=>{
        //canvas convert to png
        doc.addImage(canvas.toDataURL("image/png"),'PNG',10,10);

    })
    doc.save("cv.pdf");
    
}
 btnpdf.addEventListener("click",generatepdf());
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It works for me.
What error are you getting?
Did you include the jspdf?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
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