Vugar Abdullayev
Vugar Abdullayev

Reputation: 2105

@Media print styles are not applied

Stackblitz: https://stackblitz.com/edit/angular-xvdy1q
I want to print specific div.

<div id="forPrint" (click)="onPrint('23')">
  <div id="printable">
    <div class="box">

    </div>
  </div>
</div>

Javascript Code:

onPrint(url) {
  this.mode = 'print';
  const mywindow = window.open('', 'PRINT', 'height=400,width=600');

  mywindow.document.write(`<html><head><title>${document.title}</title>`);
  mywindow.document.write('</head><body >');
  mywindow.document.write(`<h1> www.${url}</h1>`);
  mywindow.document.write(document.getElementById('forPrint').innerHTML);
  mywindow.document.write('</body></html>');
  mywindow.document.close(); // necessary for IE >= 10
  mywindow.focus(); // necessary for IE >= 10*/

  mywindow.print();
  mywindow.close();

  return true;
}

Stlyles:

@media print {
  html,
  body {
    height: 100% !important;
    width: 100%!important;
    h1 {
      color: #1c7430;
    }
  }
  #printable {
    display: flex!important;
    justify-content: center!important;
    align-items: center!important;
    height: 100% !important;
    width: 700px!important;
  }
  .box {
    width: 100px!important;
    height: 100px!important;
    background: green!important;
  }
}

But after print dialog is opened, the box is not shown. So no styles are applied such as h1, .box. What is the issue? Thanks in advance.

Upvotes: 1

Views: 1753

Answers (2)

Turnip
Turnip

Reputation: 36742

You need to somehow inject your CSS into the new page.

Here I create a style tag and add the CSS directly. You could alternatively link to a stylesheet:

function onPrint(url) {
  this.mode = 'print';
  const mywindow = window.open('', 'PRINT', 'height=400,width=600');

  const styles = `html,
      body {
        height: 100% !important;
        width: 100%!important;
      }
      h1 {
          color: #1c7430;
      }
      #printable {
        display: flex!important;
        justify-content: center!important;
        align-items: center!important;
        height: 100% !important;
        width: 700px!important;
      }
      .box {
        width: 100px!important;
        height: 100px!important;
        background: green!important;
      }`

  mywindow.document.write(`<html><head><title>${document.title}</title>`);
  mywindow.document.write(`<style>${styles}</style>`)
  mywindow.document.write('</head><body >');
  mywindow.document.write(`<h1> www.${url}</h1>`);
  mywindow.document.write(document.getElementById('forPrint').innerHTML);
  mywindow.document.write('</body></html>');
  mywindow.document.close(); // necessary for IE >= 10
  mywindow.focus(); // necessary for IE >= 10*/

  mywindow.print();
  mywindow.close();

  return true;
}

JSFiddle Demo

Upvotes: 5

Furkan Poyraz
Furkan Poyraz

Reputation: 692

You need to enable background colors and images when printing. You can do this in Chrome in the print dialog under andvanced settings.

You can also apply the -webkit-print-color-adjust: exact; property to change the background print behavior.

Upvotes: 3

Related Questions