如何在ASP.NET Core C#應用程式中將Razor頁面轉換為PDF
IronPDF能夠在ASP.NET Core應用程式中通過使用.cshtml文件)無縫轉換為PDF文件,從而簡化從網頁內容生成PDF的過程,並全面支持C#和HTML渲染。
Razor頁面是一個具有.cshtml擴展名的文件,結合了C#和HTML以生成網頁內容。 在ASP.NET Core中,Razor頁面是一種更簡單的方法來組織Web應用程式的程式碼,這使得它們非常適合只讀或進行簡單資料輸入的簡單頁面。
ASP.NET Core Web App是一個使用ASP.NET Core構建的Web應用程式,這是一個用於開發現代Web應用程式的跨平台框架。
IronPDF簡化了在ASP.NET Core Web App項目中從Razor頁面建立PDF文件的過程。 這使得在ASP.NET Core Web Apps中生成PDF文件變得簡單明了。
快速開始:在幾秒鐘內將Razor頁面轉換為PDF在ASP.NET Core應用程式中將您的Razor頁面轉換為高品質PDF。 通過使用CSHTML文件轉換為PDF文件,優化您的工作流程並增強文件發佈。 此指南將指導您在幾分鐘內完成這一過程所需的簡單步驟。
-
1Install IronPDF with NuGet Package Manager
PM > Install-Package IronPdf
-
2Copy and run this code snippet.
// Install-Package IronPdf.Extensions.Razor var pdf = new IronPdf.ChromePdfRenderer().RenderRazorToPdf("Views/Home/Index.cshtml");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
最小工作流程 (5步)
- 下載將Razor頁面轉換為PDF的ASP.NET Core Web App的C#程式庫
- 為資料新增模型類
- 建立一個新的Razor頁面並編輯".cshtml"文件以顯示資料
- 編輯".cs"文件並使用
RenderRazorToPdf方法 - 下載範例項目以快速入門
介紹
Razor Pages在ASP.NET Core應用程式中提供了一種強大且直觀的方法來構建動態Web內容。 結合IronPDF的渲染功能,開發者可以直接從其Web內容建立專業的PDF文件。 這種方法去除了複雜的PDF生成邏輯,允許您使用已有的HTML和CSS技能。
IronPDF與Razor Pages的結合對於生成報告、發票、證書和任何需要動態資料展示的文件特別有價值。 使用您已熟悉的Razor語法,您可以保持Web視圖和PDF輸出的一致性。
將Razor轉換為PDF需要哪些NuGet包?
**IronPdf**包的擴展。 在ASP.NET Core Web App中渲染Razor頁面為PDF文件需要IronPdf包。欲了解詳細的安裝指導,請存取我們的安裝概述指南。
# Command to install IronPdf.Extensions.Razor package using NuGet Package Manager
Install-Package IronPdf.Extensions.Razor
使用 NuGet 安裝
Install-Package IronPdf.Extensions.Razor
IronPdf.Extensions.Razor/我如何在ASP.NET Core中將Razor頁面轉換為PDF?
您需要一個ASP.NET Core Web App項目來將Razor頁面轉換為PDF文件。 此過程包括為您的資料建立模型,設置Razor頁面以顯示該資料,然後使用IronPDF的RenderRazorToPdf方法生成PDF輸出。
為何PDF生成需要模型類別?
模型類別作為Razor頁面資料表示的骨幹。 它們提供了一種從控制器邏輯傳遞資料到視圖的結構化方式,確保型別安全和可維護性。 在生成PDF時,這些模型更為重要,因為它們定義了最終文件中顯示的資料的精確結構。
- 在項目中建立一個新文件夾,並命名為"Models"。
- 向文件夾中新增一個標準的C#類,並命名為
Person。 此類將作為個別資料的模型。 使用以下程式碼片段:
namespace RazorPageSample.Models
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}Namespace RazorPageSample.Models
Public Class Person
Public Property Id() As Integer
Public Property Name() As String
Public Property Title() As String
Public Property Description() As String
End Class
End Namespace我如何設置Razor頁面以進行PDF轉換?
在"Pages"文件夾中新增一個空Razor頁面,並命名為persons.cshtml。
- 使用以下提供的程式碼範例修改新建立的
Persons.cshtml文件。
下面的程式碼在瀏覽器中顯示資訊。 請注意,Razor語法允許在HTML中無縫整合C#程式碼,使其非常適合生成可以轉換為PDF的動態內容:
@page
@using RazorPageSample.Models;
@model RazorPageSample.Pages.PersonsModel
@{
}
<table class="table">
<tr>
<th>Name</th>
<th>Title</th>
<th>Description</th>
</tr>
@foreach (var person in ViewData["personList"] as List<Person>)
{
<tr>
<td>@person.Name</td>
<td>@person.Title</td>
<td>@person.Description</td>
</tr>
}
</table>
<form method="post">
<button type="submit">Print</button>
</form>
將Razor頁面渲染為PDF
接下來,下面的程式碼首先實例化ChromePdfRenderer類別。 將this傳遞給RenderRazorToPdf方法足以將此Razor頁面轉換為PDF文件。
您可以完全存取RenderingOptions中提供的功能。 這些功能包括對生成的PDF應用頁碼的能力,設置自定義邊距,以及新增自定義文字以及HTML頁眉和頁腳。 您還可以為您的PDF配置元資料,以確保正確的文件識別和可搜索性。
- 打開
Persons.cshtml.cs文件。 - 使用下面的程式碼修改
Persons.cshtml.cs。
using IronPdf.Razor;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPageSample.Models;
namespace RazorPageSample.Pages
{
public class PersonsModel : PageModel
{
[BindProperty(SupportsGet = true)]
public List<Person> Persons { get; set; }
// Handle GET request to load initial data
public void OnGet()
{
Persons = new List<Person>
{
new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" },
new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" },
new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" }
};
ViewData["personList"] = Persons;
}
// Handle POST request to convert Razor page to PDF
public IActionResult OnPost()
{
Persons = new List<Person>
{
new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" },
new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" },
new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" }
};
ViewData["personList"] = Persons;
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render Razor Page to PDF document
PdfDocument pdf = renderer.RenderRazorToPdf(this);
// Return the generated PDF file with appropriate content headers
Response.Headers.Add("Content-Disposition", "inline");
return File(pdf.BinaryData, "application/pdf", "razorPageToPdf.pdf");
// Optionally view the output PDF in browser (uncomment below line if needed)
// return File(pdf.BinaryData, "application/pdf");
}
}
}Imports IronPdf.Razor
Imports Microsoft.AspNetCore.Mvc
Imports Microsoft.AspNetCore.Mvc.RazorPages
Imports RazorPageSample.Models
Namespace RazorPageSample.Pages
Public Class PersonsModel
Inherits PageModel
<BindProperty(SupportsGet:=True)>
Public Property Persons As List(Of Person)
' Handle GET request to load initial data
Public Sub OnGet()
Persons = New List(Of Person) From {
New Person With {.Name = "Alice", .Title = "Mrs.", .Description = "Software Engineer"},
New Person With {.Name = "Bob", .Title = "Mr.", .Description = "Software Engineer"},
New Person With {.Name = "Charlie", .Title = "Mr.", .Description = "Software Engineer"}
}
ViewData("personList") = Persons
End Sub
' Handle POST request to convert Razor page to PDF
Public Function OnPost() As IActionResult
Persons = New List(Of Person) From {
New Person With {.Name = "Alice", .Title = "Mrs.", .Description = "Software Engineer"},
New Person With {.Name = "Bob", .Title = "Mr.", .Description = "Software Engineer"},
New Person With {.Name = "Charlie", .Title = "Mr.", .Description = "Software Engineer"}
}
ViewData("personList") = Persons
Dim renderer As New ChromePdfRenderer()
' Render Razor Page to PDF document
Dim pdf As PdfDocument = renderer.RenderRazorToPdf(Me)
' Return the generated PDF file with appropriate content headers
Response.Headers.Add("Content-Disposition", "inline")
Return File(pdf.BinaryData, "application/pdf", "razorPageToPdf.pdf")
' Optionally view the output PDF in browser (uncomment below line if needed)
' Return File(pdf.BinaryData, "application/pdf")
End Function
End Class
End NamespacePdfDocument物件,可以進行額外的處理和編輯。 您可以將PDF匯出為PDFUA,將數位簽名應用於渲染的PDF文件,或者合併和拆分PDF文件。 該方法還允許您旋轉頁面,新增註釋或書籤,並在PDF上蓋上自定義水印。
為了增強文件管理,您還可以壓縮PDF以減少文件大小,而不會損失質量。 這在處理大型報告或當帶寬有限時特別有用。 此外,IronPDF提供的廣泛編輯功能在我們綜合的PDF編輯教程中有記錄。
我如何為PDF生成頁面新增導航?
導航對於您的ASP.NET Core應用程式的使用者體驗至關重要。 通過將PDF生成頁面整合到主導航中,使用者可以輕鬆存取此功能,而無需手動輸入URL。
- 導航至Pages文件夾 -> Shared文件夾 -> _Layout.cshtml。 將"Person"導航項目放在"Home"後面。
確保Persons。 這確保了您的ASP.NET Core應用程式內的正確路由:
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-page="/Index">RazorPageSample</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Persons">Person</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
當我運行PDF生成時會發生什麼?
這顯示了您如何運行項目並生成PDF文件。 當您點擊"Person"導航連結時,您將看到資料以表格格式顯示。 點擊"Print"按鈕會觸發PDF生成過程,將當前Razor頁面視圖轉換為可下載的PDF文件。
生成的PDF保留了Razor頁面的所有樣式和格式,確保了您的Web視圖與PDF輸出之間的一致外觀。 這種方法對於生成報告、發票或需要您應用程式的資料庫或業務邏輯的資料的任何文件特別有用。
在哪裡可以下載完整的ASP.NET Core Web App範例?
您可以下載本指南的完整程式碼作為壓縮文件,您可以將其作為ASP.NET Core Web App項目打開在Visual Studio中。
Download the RazorPageSample.zip ASP.NET Core Web App Project
Frequently Asked Questions
如何在 ASP.NET Core 中將 Razor Pages 轉換為 PDF?
您可以在 ASP.NET Core 中使用 IronPDF 的 RenderRazorToPdf 方法,將 Razor Pages 轉換為 PDF。只需安裝 IronPdf.Extensions.Razor 套件,並使用 ChromePdfRenderer 將您的 .cshtml 檔案直接渲染為 PDF 文件。
將 Razor 轉換為 PDF 需要哪些 NuGet 套件?
您需要兩個 NuGet 套件:IronPdf.Extensions.Razor(擴充套件)和 IronPdf(主套件)。這兩套套件皆為在 ASP.NET Core Web Apps 中將 Razor Pages 渲染為 PDF 文件所必需。
在將 Razor Pages 轉換為 PDF/A 時,我可以使用動態資料嗎?
是的,IronPDF 完全支援 Razor Pages 中的動態資料。在轉換為 PDF 時,您可以使用 C# 程式碼、模型綁定以及所有標準的 Razor 語法功能,使其成為生成包含動態內容的報告、發票和證書的理想選擇。
我可以在 Razor Pages 中建立哪些型別的文件?
IronPDF 的 Razor 轉 PDF 功能非常適合生成各類文件型別,包括報告、發票、證書、收據,以及任何其他需要動態呈現資料且需保留現有 HTML 和 CSS 樣式的文件。
我能在專案中多快實現 Razor 轉 PDF 的功能?
using IronPDF,您可在數分鐘內完成 Razor 轉 PDF 的轉換。最簡化的工作流程僅需 5 個步驟:安裝函式庫、新增模型類別、建立 Razor 頁面、編輯 .cs 檔案以使用 RenderRazorToPdf 方法,以及執行您的應用程式。
轉換後是否能保留我的 HTML 和 CSS 樣式?
是的,IronPDF 在將 Razor Pages 轉換為 PDF 時會保留您的 HTML 和 CSS 樣式。這讓您能夠利用已建立的樣式和版面配置,確保網頁檢視與 PDF 輸出之間保持一致性。

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.