Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick Print HTML5 Canvas

I want to send/print the canvas image directly to the default printer. That means a quick printing.

Anyone can give a hint.

Javascript or jQuery.

like image 257
Derin Avatar asked Oct 09 '12 23:10

Derin


2 Answers

I have searched alot and found a solution which works perfectly :) Used onclick event

function printCanvas()  
{  
    var dataUrl = document.getElementById('anycanvas').toDataURL(); //attempt to save base64 string to server using this var  
    var windowContent = '<!DOCTYPE html>';
    windowContent += '<html>'
    windowContent += '<head><title>Print canvas</title></head>';
    windowContent += '<body>'
    windowContent += '<img src="' + dataUrl + '">';
    windowContent += '</body>';
    windowContent += '</html>';
    var printWin = window.open('','','width=340,height=260');
    printWin.document.open();
    printWin.document.write(windowContent);
    printWin.document.close();
    printWin.focus();
    printWin.print();
    printWin.close();
}
like image 125
Raza Avatar answered Sep 21 '22 19:09

Raza


Use printJS and this one liner:

printJS({printable: document.querySelector("#your-canvas").toDataURL(), type: 'image', imageStyle: 'width:100%'});
like image 32
Paweł Moskal Avatar answered Sep 19 '22 19:09

Paweł Moskal