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

137
Views
Angular v8 - Applying a calculated css style to print media

I have an angular component with html which includes the following for positioning labels correctly:

<div *ngFor="let label of labels"
class="text"
[style.left]="calcXPos(label.text.position.x) + 'px'"
[style.top]="calcYPos(label.text.position.y) + 'px'"
...>

This works correctly for setting the x and y positions on the screen.

In the parent component I have 2 buttons for printing:

<button id="printButton1"
    (click)="printChartWithLabels()">
    Print Label from Code
</button>
<button id="printButton2"
    onclick="window.print()">
    Print Chart direct from HTML
</button>

And in the javascript I have the following function

public printChartWithLabels(): void {
  window.print();
}

I have found that when I click printButton2 which has the window.print() command inserted directly in the html, then the [style.left] and [style.top] css values get calculated for the print and the label is in the correct position in the print preview. Debugging shows that calcXPos and calcYPos get called.

However when I use printButton1, then [style.left] and [style.top] css values do not get calculated because calcXPos and calcYPos never get called. This is despite the fact that window.print() is still being used (just being called from the javascript)

Does anyone know why the Angular style calculations only fire for print media if I call window.print() directly from the html?

What is the best way for resolving this to make sure calcXPos and calcYPos are fired even when calling window.print() from the javascript?

about 4 years ago · Juan Pablo Isaza
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!