Last active
November 18, 2021 16:10
-
-
Save camilamoreiradev/745d878cb18234502d946933199f14f8 to your computer and use it in GitHub Desktop.
Criar um botão js e nele colocar o código passando o id da div que quer imprimir no pdf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Links para adicionar | |
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script> | |
//container-print é o id da div que quer imprimir | |
Swal.fire({ | |
title: 'Aguarde...', | |
html: 'Estamos gerando o PDF com as informações da tela.', | |
timerProgressBar: true, | |
didOpen: () => { | |
Swal.showLoading() | |
} | |
}); | |
setTimeout(function(){ | |
html2canvas(document.getElementById("container-print"), { | |
onrendered: function (canvas) { | |
var imgData = canvas.toDataURL('image/png'); | |
var imgWidth = 210; | |
var pageHeight = 295; | |
var imgHeight = canvas.height * imgWidth / canvas.width; | |
var heightLeft = imgHeight; | |
var doc = new jsPDF('p', 'mm'); | |
var position = 0; | |
doc.addImage(imgData, 'jpeg', 0, position, imgWidth, imgHeight); | |
heightLeft -= pageHeight; | |
while (heightLeft >= 0) { | |
position = heightLeft - imgHeight; | |
doc.addPage(); | |
doc.addImage(imgData, 'jpeg', 0, position, imgWidth, imgHeight); | |
heightLeft -= pageHeight; | |
} | |
doc.save( 'file.pdf'); | |
Swal.close(); | |
} | |
}); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment