ASP.NET Core WebアプリでRazor PagesをPDFに変換する方法
Razorページは、C#とHTMLを組み合わせてWebコンテンツを生成する.cshtml拡張子のファイルです。 ASP.NET Coreでは、Razor PagesはWebアプリケーションのコードを整理するためのより簡単な方法であり、読み取り専用のページや簡単なデータ入力を行うシンプルなページに適しています。
ASP.NET Core Webアプリは、ASP.NET Coreを使用して構築されたWebアプリケーションです。ASP.NET Coreは、最新のWebアプリケーションを開発するためのクロスプラットフォームフレームワークです。
IronPDFは、ASP.NET Core Webアプリプロジェクト内のRazor PagesからPDFファイルを作成するプロセスを簡素化します。 これにより、ASP.NET Core WebアプリケーションでのPDF生成が簡単かつ直接的になります。
ASP.NET Core WebアプリでRazor PagesをPDFに変換する方法
IronPDF拡張パッケージ
IronPdf.Extensions.Razor パッケージはメインのIronPdfパッケージの拡張機能です。 IronPdf.Extensions.Razor と IronPdf パッケージは、ASP.NET Core Web AppでRazor PagesをPDFドキュメントにレンダリングするために必要です。
Install-Package IronPdf.Extensions.Razor
申し訳ありませんが、翻訳するコンテンツのテキストを提供してください。その後、英語から日本語に翻訳いたします。!-- NUGET ライブラリ ダウンロード ディレクティブ タグ :: スタート -->
でインストール NuGet
パッケージをインストールする:Install-Package IronPdf.Extensions.Razor
申し訳ありませんが、翻訳するコンテンツのテキストを提供してください。その後、英語から日本語に翻訳いたします。!-- NUGETライブラリダウンロード指示タグ :: 終了 -->
RazorページをPDFにレンダリングする
RazorページをPDFファイルに変換するには、ASP.NET Core Webアプリプロジェクトが必要です。
モデルクラスを作成する
- プロジェクトに新しいフォルダーを作成し、「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ページを追加
「Pages」フォルダーに空のRazorページを追加し、名前を「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クラスを最初にインスタンス化します。 RenderRazorToPdf
メソッドにthisを渡すだけで、このRazorページをPDFドキュメントに変換することができます。
ユーザーはRenderingOptionsで利用可能な機能にフルアクセスできます。 これらの機能には、適用する能力が含まれますページ番号生成されたPDFに対して、カスタムマージンを設定し、カスタム要素を追加テキストおよびHTMLヘッダーとフッター.
- 「Persons.cshtml」ファイルのドロップダウンを開き、「Persons.cshtml.cs」ファイルを確認してください。
-
次のコードで「Persons.cshtml.cs」を修正します。
次の内容にご注意ください。
以下のコードを使用して、ブラウザでPDF文書を表示できます:ファイル(pdf.BinaryData, "application/pdf")
. しかし、ブラウザで表示した後にPDFをダウンロードすると、PDFドキュメントが破損してしまいます。
using IronPdf.Razor.Pages;
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; }
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;
}
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");
// View output PDF on browser
return File(pdf.BinaryData, "application/pdf");
}
}
}
using IronPdf.Razor.Pages;
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; }
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;
}
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");
// View output PDF on browser
return File(pdf.BinaryData, "application/pdf");
}
}
}
Imports IronPdf.Razor.Pages
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)
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
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")
' View output PDF on browser
Return File(pdf.BinaryData, "application/pdf")
End Function
End Class
End Namespace
RenderRazorToPdf
メソッドは、追加の処理や編集を行うことができる PdfDocument オブジェクトを返します。 PDFとしてエクスポートできますPDFAはい、以下の内容を日本語に翻訳いたします:
以下の内容を日本語に翻訳してください:
IronPDF allows developers to generate, edit and read PDF files in .NET applications. It simplifies the process of integrating PDF functionalities into software development projects.
Let's get started適用するデジタル署名レンダリングされたPDF文書に、またはマージおよび分割PDFドキュメント。 メソッドは、ページの回転、追加も可能です注釈はい、以下の内容を日本語に翻訳いたします:
以下の内容を日本語に翻訳してください:
IronPDF allows developers to generate, edit and read PDF files in .NET applications. It simplifies the process of integrating PDF functionalities into software development projects.
Let's get started、およびカスタムウォーターマークをスタンプするPDFへ追加します。
ナビゲーションバーの上部にセクションを追加
-
Pages フォルダー -> Shared フォルダー -> _Layout.cshtml に移動します。 「Person」ナビゲーション項目を「Home」の後に配置します。
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ドキュメントを生成する方法を示します。

申し訳ありませんが、翻訳するコンテンツのテキストを提供してください。その後、英語から日本語に翻訳いたします。!-- #### 出力 PDF
--> ## ASP.NET Core Webアプリプロジェクトをダウンロード このガイドのコード一式をzipファイルとしてダウンロードすることができ、Visual StudioでASP.NET Core Web Appプロジェクトとして開くことができます。 [RazorPageSample.zip ASP.NET Core Web App Projectをダウンロードする。](/static-assets/pdf/how-to/cshtml-to-pdf-razor/RazorPageSample.zip)