Blazor Server vs. WebAssembly: Comparison
Does IronPDF support Blazor Server and Blazor WebAssembly (WASM)?
IronPDF supports Blazor Server but not Blazor WebAssembly (WASM).
To save a PDF in Blazor Server, you need to convert the PDF document stream to a byte array, which is then passed to a JavaScript function to facilitate the download.
Here is an example of how you might convert a PDF document to a byte array in a Blazor Server application and then use JavaScript to trigger a download:
@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
Below is the accompanying JavaScript function to handle the PDF download. Make sure to include this in your _Host.cshtml
or index.html
file:
<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>
Key Points:
- Blazor Server allows for server-side operations and can execute C# code directly on the server.
- Blazor WebAssembly runs client-side and doesn’t have direct access to server-side resources like IronPDF.
You can view a full Blazor Server tutorial on our website: Blazor Server Tutorial