Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
How to hide background contents of window.open while printing in Angular

I have running Angular 12 application and I am integrating the behavior to print report. I am able to achieve the print on button click which opens a new window, but I am not able to figure out how to achieve below things:

  1. Remove background contents on window.open so that the print window looks like a normal print window which we see by pressing Win+P in our browser. enter image description here
  2. Center the print window popup in the screen.
  3. Not able to display images in print window. If I add any img tag in html like: <img src="assets/images/galaxy.png" alt="">, the image is not getting displayed in the print window. If I comment window.print() and just use window.open(), image is getting displayed

print.service.ts

  showDialog() {
    const factory =
      this.componentFactoryResolver.resolveComponentFactory(PrintReportComponent);

    const dialogComponentRef = factory.create(this.injector);
    dialogComponentRef.instance.title = 'Report';

    dialogComponentRef.changeDetectorRef.detectChanges();

    //fetch the root DOM element of ModalComponent
    const domElement = (dialogComponentRef.hostView as EmbeddedViewRef<any>)
      .rootNodes[0] as HTMLElement;

    const WindowPrt = window.open(
      '',
      '',
      'top=0,left=0,height=100%,width=auto'
    );
    WindowPrt.document.head.innerHTML = document.head.innerHTML;
    WindowPrt.document.body.innerHTML = domElement.outerHTML;
    WindowPrt.document.close();
    WindowPrt.focus();
    WindowPrt.print();
    WindowPrt.close();
  }

stackblitz link: https://stackblitz.com/edit/angular-ivy-c1ewya?file=src%2Fapp%2Fprint.service.ts

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In this part :

const WindowPrt = window.open(
      '',
      '',
      'top=0,left=0,height=100%,width=auto'
    );

You have to remove '', ''. change to :

const WindowPrt = window.open(
          'top=0,left=0,height=100%,width=auto'
        );
about 4 years ago · Juan Pablo Isaza Report

0

You should change your strategy at all. If you will do this, it will work. Please try this example one:

link-stackblitz

I added one dummy image in it and it is displayed.

Good luck ;)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!