// PM > Install-Package IronPdf.Extensions.Mvc.Framework
public ActionResult Persons()
{
var 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" }
};
if (HttpContext.Request.HttpMethod == "POST")
{
// Provide the path to your view file
var viewPath = "~/Views/Home/Persons.cshtml";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render Razor view to PDF document
PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons);
Response.Headers.Add("Content-Disposition", "inline");
// View the PDF
return File(pdf.BinaryData, "application/pdf");
}
return View(persons);
}
' PM > Install-Package IronPdf.Extensions.Mvc.Framework
Public Function Persons() As ActionResult
'INSTANT VB NOTE: The local variable persons was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property:
Dim persons_Conflict = 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"
}
}
If HttpContext.Request.HttpMethod = "POST" Then
' Provide the path to your view file
Dim viewPath = "~/Views/Home/Persons.cshtml"
Dim renderer As New ChromePdfRenderer()
' Render Razor view to PDF document
Dim pdf As PdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons_Conflict)
Response.Headers.Add("Content-Disposition", "inline")
' View the PDF
Return File(pdf.BinaryData, "application/pdf")
End If
Return View(persons_Conflict)
End Function
Install-Package IronPdf
CSHTML을 PDF로 변환(MVC 프레임워크)
이 코드 예제는 뷰를 PDF 문서로 변환하는 방법을 보여줍니다.
이를 위해 우리는 IronPdf.Extensions.Mvc.Framework와 IronPdf라는 두 가지 패키지를 활용합니다. 이 패키지들은 뷰를 PDF로 렌더링하는 과정을 용이하게 해줍니다. IronPdf.Extensions.Mvc.Framework 패키지는 IronPdf의 기능을 확장하여 특히 뷰를 PDF로 렌더링할 수 있도록 합니다.
변환을 수행하려면 RenderView 메서드를 사용하십시오. 이 메서드는 몇 가지 주요 입력값을 필요로 합니다. 즉, HttpContext, ".cshtml" 파일의 경로, 그리고 ".cshtml" 템플릿을 채우는 데 필요한 데이터입니다. Persons 액션을 호출하면 현재 뷰를 PDF 문서로 원활하게 렌더링할 수 있습니다.
또한 RenderingOptions 클래스에서 제공하는 포괄적인 기능 세트에 액세스할 수 있습니다. 여기에는 IronPDF를 사용하여 페이지 번호를 추가하고, 텍스트 및 HTML 머리글과 바닥글을 삽입하고, 필요에 따라 PDF 용지 크기를 사용자 지정하는 기능이 포함됩니다. 필요에 따라 추가적인 수정을 하거나 결과 PDF 문서를 내보낼 수 있는 유연성을 제공합니다.