Blazor Server vs. WebAssembly: Comparison

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

IronPDF prend-il en charge Blazor Server et Blazor WebAssembly (WASM) ?

IronPDF prend en charge Blazor Server mais pas Blazor WebAssembly (WASM).

Pour enregistrer un PDF dans Blazor Server, vous devez convertir le flux de document PDF en un tableau d'octets, qui est ensuite transmis à une fonction JavaScript pour faciliter le téléchargement.

Voici un exemple de la façon dont vous pourriez convertir un document PDF en un tableau d'octets dans une application Blazor Server, puis utiliser JavaScript pour déclencher un téléchargement:

@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

Voici la fonction JavaScript correspondante pour gérer le téléchargement du PDF. Assurez-vous de l'inclure dans votre fichier _Host.cshtml ou index.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

Points Clés :

  • Blazor Server permet des opérations côté serveur et peut exécuter du code C# directement sur le serveur.
  • Blazor WebAssembly s'exécute côté client et n'a pas d'accès direct aux ressources côté serveur comme IronPDF.

Vous pouvez voir un tutoriel complet sur Blazor Server sur notre site Web : Tutoriel Blazor Server

Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite
Prêt à commencer?
Nuget Téléchargements 16,154,058 | Version : 2025.11 vient de sortir