@code { // Model to bind user input private InputHTMLModel _InputMsgModel = new InputHTMLModel(); private async TaskSubmitHTML() { // Set your IronPDF license keyIronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"; // Create a renderer to convert HTML to PDF var render = new IronPdf.ChromePdfRenderer(); // Configure rendering options for better output render.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4; render.RenderingOptions.MarginTop = 40; render.RenderingOptions.MarginBottom = 40; // Render the HTML input into a PDF document var doc = render.RenderHtmlAsPdf(_InputMsgModel.HTML); var fileName = "iron.pdf"; // Create a stream reference for the PDF content using var streamRef = new DotNetStreamReference(stream: doc.Stream); // Invoke JavaScript function to download the PDF in the browser awaitJS.InvokeVoidAsync("SubmitHTML", fileName, streamRef); } public class InputHTMLModel { public stringHTML { get; set; } = @"<h1>Welcome to IronPDF</h1> <p>This is a sample PDF generated from HTML content in Blazor Server.</p> <ul> <li>Easy to use API</li> <li>High-quality rendering</li> <li>Full HTML5 and CSS3 support</li> </ul>"; }}
@code {
// Model to bind user input
private InputHTMLModel _InputMsgModel = new InputHTMLModel();
private async Task SubmitHTML()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";
// Create a renderer to convert HTML to PDF
var render = new IronPdf.ChromePdfRenderer();
// Configure rendering options for better output
render.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
render.RenderingOptions.MarginTop = 40;
render.RenderingOptions.MarginBottom = 40;
// Render the HTML input into a PDF document
var doc = render.RenderHtmlAsPdf(_InputMsgModel.HTML);
var fileName = "iron.pdf";
// Create a stream reference for the PDF content
using var streamRef = new DotNetStreamReference(stream: doc.Stream);
// Invoke JavaScript function to download the PDF in the browser
await JS.InvokeVoidAsync("SubmitHTML", fileName, streamRef);
}
public class InputHTMLModel
{
public string HTML { get; set; } = @"<h1>Welcome to IronPDF</h1>
<p>This is a sample PDF generated from HTML content in Blazor Server.</p>
<ul>
<li>Easy to use API</li>
<li>High-quality rendering</li>
<li>Full HTML5 and CSS3 support</li>
</ul>";
}
}
ImportsSystem.Threading.TasksImportsMicrosoft.JSInteropImportsIronPdf@Code' Model to bind user inputPrivate _InputMsgModel As New InputHTMLModel()PrivateAsync Function SubmitHTML() AsTask ' Set your IronPDF license keyIronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" ' Create a renderer to convert HTML to PDF Dim render = New IronPdf.ChromePdfRenderer() ' Configure rendering options for better output render.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4 render.RenderingOptions.MarginTop = 40 render.RenderingOptions.MarginBottom = 40 ' Render the HTML input into a PDF document Dim doc = render.RenderHtmlAsPdf(_InputMsgModel.HTML) Dim fileName = "iron.pdf" ' Create a stream reference for the PDF contentUsing streamRef = New DotNetStreamReference(stream:=doc.Stream) ' Invoke JavaScript function to download the PDF in the browserAwaitJS.InvokeVoidAsync("SubmitHTML", fileName, streamRef)EndUsingEnd FunctionPublic Class InputHTMLModel Public Property HTMLAsString = "<h1>Welcome to IronPDF</h1> <p>This is a sample PDF generated from HTML content in Blazor Server.</p> <ul> <li>Easy to use API</li> <li>High-quality rendering</li> <li>Full HTML5 and CSS3 support</li> </ul>"End Class
Imports System.Threading.Tasks
Imports Microsoft.JSInterop
Imports IronPdf
@Code
' Model to bind user input
Private _InputMsgModel As New InputHTMLModel()
Private Async Function SubmitHTML() As Task
' Set your IronPDF license key
IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
' Create a renderer to convert HTML to PDF
Dim render = New IronPdf.ChromePdfRenderer()
' Configure rendering options for better output
render.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4
render.RenderingOptions.MarginTop = 40
render.RenderingOptions.MarginBottom = 40
' Render the HTML input into a PDF document
Dim doc = render.RenderHtmlAsPdf(_InputMsgModel.HTML)
Dim fileName = "iron.pdf"
' Create a stream reference for the PDF content
Using streamRef = New DotNetStreamReference(stream:=doc.Stream)
' Invoke JavaScript function to download the PDF in the browser
Await JS.InvokeVoidAsync("SubmitHTML", fileName, streamRef)
End Using
End Function
Public Class InputHTMLModel
Public Property HTML As String = "<h1>Welcome to IronPDF</h1>
<p>This is a sample PDF generated from HTML content in Blazor Server.</p>
<ul>
<li>Easy to use API</li>
<li>High-quality rendering</li>
<li>Full HTML5 and CSS3 support</li>
</ul>"
End Class
こ for JavaScriptコードを_layout.cshtmlに追加して、BlazorアプリケーションでIronPDFによってレンダリングされたPDFのダウンロードを許可します:
<script> // JavaScript function to download PDFs generated by IronPdf window.SubmitHTML = async (fileName, contentStreamReference) => { // Get the PDF content as an ArrayBuffer const arrayBuffer = await contentStreamReference.arrayBuffer(); // Create a Blob from the ArrayBuffer const blob = new Blob([arrayBuffer]); // Create an object URL for the Blob const url = URL.createObjectURL(blob); // Create an anchor element to initiate the download const anchorElement = document.createElement("a"); anchorElement.href = url; anchorElement.download = fileName ?? "download.pdf"; // Programmatically click the anchor to start the download anchorElement.click(); // Clean up by removing the anchor and revoking the object URL anchorElement.remove();URL.revokeObjectURL(url); };</script>
<script>
// JavaScript function to download PDFs generated by IronPdf
window.SubmitHTML = async (fileName, contentStreamReference) => {
// Get the PDF content as an ArrayBuffer
const arrayBuffer = await contentStreamReference.arrayBuffer();
// Create a Blob from the ArrayBuffer
const blob = new Blob([arrayBuffer]);
// Create an object URL for the Blob
const url = URL.createObjectURL(blob);
// Create an anchor element to initiate the download
const anchorElement = document.createElement("a");
anchorElement.href = url;
anchorElement.download = fileName ?? "download.pdf";
// Programmatically click the anchor to start the download
anchorElement.click();
// Clean up by removing the anchor and revoking the object URL
anchorElement.remove();
URL.revokeObjectURL(url);
};
</script>
IronPDF用にBlazor Serverプロジェクトを設定する際、安全な通信のために'Configure for HTTPS'を選択してください。DockerコンテナでIronPDFを実行する予定がない限り、'Enable Docker'のチェックは外したままにしてください。認証は'None'から始めてください。スペースや特殊文字を使用せず、適切なC#命名規則を使用してください。
Why is JavaScript necessary for PDF downloads in Blazor applications using IronPDF?
In Blazor Server, C# code executes on the server, requiring JavaScript interop to handle client-side functionalities like file downloads. The `DotNetStreamReference` class facilitates efficient PDF transmission from server to client, avoiding large memory usage.
What should I consider when handling large PDFs using IronPDF in Blazor applications?
When managing large PDFs, utilize progress indicators and chunked downloads. Optimize PDF size through compression and configure appropriate server settings to manage the maximum message size and timeouts, ensuring efficient handling of large documents.