Home > Blockchain >  How do I print the content of a DIV? This includes a picture
How do I print the content of a DIV? This includes a picture

Time:11-17

I have this code. I print the content of a DIV via its ID, but omit an image. I would like your support to know how to make that image be included. (the image is also inside the DIV)

impresion = The DIV to print

function printDiv(impresion) {
     var contenido= document.getElementById(impresion).innerHTML;
     var contenidoOriginal= document.body.innerHTML;


     document.body.innerHTML = contenido;

     window.print();

     document.body.innerHTML = contenidoOriginal;
}

I have tried many things and nothing works

CodePudding user response:

Here is a simple solution using just CSS:

@media print {
  body * {
    visibility: hidden;
  }
  #impresion, #impresion * {
    visibility: visible;
  }
  #impresion {
    position: absolute;
    left: 0;
    top: 0;
  }
}

CodePudding user response:

Try this, using a div with id="impresion":

function Print() {
  let data = document.getElementById("impresion").innerHTML;
  let mywindow = window.open('', 'new div', 'height=400,width=600');
  mywindow.document.write('<html><head><title></title></head><body >');
  mywindow.document.write(data);
  mywindow.document.write('</body></html>');
  setTimeout(function(){mywindow.print()},1000);
  return true;
}
  • Related