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

462
Visualizações
How to perfectly add my image in to pdf file using jsPDF without the image getting distorted

I spend a day and half trying to solve this .Im trying to get a screenshot of my whole webpage and add it in to a pdf file using jspdf. How can i align it in a way that it looks in a similar manner like we get when use the default print option in the browser. Neat and clean. However in jspdf i dont understand how to extend my data to next page in pdf also . This is what i have tried:

function setScreenshot() {
    html2canvas(document.body).then(
        function (canvas) {
            var extra_canvas = document.createElement("canvas");
            let height = $('body').height();
            let width = $('body').width();
            extra_canvas.setAttribute('width', width);
            extra_canvas.setAttribute('height', height);
            var ctx = extra_canvas.getContext('2d');
            ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, width, height);
            var dataURL = extra_canvas.toDataURL();
            var img = $(document.createElement('img'));
           doc = new jsPDF({
                unit: 'px',
            });
            let docWidth = doc.internal.pageSize.width
            let docHeight = doc.internal.pageSize.height
            let widthRatio = docWidth / canvas.width
            let heightRatio = docHeight / canvas.height
            doc.addImage(dataURL, 'JPEG', 0, 0, width, height * heightRatio);
            doc.save('ExportFile.pdf');
            $('#screenShot').attr('download', 'Test file.png');
            $('#screenShot')[0].click();

        })
} 

I have also tried changing the canvas size to the that of an a4 sheet like following :

 ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, 793, 1122);

But again it doesnt work for smaller devices as the image gets stretched out.Any solution to this will be real helpful . Thank you guys .Also suggest if there is an alternate method or library using which i can achieve the same .

about 4 years ago · Juan Pablo Isaza
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