I'm trying to clone a page I need for printing. The print dialog should ONLY print out the selected element and its elements.
https://jsfiddle.net/ctqdhta7/
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);
setTimeout(function() {
var restorepage = $('body').html();
var printcontent = $("#hello").clone();
$('body').empty().html(printcontent);
window.print();
$('body').html(restorepage);
}, 1000);
This copies the entire body page, clones the content of the print I should print out, then it empties the body and replaces it with the content it should print. After that, it opens the print dialog with ONLY the printable elements. A workaround could be: Create the whole canvas after .html(printcontent)
once again. Since clone()
only clones the HTML, the canvas has not been initialized and has no content.
Since I am using an Angular directive, I am unable to simply do the whole "redraw the canvas". Is there an Angular way that makes it possible to "re-render the DOM again" or something like that?