IronPDF 操作指南 將 Blazor 伺服器渲染為 PDF IronPDF on Blazor Server App (HTML to PDF Tutorial) Curtis Chau 更新日期:7月 27, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English IronPDF 支援 .NET 6 並包含如 Blazor 這類的專案類型。 使用 Visual Studio,您可以將 IronPDF 添加到您的 Blazor 伺服器應用專案中,並如下例所示地使用它: 快速開始:在 Blazor 伺服器上輕鬆渲染 PDF 快速開始在您的 Blazor 伺服器應用中使用 IronPDF。 此示例展示如何以最少的設置將 HTML 內容渲染成 PDF。只需幾行代碼,即可將您的 Blazor 元件轉換為專業外觀的 PDF。 非常適合希望將 PDF 功能無縫整合到其 Blazor 專案中的開發人員。 Get started making PDFs with NuGet now: Install IronPDF with NuGet Package Manager PM > Install-Package IronPdf Copy and run this code snippet. IronPdf.HtmlToPdf.RenderHtmlAsPdf(htmlContent).SaveAs(outputPath); Deploy to test on your live environment Start using IronPDF in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最小化工作流程(5 步) 安裝適用於 Blazor 應用程序的 HTML 到 PDF 庫 在 Visual Studio 中創建一個新的 Blazor 專案。 通過 URL 將網頁轉換為 PDF 文件 將網頁渲染到客戶端的網頁瀏覽器中 從 HTML 字串中查看 PDF 文件 創建一個新的 Blazor 伺服器專案 創建一個新專案並選擇 Blazor 伺服器應用類型。 class="content-img-align-center"> class="center-image-wrapper"> 將 IronPDF 安裝到您的 Blazor 專案中 創建專案後,按照以下步驟在 Visual Studio 內透過 NuGet 安裝 IronPDF 庫。 在 Visual Studio 的解決方案資源管理器視窗中,右鍵單擊 References 並選擇 Manage NuGet Packages。 選擇 Browse 並搜尋 IronPdf。 選擇最新版本的套件,勾選您專案的複選框,然後點擊安裝。 或者,您可以使用 .NET CLI 來安裝它: Install-Package IronPdf 添加新的 Razor 元件 一旦在您的 Blazor 專案中安裝了 IronPDF,開始練習,添加一個新的 Razor 元件。 在本教程中,我們將其命名為 "IronPdfComponent": class="content-img-align-center"> class="center-image-wrapper"> 之後,按照以下方式更新代碼: @page "/IronPdf" @inject IJSRuntime JS <h3>IronPdfComponent</h3> <EditForm Model="@_InputMsgModel" id="inputText"> <div> <InputTextArea @bind-Value="@_InputMsgModel.HTML" rows="20" /> </div> <div> <button type="button" @onclick="@SubmitHTML">Render HTML</button> </div> </EditForm> @page "/IronPdf" @inject IJSRuntime JS <h3>IronPdfComponent</h3> <EditForm Model="@_InputMsgModel" id="inputText"> <div> <InputTextArea @bind-Value="@_InputMsgModel.HTML" rows="20" /> </div> <div> <button type="button" @onclick="@SubmitHTML">Render HTML</button> </div> </EditForm> HTML @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(); // 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; } = "My new message"; } } @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(); // 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; } = "My new message"; } } code If True Then ' Model to bind user input private InputHTMLModel _InputMsgModel = New InputHTMLModel() 'INSTANT VB TODO TASK: Local functions are not converted by Instant VB: ' 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(); ' ' ' 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 ' var streamRef = New DotNetStreamReference(stream: doc.Stream); ' ' ' Invoke JavaScript function to download the PDF in the browser ' await JS.InvokeVoidAsync("SubmitHTML", fileName, streamRef); ' } 'INSTANT VB TODO TASK: Local functions are not converted by Instant VB: ' public class InputHTMLModel ' { ' public string HTML ' { ' get; ' set; ' } = "My new message"; ' } End If $vbLabelText $csharpLabel 將這段 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> JAVASCRIPT 編輯 Shared 資料夾中的 NavMenu.razor 文件,新增導航標籤到我們的新 Razor 元件。 添加以下代碼: <div class="nav-item px-3"> <NavLink class="nav-link" href="IronPdf"> <span class="oi oi-list-rich" aria-hidden="true"></span> IronPdf </NavLink> </div> <div class="nav-item px-3"> <NavLink class="nav-link" href="IronPdf"> <span class="oi oi-list-rich" aria-hidden="true"></span> IronPdf </NavLink> </div> HTML 應用完這一切後,您可以運行您的解決方案,然後您應該看到這個: class="content-img-align-center"> class="center-image-wrapper"> 常見問題解答 PDF 庫如何與 Blazor 伺服器應用集成? IronPDF 使 PDF 生成和操作在 .NET 應用中成為可能,包括 Blazor 伺服器應用。可以通過 Visual Studio 和 NuGet 集成,用於 HTML 到 PDF 的轉換。 如何在 Blazor 項目中安裝 PDF 庫? 要在 Blazor 項目中安裝 IronPDF,請在 Visual Studio 中導航到方案資源管理器,右鍵單擊“References”,選擇“管理 NuGet 包”,搜尋“IronPdf”並安裝。或者,使用 .NET CLI 命令 dotnet add package IronPdf。 創建新 Blazor 伺服器應用以使用 PDF 庫的步驟是什麼? 要創建新 Blazor 伺服器應用,請打開 Visual Studio,創建一個新項目,並選擇 Blazor 伺服器應用模板。設置項目後,集成 IronPDF 以處理 PDF 生成。 如何在 Blazor 應用程序中使用 PDF 庫將 HTML 渲染為 PDF? 安裝 IronPDF 後,添加新 Razor 元件,注入 IJSRuntime,並使用 IronPdf.ChromePdfRenderer 將HTML輸入渲染為PDF文件。在 _layout.cshtml 中實現 JavaScript 函數下載生成的 PDF。 如何在 Blazor 伺服器端應用中查看 PDF? 要在 Blazor 伺服器端應用中查看 PDF,請使用 IronPDF 通過 URL 將網頁轉換為 PDF 文件,然後在客戶端的網頁瀏覽器中渲染這些 PDF。 JavaScript 在 Blazor 應用中下載 PDF 的角色是什麼? JavaScript 用於創建下載 IronPDF 生成的 PDF 的函數。它將 PDF 內容轉換為 Blob,並通過程式點擊錨元素以啟動下載,確保 PDF 被保存到用戶的設備上。 如何在 Blazor 應用中為 PDF 組件添加導航標籤? 要為 PDF 組件添加導航標籤,請編輯 Shared 文件夾中的 NavMenu.razor 文件,並包含導向到 PDF 路由的 NavLink 元素。 在 Blazor 和 PDF 庫上下文中,什麼是 Razor 元件? Blazor 中的 Razor 元件是在 .razor 文件中定義的可重用 UI 片段。在 IronPDF 的上下文中,它用於處理將 HTML 輸入轉換為 PDF 的用戶界面和邏輯。 如何解決 Blazor 中的 PDF 生成問題? 如果您在 Blazor 中遇到 PDF 生成問題,請檢查 IronPDF 是否正確安裝和 HTML 輸入是否有效。確保 JavaScript 函數正確設置以處理文件下載。 我可以在 Blazor 應用中將整個網頁轉換為 PDF 嗎? 是的,IronPDF 允許您使用 URL 到 PDF 轉換功能將整個網頁轉換為 Blazor 應用中的 PDF。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:16,154,058 查看許可證