Blazor Server vs. WebAssembly: Comparison

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 是否支持 Blazor 服务器和 Blazor WebAssembly (WASM)?

IronPDF 支持 Blazor 服务器,但不支持 Blazor WebAssembly (WASM)。

要在 Blazor 服务器中保存 PDF,您需要将 PDF 文档流转换为字节数组,然后将其传递给 JavaScript 函数以便于下载。

以下是如何在 Blazor 服务器应用程序中将 PDF 文档转换为字节数组,然后使用 JavaScript 触发下载的示例:

@page "/pdfdownload"
@inject IJSRuntime JSRuntime

@* A button to download the PDF *@
<button @onclick="DownloadPDF">Download PDF</button>

@code {
    private async Task DownloadPDF()
    {
        // Create a PDF document using IronPDF (this is hypothetical code)
        var renderer = new IronPdf.HtmlToPdf();
        var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>");

        // Convert PDF document to a byte array
        byte[] pdfBytes = pdfDocument.BinaryData;

        // Call the JavaScript function to download the PDF
        await JSRuntime.InvokeVoidAsync("downloadFile", pdfBytes, "example.pdf");
    }
}
@page "/pdfdownload"
@inject IJSRuntime JSRuntime

@* A button to download the PDF *@
<button @onclick="DownloadPDF">Download PDF</button>

@code {
    private async Task DownloadPDF()
    {
        // Create a PDF document using IronPDF (this is hypothetical code)
        var renderer = new IronPdf.HtmlToPdf();
        var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>");

        // Convert PDF document to a byte array
        byte[] pdfBytes = pdfDocument.BinaryData;

        // Call the JavaScript function to download the PDF
        await JSRuntime.InvokeVoidAsync("downloadFile", pdfBytes, "example.pdf");
    }
}
'INSTANT VB TODO TASK: The following line could not be converted:
page "/pdfdownload" inject IJSRuntime JSRuntime * A button [to] download the PDF * <button onclick="DownloadPDF"> Download PDF</button> code
If True Then
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'	private async Task DownloadPDF()
'	{
'		' Create a PDF document using IronPDF (this is hypothetical code)
'		var renderer = New IronPdf.HtmlToPdf();
'		var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>");
'
'		' Convert PDF document to a byte array
'		byte[] pdfBytes = pdfDocument.BinaryData;
'
'		' Call the JavaScript function to download the PDF
'		await JSRuntime.InvokeVoidAsync("downloadFile", pdfBytes, "example.pdf");
'	}
End If
$vbLabelText   $csharpLabel

下面是处理 PDF 下载的 JavaScript 函数。 确保将此内容包含在您的 _Host.cshtmlindex.html 文件中:

<script>
    // Function to download a file from a byte array in JavaScript
    function downloadFile(fileBytes, fileName) {
        // Convert byte array to a Blob
        const blob = new Blob([fileBytes], { type: 'application/pdf' });

        // Create a link element for downloading the file
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = fileName;

        // Append the link to the body, trigger it, and remove it afterwards
        document.body.appendChild(link); // Required for Firefox
        link.click();
        document.body.removeChild(link);
    }
</script>
<script>
    // Function to download a file from a byte array in JavaScript
    function downloadFile(fileBytes, fileName) {
        // Convert byte array to a Blob
        const blob = new Blob([fileBytes], { type: 'application/pdf' });

        // Create a link element for downloading the file
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = fileName;

        // Append the link to the body, trigger it, and remove it afterwards
        document.body.appendChild(link); // Required for Firefox
        link.click();
        document.body.removeChild(link);
    }
</script>
JAVASCRIPT

关键点:

  • Blazor 服务器 允许进行服务器端操作,并可以直接在服务器上执行 C# 代码。
  • Blazor WebAssembly 运行在客户端,无法直接访问服务器端资源,如 IronPDF。

您可以在我们的网站上查看完整的 Blazor 服务器教程:Blazor 服务器教程

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布