Servidor Blazor vs. WebAssembly: Comparación

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

¿Es IronPDF compatible con Blazor Server y Blazor WebAssembly (WASM)?

IronPDF admite Blazor Server pero no Blazor WebAssembly (WASM).

Para guardar un PDF en Blazor Server, necesitas convertir el flujo de documentos PDF a un arreglo de bytes, que luego se pasa a una función de JavaScript para facilitar la descarga.

Aquí tienes un ejemplo de cómo podrías convertir un documento PDF a un arreglo de bytes en una aplicación de Blazor Server y luego usar JavaScript para iniciar una descarga:

@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

A continuación se presenta la función de JavaScript correspondiente para manejar la descarga del PDF. Asegúrate de incluir esto en tu archivo _Host.cshtml o 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

Puntos clave:

  • Blazor Server permite operaciones del lado del servidor y puede ejecutar código C# directamente en el servidor.
  • Blazor WebAssembly se ejecuta en el lado del cliente y no tiene acceso directo a recursos del lado del servidor como IronPDF.

Puedes ver un tutorial completo de Blazor Server en nuestro sitio web: Tutorial de Blazor Server

Curtis Chau
Escritor Técnico

Curtis Chau tiene una licenciatura en Ciencias de la Computación (Carleton University) y se especializa en el desarrollo front-end con experiencia en Node.js, TypeScript, JavaScript y React. Apasionado por crear interfaces de usuario intuitivas y estéticamente agradables, disfruta trabajando con frameworks modernos y creando manuales bien ...

Leer más
¿Listo para empezar?
Nuget Descargas 16,685,821 | Versión: 2025.12 recién lanzado