I have a canvas element on my page. I draw an image above it and some data entered by the user. By clicking on the button, I want to send the canvas to the printer to print it on paper. I tried using this plugin: jQuery.printElement , for example:
button code:
<a href="javascript:print_voucher()">PRINT</a>
'print_voucher ()' function:
function print_voucher() { $("#canvas_voucher").printElement(); }
canvas_voucher is the identifier of my canvas element. He printed the page, but did not print the canvas. I tried using it like this:
$("#canvas_voucher img").printElement();
But this did not even start the printer.
So how can I do this? How to print the contents of the canvas?
** EDIT ** Here is the code that creates my canvas and tries to create an image with it:
function create_voucher(visitor_name, visitor_identity_num, unique_number) { var canvas = $("#canvas_voucher")[0]; if (canvas.getContext) { var ctx = canvas.getContext('2d'); // Draw image var img = new Image(); img.src = 'https://someurl.com/image.jpg'; img.onload = function(){ ctx.drawImage(img,0,0); ctx.fillStyle="#CCC"; ctx.font="bold 20px Arial"; ctx.fillText(visitor_name, 750, 270); ctx.fillText(visitor_identity_num, 750, 295); ctx.font="bold 25px Arial"; ctx.fillText(unique_number, 620, 325); } var voucher = canvas.toDataURL("image/png"); $("#voucher_img").attr("src", voucher); } else { alert('You need Safari or Firefox 1.5+ to see this.'); }
}
source share