Blazor Sunucu vs. WebAssembly: Karşılaştırma
IronPDF, Blazor Sunucu ve Blazor WebAssembly'yi (WASM) destekliyor mu?
IronPDF, Blazor Sunucuyu destekliyor ancak Blazor WebAssembly (WASM) için desteklemiyor.
Blazor Sunucu'da bir PDF kaydetmek için PDF doküman akışını bir byte dizisine dönüştürmelisiniz; bu daha sonra indirmenin kolaylaştırılması için bir JavaScript fonksiyonuna aktarılır.
Bir Blazor Sunucu uygulamasinda bir PDF dokümani byte dizisine nasıl dönüştürüp JavaScript kullanarak indirimi başlatabileceginizin bir örneği aşağıda verilmiştir:
@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
Aşağıda, PDF indirimini yönetmek için kullanılan JavaScript fonksiyonu yer almaktadır. Bunu _Host.cshtml veya index.html dosyanıza eklediğinizden emin olun:
<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>
Ana Noktalar:
- Blazor Sunucu, sunucu tarafında işlemler yapmaya izin verir ve C# kodunu doğrudan sunucuda çalıştırabilir.
- Blazor WebAssembly, istemci tarafında çalışır ve IronPDF gibi sunucu kaynaklarına doğrudan erişemez.
Web sitemizde tam bir Blazor Sunucu dersini görebilirsiniz: Blazor Sunucu Dersi

