在Blazor中打印一个
元素

huangapple go评论80阅读模式
英文:

Printing a div in blazor

问题

在我的Blazor应用程序中,单击按钮时,我希望用户能够打印特定的组件。请问如何实现这个功能?

我尝试过使用Blazorprint,但它会更改打印窗口中的一些样式。

此外,我还尝试了一些在线获取的解决方案,比如使用window.print,我在index.html中创建了JS函数,并通过JSRuntime调用它,但仍然显示找不到函数名称的错误。

英文:

In my blazor app, onclick on a button, I want the user to be able to print a particular component. Pls how can this be done?

I've tried using Blazorprint but it's change some of the styles n the print window.

Also, I tried some solutions gotten online, the one of window.print, I created the js function inside my index.html and make a call to it using the JSRuntime and this also keeps saying the function name not found

答案1

得分: 1

为了在你的 Blazor 应用程序中启用特定组件的打印功能,你可以使用 Blazor 提供的 JavaScript 互操作功能。以下是如何实现的逐步指南:

步骤 1:创建 JavaScript 函数
在你的 Blazor 应用程序的 wwwroot 文件夹中创建一个新的 JavaScript 文件,例如 print.js。在这个文件中,定义一个触发打印功能的函数:

// print.js
function printComponent(componentSelector) {
  var elementToPrint = document.querySelector(componentSelector);
  if (elementToPrint) {
    var printWindow = window.open('', '_blank');
    printWindow.document.open();
    printWindow.document.write('<html><head><title>Print</title></head><body>');
    printWindow.document.write(elementToPrint.innerHTML);
    printWindow.document.write('</body></html>');
    printWindow.document.close();
    printWindow.print();
    printWindow.close();
  }
}

步骤 2:包含 JavaScript 文件
在你的 index.html 文件中,在 &lt;head&gt; 部分内添加以下行,以包含你在前一步中创建的 JavaScript 文件:

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

步骤 3:从你的 Blazor 组件调用 JavaScript 函数
在你的 Blazor 组件内,你可以使用 IJSRuntime 服务来调用 JavaScript 函数。以下是如何做的示例:

@inject IJSRuntime JSRuntime

<button @onclick="PrintComponent">Print</button>

@code {
    private async Task PrintComponent()
    {
        await JSRuntime.InvokeVoidAsync("printComponent", "#componentId");
    }
}

在上面的代码中,#componentId 是你要打印的组件的 CSS 选择器。将其替换为你的组件的适当选择器。

注意:确保你已经在组件文件中导入了 Microsoft.JSInterop 命名空间。

这就是全部!当用户点击“Print”按钮时,它将触发 JavaScript 函数,打开一个新窗口,其中包含所选组件的内容,并启动该窗口的打印对话框。

英文:

To enable printing of a particular component in your Blazor app, you can use the JavaScript interop feature provided by Blazor. Here's a step-by-step guide on how you can achieve this:

Step 1: Create a JavaScript function
In the wwwroot folder of your Blazor app, create a new JavaScript file, such as print.js. Inside this file, define a function that triggers the print functionality:

// print.js
function printComponent(componentSelector) {
  var elementToPrint = document.querySelector(componentSelector);
  if (elementToPrint) {
    var printWindow = window.open(&#39;&#39;, &#39;_blank&#39;);
    printWindow.document.open();
    printWindow.document.write(&#39;&lt;html&gt;&lt;head&gt;&lt;title&gt;Print&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&#39;);
    printWindow.document.write(elementToPrint.innerHTML);
    printWindow.document.write(&#39;&lt;/body&gt;&lt;/html&gt;&#39;);
    printWindow.document.close();
    printWindow.print();
    printWindow.close();
  }
}

Step 2: Include the JavaScript file
In your index.html file, include the JavaScript file you created in the previous step by adding the following line inside the &lt;head&gt; section:

&lt;script src=&quot;print.js&quot;&gt;&lt;/script&gt;

Step 3: Call the JavaScript function from your Blazor component
Inside your Blazor component, you can use the IJSRuntime service to call the JavaScript function. Here's an example of how you can do it:

@inject IJSRuntime JSRuntime

&lt;button @onclick=&quot;PrintComponent&quot;&gt;Print&lt;/button&gt;

@code {
    private async Task PrintComponent()
    {
        await JSRuntime.InvokeVoidAsync(&quot;printComponent&quot;, &quot;#componentId&quot;);
    }
}

In the above code, #componentId is the CSS selector for the component you want to print. Replace it with the appropriate selector for your component.

Note: Make sure you have imported the Microsoft.JSInterop namespace in your component file.

That's it! When the user clicks the "Print" button, it will trigger the JavaScript function, which opens a new window with the content of the selected component and initiates the print dialog for that window.

huangapple
  • 本文由 发表于 2023年6月9日 08:14:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定