How to print a Blazor component in MAUI windows?

Mevolent 0 Reputation points
2024-08-07T21:55:40.53+00:00

Target framework: net7.0-windows10.0.19041.0

I need to print some components from my app. When I read about JS interop I thought that's going to be easy. However printing like that seems to strip some (not all) of the applications styles - currently colors and icons.

I'm using MudBlazor components, so I dug in how they are doing their CSS and I found that they have defined placeholders in their CSS that are then dynamically replaced using C# and I thought maybe that's why it ends up missing half the styles. So using several tools I transformed my Blazor component to an HTML string with in-line styles that produce the same result in web-based HTML evaluators. However the Print result is still lacking any colors/icons. Why could that be?

Are there other ways to print MAUI Blazor components?

Developer technologies | .NET | .NET MAUI
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,766 Reputation points Volunteer Moderator
    2024-08-09T15:55:39.6166667+00:00

    In the end, Blazor components just render html. The browser converts the html and style to a print image. You can better control with media css queries.

    https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Printing

    1 person found this answer helpful.

  2. Mevolent 0 Reputation points
    2024-08-18T16:03:43.4433333+00:00

    I've found the solution of my problem. Turns out Edge's engine has an option that disables styles by default:

    1. Open the print dialog
    2. Click on "More settings" in the sidebar
    3. Tick "Background graphics" in the bottom

    Now all styles are properly rendered.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.