Skip to content

Instantly share code, notes, and snippets.

@GavinRay97
Created January 27, 2020 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GavinRay97/78aecb4256631aa2d8a8048f2ef1eea9 to your computer and use it in GitHub Desktop.
Save GavinRay97/78aecb4256631aa2d8a8048f2ef1eea9 to your computer and use it in GitHub Desktop.
Print DOM element (avoids rendering issues)
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