Last active
May 20, 2022 13:59
-
-
Save bertoni/3a9c8b7853d62c2d76ed087397d38a8a to your computer and use it in GitHub Desktop.
html2canvas and jsPDF in action
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
<template> | |
<div id="app"> | |
<img src="./assets/logo.png"> | |
<router-view/> | |
<button @click="download">Download</button> | |
<div id="pdf"></div> | |
</div> | |
</template> | |
<script> | |
import html2canvas from 'html2canvas' | |
import * as JsPDF from 'jspdf' | |
export default { | |
name: 'App', | |
methods: { | |
download () { | |
html2canvas(document.querySelector('#app'), {imageTimeout: 5000, useCORS: true}).then(canvas => { | |
document.getElementById('pdf').appendChild(canvas) | |
let img = canvas.toDataURL('image/png') | |
let pdf = new JsPDF('portrait', 'mm', 'a4') | |
pdf.addImage(img, 'JPEG', 5, 5, 200, 287) | |
pdf.save('relatorio-remoto.pdf') | |
document.getElementById('pdf').innerHTML = '' | |
}) | |
} | |
} | |
} | |
</script> | |
<style> | |
#app { | |
font-family: 'Avenir', Helvetica, Arial, sans-serif; | |
-webkit-font-smoothing: antialiased; | |
-moz-osx-font-smoothing: grayscale; | |
text-align: center; | |
color: #2c3e50; | |
margin-top: 60px; | |
} | |
</style> |
Talvez tenha que iterar cada class encontrada, adicionar ao canvas e checar na lib de pdf se existe a possibilidade de criar marcações de páginas, ou se ele interpreta algum elemento HTML do canvas como "quebra" de página, acho que isso resolveria...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Desculpe, mas estou tentando fazer isso em uma página com vários DIV's com a Class: pdfpage
Quero que cada div.pdfpage seja uma página do PDF
Como posso fazer isso funcionar com o Wordpress, por exemplo?