Created
January 27, 2020 23:45
-
-
Save GavinRay97/78aecb4256631aa2d8a8048f2ef1eea9 to your computer and use it in GitHub Desktop.
Print DOM element (avoids rendering issues)
This file contains 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
printElement(selector) { | |
const elem = document.querySelector(selector) | |
var printWindow = window.open('','PrintWindow', 'width=400,height=200'); | |
html2canvas(elem).then(canvas => { | |
var doc = printWindow.document | |
var img = doc.createElement('img') | |
img.src = canvas.toDataURL('image/png') | |
doc.body.appendChild(img) | |
setTimeout(() => { | |
printWindow.print() | |
printWindow.close() | |
}, 0) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment