@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
<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 專案,請在 Visual Studio 中選擇 'Blazor Server App' 作為專案型別。Blazor Server 託管模型在伺服器上執行應用程式邏輯,使其非常適合需要伺服器端處理的 PDF 生成方案,與 IronPDF 一起使用。
using PDF 生成功能的 Blazor Server 應用的先決條件是什麼?
您需要 Visual Studio 2022 或更高版本,包含 ASP.NET 和網頁開發工作負載,外加 .NET 6 SDK 或更高版本。Blazor Server 應用需要不斷的伺服器連接,使其適合使用 IronPDF 從複雜的 HTML 內容生成 PDF,或當處理需留存在伺服器上的敏感資料時。
IronPDF 提供了一個簡單的單行解決方案,用於在 Blazor Server 中的 HTML 至 PDF 轉換:IronPdf.HtmlToPdf.RenderHtmlAsPdf(htmlContent).SaveAs(outputPath)。這允許您通過最少的程式碼,將您的 Blazor 組件轉換為 PDF。
實施在 Blazor 中的 PDF 生成的最小工作流程是什麼?
最小工作流程包括 5 個步驟:1)安裝 IronPDF HTML 到 PDF 函式庫,2)在 Visual Studio 中建立新的 Blazor 專案,3)使用 IronPDF 通過 URL 將網頁轉換為 PDF 文件,4)將網頁內容呈現到客戶端的網頁瀏覽器,5)查看從 HTML 字串生成的 PDF 文件。
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.