Blazor Server a WebAssembly: porównanie
Czy IronPDF obsługuje Blazor Server i Blazor WebAssembly (WASM)?
IronPDF obsługuje Blazor Server, ale nie obsługuje Blazor WebAssembly (WASM).
Aby zapisac PDF w Blazor Server, musisz przekonwertowac strumien dokumentu PDF na tablice bajtow, ktora nastepnie zostanie przekazana do funkcji JavaScript w celu ulatwienia pobrania.
Oto przyklad, jak mozesz przekonwertowac dokument PDF na tablice bajtow w aplikacji Blazor Server, a nastepnie uzyc JavaScript do wyzwolenia pobierania:
@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
Poniżej znajduje się towarzysząca funkcja JavaScript do obslugi pobierania PDF. Upewnij się, że to dodasz do swojego pliku _Host.cshtml lub 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>
Kluczowe punkty:
- Blazor Server umożliwia operacje po stronie serwera i może wykonywać kod C# bezpośrednio na serwerze.
- Blazor WebAssembly dziala po stronie klienta i nie ma bezposredniego dostepu do zasobow po stronie serwera takich jak IronPDF.
Pelny poradnik Blazor Server mozesz zobaczyc na naszej stronie internetowej: Blazor Server Tutorial

