Search code examples
c#blazor.net-5blazor-webassembly

window.print in Blazor WASM


I'm new to Blazor and I've been trying to find if there's any window.print() equivalent in Blazor. I wonder if there is any pre-made component/helper for this or do I need to create one from scratch? I've already searched enough and can't find any.


Solution

  • There is no equivalent in blazor but you should consider adding a javascript file with a method with window.print(), reference it in index.html and call it with injected IJSRuntime.

    Reference in index.html

    <script src="js/script.js"></script>
    

    Javascript file

    function printInvoke() {
        window.print();
    }
    

    Example in component

    
    @page "/print"
    @inject IJSRuntime Js
    
    <h3>PrintTest</h3>
    
    <button @onclick="Print"></button>
    
    @code {
        private async Task Print()
        {
            await Js.InvokeVoidAsync("printInvoke");
        }
    }