1

I am using html2Canvas.js to convert HTML element to canvas and then converting canvas to image makes it browser comaptible for printing, is this the correct approach for printing HTML? I find image is the best to way to get similar print in any printer.

Deepak K
  • 309
  • 3
  • 11

2 Answers2

0

You can use the javascript function for print

<div onlick='window.print()'>Print</div>
Dineshkani
  • 2,715
  • 7
  • 29
  • 43
0

If you add this code to your css:

.toPrint {
  display:none;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
}

@media print {
   * {
    display: none !important;
  }

  .toPrint {
    display: block !important;
  }

}

And use this code when you want the print view updated:

/** code borrowed from @MicrosoftGoogle */
var canvas = document.getElementById("mycanvas");
var img    = canvas.toDataURL("image/png");

if (document.getElementsByClassName("toPrint")) {
  document.getElementsByClassName("toPrint")[0].src = img;
else
  document.write('<img class="toPrint" src="'+img+'"/>');
Brigand
  • 80,366
  • 19
  • 159
  • 169