Blazor Server vs. WebAssembly: Comparison

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

Unterstützt IronPDF Blazor Server und Blazor WebAssembly (WASM)?

IronPDF unterstützt Blazor Server, aber nicht Blazor WebAssembly (WASM).

Um ein PDF im Blazor Server zu speichern, müssen Sie den PDF-Dokumentenstrom in ein Byte-Array konvertieren, das dann an eine JavaScript-Funktion übergeben wird, um den Download zu erleichtern.

Hier ist ein Beispiel, wie Sie ein PDF-Dokument in einem Blazor-Server-Anwendung in ein Byte-Array konvertieren und dann JavaScript verwenden, um einen Download auszulösen:

@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

Nachfolgend finden Sie die begleitende JavaScript-Funktion zum Verarbeiten des PDF-Downloads. Stellen Sie sicher, dass Sie dies in Ihre _Host.cshtml oder index.html Datei einfügen:

<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

Wichtige Punkte:

  • Blazor Server ermöglicht serverseitige Operationen und kann C# Code direkt auf dem Server ausführen.
  • Blazor WebAssembly wird clientseitig ausgeführt und hat keinen direkten Zugriff auf serverseitige Ressourcen wie IronPDF.

Sie können ein vollständiges Blazor Server-Tutorial auf unserer Website ansehen: Blazor Server Tutorial

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen
Bereit anzufangen?
Nuget Downloads 16,154,058 | Version: 2025.11 gerade veröffentlicht