Skip to content

Instantly share code, notes, and snippets.

@doytsujin
Forked from bertoni/html2canvasAndjsPDF.vue
Created May 20, 2022 13:59
Show Gist options
  • Save doytsujin/278da79d205b9222d24bad1157f18894 to your computer and use it in GitHub Desktop.
Save doytsujin/278da79d205b9222d24bad1157f18894 to your computer and use it in GitHub Desktop.
html2canvas and jsPDF in action
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment