无法从 angular 2 更改 chrome 浏览器中的打印边距和布局

Unable to change the margin & layout for printing in chrome browser from angular 2

我正在尝试打印 HTML 但我无法更改 layoutmargin chrome 浏览器(参考附图)。

PRINT() {
  window.print();
}
@media print {
  .doNotPrint {
    display: none;
  }
  * {
    -webkit-print-color-adjust: exact;
  }
  @page {
    margin: 0 !important;
    size: A4 Landscape !important;
    -webkit-print-color-adjust: exact;
  }
  html,
  body {
    margin: 0 !important;
    size: A4 Landscape !important;
    -webkit-print-color-adjust: exact;
  }
}
<form [ngClass]="themeSRVC.currentThemeNAME" fxLayout="column" fxFlex>
  <!-- navBAR -->
  <mat-toolbar id="idPrimaryTOOLBAR" color="primary" class="doNotPrint">
    <mat-toolbar-row>
      <button mat-icon-button type="button" (click)="routeSRVC.goBACK()">
        <mat-icon matTooltip="Go Back">arrow_back</mat-icon>
      </button>
      <span class="fillSPACE"></span>
      <button mat-icon-button (click)="PRINT()">
        <mat-icon matTooltip="print">print</mat-icon>
      </button>
    </mat-toolbar-row>
  </mat-toolbar>

  <!-- *****printableCONTENT***** -->
  <div id="idToPRINT" fxFlex>

    <div fxLayout="row">
      <div fxFlex="33" style="height:200px;background-color:red">Left</div>
      <div fxFlex style="height:200px;background-color:yellow">Center</div>
      <div fxFlex="33" style="height:200px;background-color:green"> Right</div>
    </div>


  </div>

</form>

我是不是漏掉了什么……?请帮我删除边距并更改布局。

经过数小时的研究...我终于弄明白了!

set @media styles in the global styles.scss of your app.

NOT in the component.scss

如果您想将布局动态更改为portrait/landscape,您可以像下面那样做

1) Remove the @media print{...} from the global scss

2) and in your component.ts call the print function by passing true/false

PRINT(landscape: boolean) {
  var head = document.head || document.getElementsByTagName('head')[0];
  var style = document.createElement('style');
  style.type = 'text/css';
  style.media = 'print';

  style.appendChild(document.createTextNode(landscape ?
    '@page { size: A4 landscape; margin: 0in;}' :
    '@page { size: A4;  margin: 0in; }'));

  head.appendChild(style);
  window.print();
}

我知道这个帖子可能很旧。但是,如果您的程序员使用 CSS 并且您想要打印但布局选项未显示,请添加此 CSS 行

"@media print { @page {size: auto !important} }" 适合您的风格 sheet

我遇到了类似的问题,但能够使用上面的代码解决。代码来源来自@yeshwanthkv (@https://whosebug.com/users/3902260/yeshwanthkv)

的线程 Bootstrap 4 CSS causing Chrome's print "Layout" to disappear