// PM > Install-Package IronPdf.Extensions.Razor
using IronPdf.Razor.Pages;
public IActionResult OnPostAsync()
{
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);
Response.Headers.Add("Content-Disposition", "inline");
return File(pdf.BinaryData, "application/pdf", "razorPageToPdf.pdf");
}
' PM > Install-Package IronPdf.Extensions.Razor
Imports IronPdf.Razor.Pages
Public Function OnPostAsync() 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)
Response.Headers.Add("Content-Disposition", "inline")
Return File(pdf.BinaryData, "application/pdf", "razorPageToPdf.pdf")
End Function
Install-Package IronPdf
CSHTML 轉換為 PDF (Razor Pages)
此程式碼範例展示了將 Razor 頁面轉換為 PDF 的過程。
要在 ASP.NET Core Web 應用程式中實現這一點,您需要兩個套件:IronPdf.Extensions.Razor 和 IronPdf。 這些套件共同作用,以實現將Razor頁面渲染成PDF。
透過從Razor頁面渲染,您可以使用RenderingOptions類別提供的全部功能範圍。 生成的 PDF 文件可以進一步編輯或根據需要導出。