How to Convert Razor Pages to PDFs in ASP.NET Core Web App
Razor Pageは、C#とHTMLを組み合わせてWebコンテンツを生成する.cshtml拡張子のファイルです。 ASP.NET Coreでは、Razor PagesはWebアプリケーション用のコードを整理する簡易的な方法であり、読み取り専用または単純なデータ入力を行うシンプルなページに適しています。
ASP.NET Core Web Appは、ASP.NET Coreを使用して構築されたWebアプリケーションで、これは現代のWebアプリケーションを開発するためのクロスプラットフォームフレームワークです。
IronPDFは、ASP.NET Core Web Appプロジェクト内でRazor PagesからPDFファイルを作成するプロセスを簡素化します。 これにより、ASP.NET Core Web AppsでのPDF生成が簡単かつ直接的になります。
クイックスタート: Razor Pagesを数秒でPDFに変換する
IronPDFの力を活用して、Razor PagesをASP.NET Coreアプリケーション内で迅速に高品質のPDFに変換します。 RenderRazorToPdfメソッドを使用することで、CSHTMLファイルをPDFドキュメントにシームレスに変換でき、ワークフローを最適化し、ドキュメントの配布を強化します。 このガイドは、これを数分で達成するために必要な簡単な手順を案内し、開発者にとってスムーズで効率的なプロセスを保証します。
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
// Install-Package IronPdf.Extensions.Razor var pdf = new IronPdf.ChromePdfRenderer().RenderRazorToPdf("Views/Home/Index.cshtml");Deploy to test on your live environment
最小限のワークフロー(5ステップ)
- ASP.NET Core Web AppでRazorページをPDFに変換するためのC#ライブラリをダウンロード
- データのためのモデルクラスを追加
- 新しいRazor Pageを作成し、データを表示するために「.cshtml」ファイルを編集
- 「.cs」ファイルを編集し、
RenderRazorToPdfメソッドを使用 - クイックスタート用にサンプルプロジェクトをダウンロード
IronPDF拡張パッケージ
IronPdf.Extensions.Razorパッケージは、主要なIronPdfパッケージの拡張です。 ASP.NET Core Web AppでRazor PagesをPDFドキュメントにレンダリングするには、IronPdf.Extensions.RazorおよびIronPdfパッケージの両方が必要です。
# Command to install IronPdf.Extensions.Razor package using NuGet Package Manager
Install-Package IronPdf.Extensions.Razor
# Command to install IronPdf.Extensions.Razor package using NuGet Package Manager
Install-Package IronPdf.Extensions.Razor
NuGetでインストール
Install-Package IronPdf.Extensions.Razor
Razor PagesをPDFにレンダリングする
RazorページをPDFファイルに変換するには、ASP.NET Core Web Appプロジェクトが必要です。
モデルクラスを作成する
- プロジェクトに新しいフォルダを作成し、「Models」と名付けます。
- フォルダに標準的なC#クラスを追加し、「Person」と名付けます。このクラスは個々のデータのモデルとして使用されます。 次のコードスニペットを使用してください。
:path=/static-assets/pdf/content-code-examples/how-to/cshtml-to-pdf-razor-model.cs
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 Pageを追加する
「Pages」フォルダに空のRazor Pageを追加し、「persons.cshtml」と名付けます。
- 以下のコードサンプルを使用して、作成したばかりの「Persons.cshtml」ファイルを修正します。
以下のコードは、ブラウザで情報を表示するためのものです。
@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>
@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>
次に、次のコードはChromePdfRendererクラスをインスタンス化します。 thisをRenderRazorToPdfメソッドに渡すだけで、このRazor PageをPDFドキュメントに変換するのに十分です。
ユーザーはRenderingOptionsで利用可能な機能に完全にアクセスできます。 These features include the ability to apply page numbers to the generated PDF, set custom margins, and add custom text as well as HTML headers and footers.
- 「Persons.cshtml」ファイルを開くと、「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");
}
}
}
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 Namespace
RenderRazorToPdfメソッドは、さらなる処理や編集が可能なPdfDocumentオブジェクトを返します。 You can export the PDF as PDFA or PDFUA, apply a digital signature to the rendered PDF document, or merge and split PDF documents. The method also allows you to rotate pages, add annotations or bookmarks, and stamp custom watermarks onto your PDF.
トップナビゲーションバーにセクションを追加する
- Pagesフォルダ -> Sharedフォルダ -> _Layout.cshtmlに移動します。 "Home"の後に「Person」ナビゲーション項目を配置します。
asp-page属性の値がファイル名と正確に一致していることを確認してください。この場合は「Persons」です。
<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>
<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ドキュメントを生成する方法を示します。
ASP.NET Core Web Appプロジェクトをダウンロード
このガイドの完全なコードをVisual Studioで開くことができるASP.NET Core Web Appプロジェクトとしてzipファイルでダウンロードできます。
よくある質問
ASP.NET CoreでRazorページは何に使われますか?
RazorページはASP.NET Coreでウェブアプリケーションのコードを整理するために使用され、開発者がC#とHTMLを効率的に組み合わせてウェブコンテンツを作成できるようにします。
C#でRazorページをPDFドキュメントに変換するにはどうすればよいですか?
C#でRazorページをPDFドキュメントに変換するには、IronPDFのRenderRazorToPdfメソッドを使用すると、ChromePdfRendererクラスを利用してCSHTMLファイルからPDFを生成します。
RazorページをPDFにレンダリングするために必要なNuGetパッケージはどれですか?
RazorページをPDFにレンダリングするために必要なNuGetパッケージは、IronPdfとIronPdf.Extensions.Razorです。
ASP.NET CoreでPDF変換に必要なパッケージをインストールするにはどうすればよいですか?
NuGetパッケージマネージャーを使用して次のコマンドで必要なパッケージをインストールできます:Install-Package IronPdf.Extensions.Razor。
RazorページをPDFに変換する際にモデルクラスはどんな役割を果たしますか?
「Person」などのモデルクラスはアプリケーションのデータ構造を定義し、Razorページに表示される情報を整理するのに役立ちます。これがPDFに変換されます。
RazorページをPDFドキュメントにレンダリングするにはどのメソッドが使用されますか?
RenderRazorToPdfメソッドがRazorページをPDFドキュメントにレンダリングするのに使用され、IronPDFのChromePdfRendererクラスを利用します。
Razorページを変換する際にPDFの出力を強化することはできますか?
はい、IronPDFを使用すると、レンダリングオプションを使用してページ番号、カスタム余白、HTMLヘッダーおよびフッターなどの機能でPDFの出力を強化できます。
IronPDFでPDFを生成する際の高度な機能にはどのようなものがありますか?
IronPDFの高度な機能には、PDFAまたはPDFUAとしてのエクスポート、デジタル署名の追加、PDFのマージまたは分割、ページの回転、カスタムウォーターマークの適用が含まれます。
RazorページをPDFに変換するためのASP.NET Core Webアプリプロジェクト全体はどこで見つけられますか?
Razor PageからPDFへの変換を練習するための完全なASP.NET Core Web Appプロジェクトは、提供されたリンクから圧縮ファイルとしてダウンロードでき、Visual Studioで使用する準備が整っています。
CSHTML または Razor ビューを PDF に変換する場合、IronPDF は .NET 10 と互換性がありますか?
はい。IronPDFは.NET 10と完全に互換性があり、CSHTML/RazorビューをPDFに変換できます。プロジェクトは追加設定なしで.NET 10 ChromePdfRenderer RenderRazorToPdf非同期PDF生成などの機能は期待どおりに動作します。

