ASP.NET Core C# アプリで Razor ページを PDF に変換する方法
IronPDF は、ASP.NET Core アプリケーションで Razor Pages (.cshtml ファイル) を PDF ドキュメントにスムーズに変換できるようにし、RenderRazorToPdf メソッドを使用してウェブコンテンツからの PDF 生成を簡素化し、C# および HTML レンダリングを完全にサポートします。
Razor Page は、C# と HTML を組み合わせてウェブコンテンツを生成する .cshtml 拡張子のファイルです。 .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 に変換する.NET CoreアプリケーションでRazor Pagesを高品質のPDFに変換します。 RenderRazorToPdf メソッドを使用することで、CSHTML ファイルを PDF ドキュメントに変換し、ワークフローを最適化し、ドキュメントの配布を向上させることができます。 このガイドでは、これを数分で達成するために必要な簡単な手順を説明します。
-
1IronPDF を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
// Install-Package IronPdf.Extensions.Razor var pdf = new IronPdf.ChromePdfRenderer().RenderRazorToPdf("Views/Home/Index.cshtml");Dim pdf = New IronPdf.ChromePdfRenderer().RenderRazorToPdf("Views/Home/Index.cshtml")Dim pdf = New IronPdf.ChromePdfRenderer().RenderRazorToPdf("Views/Home/Index.cshtml") -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronPDFを使い始めるには、無料トライアルをお試しください。
最小限のワークフロー(5ステップ)
- ASP.NET Core Web アプリで Razor ページを PDF に変換するための C# ライブラリをダウンロード
- データ用のモデルクラスを追加する
- 新しいRazorページを作成し、データを表示するために".cshtml "ファイルを編集してください。
- .cs "ファイルを編集し、
RenderRazorToPdfメソッドを使用してください。 - クイックスタート用のサンプルプロジェクトをダウンロード
はじめに
Razor Pages は、ASP.NET Core アプリケーションで動的な Web コンテンツを構築するための強力かつ直感的な方法を提供します。 IronPDFのレンダリング機能と組み合わせることで、開発者はウェブコンテンツから直接プロフェッショナルなPDFドキュメントを作成することができます。 このアプローチでは、複雑なPDF生成ロジックを排除し、既存のHTMLとCSSのスキルを使用することができます。
IronPDFとRazor Pagesの統合は、レポート、請求書、証明書、その他ダイナミックなデータ表示が必要な文書の作成に特に有効です。 すでに使い慣れたRazorの構文を使用することで、WebビューとPDF出力の一貫性を保つことができます。
RazorをPDFに変換するために必要なNuGetパッケージは何ですか?
IronPdf.Extensions.Razor パッケージ は、メインの IronPdf パッケージの拡張です。 Razor ページを ASP.NET Core Web アプリで PDF ドキュメントにレンダリングするには、IronPdf.Extensions.Razor および 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に変換するにはどうすればいいですか?
Razor PagesをPDFファイルに変換するには、ASP.NET Core Web Appプロジェクトが必要です。 プロセスには、データ用のモデルを作成し、そのデータを表示するための Razor ページを設定し、その後 IronPDF の RenderRazorToPdf メソッドを使用して PDF 出力を生成することが含まれます。
なぜ PDF 生成にモデルクラスが必要ですか?
モデルクラスは、Razor Pages のデータ表現のバックボーンとして機能します。 これらのツールは、コントローラロジックからビューにデータを渡すための構造化された方法を提供し、型の安全性と保守性を保証します。 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 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 NamespaceNamespace 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 NamespacePDF変換用のRazorページをどのように設定しますか?
"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 PagesをPDFにレンダリングする
次に、次のコードはChromePdfRendererクラスをインスタンス化します。 この Razor ページを PDF ドキュメントに変換するには、この this を RenderRazorToPdf メソッドに渡すだけで十分です。
あなたは、RenderingOptionsで利用可能な機能に完全にアクセスできます。 これらの機能には、生成された PDF にページ番号を適用したり、カスタムマージンを設定したり、カスタムテキストと HTML ヘッダーおよびフッターを追加したりする機能があります。 PDF のメタデータを設定して、確実なドキュメントの識別と検索可能性を確保することもできます。
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 NamespaceImports 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 NamespaceRenderRazorToPdf メソッドは、追加の処理や編集が可能な PdfDocument オブジェクトを返します。 PDF を PDFA または PDFUA としてエクスポートしたり、レンダリングされた PDF ドキュメントにデジタル署名を適用したり、PDF ドキュメントを結合および分割することができます。 この方法では、ページの回転、注釈やしおりの追加、PDFへのカスタム透かしのスタンプも可能です。
文書管理を強化するために、品質を損なうことなくファイルサイズを縮小するためにPDFを圧縮することもできます。 これは、大規模なレポートを扱う場合や、帯域幅が懸念される場合に特に役立ちます。 さらに、IronPDFが提供する幅広い編集機能については、包括的なPDF編集チュートリアルで説明しています。
PDF生成ページへのナビゲーションをどのように追加しますか?
ナビゲーションは、ASP.NET Coreアプリケーションのユーザーエクスペリエンスにとって非常に重要です。 PDF生成ページをメインナビゲーションに統合することで、ユーザーはURLを手入力することなく、簡単に機能にアクセスすることができます。
- Pagesフォルダ -> Sharedフォルダ -> _Layout.cshtmlに移動します。 "Home"の後に「Person」ナビゲーション項目を配置します。
この場合、asp-page 属性の値がファイル名と正確に一致していることを確認してください。この場合、ファイル名は 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 Pageビューがダウンロード可能なPDFドキュメントに変換されます。
生成されるPDFは、Razorページのすべてのスタイルと書式を維持し、ウェブビューとPDF出力の間で一貫した外観を保証します。 このアプローチは、レポートや請求書の作成、またはアプリケーションのデータベースやビジネスロジックからのデータを必要とする文書の作成に特に役立ちます。
完全なASP.NET Core Webアプリの例はどこでダウンロードできますか?
このガイドの完全なコードをVisual Studioで開くことができるASP.NET Core Web Appプロジェクトとしてzipファイルでダウンロードできます。
よくある質問
IronPDFを使用してASP.NET CoreでRazor PagesをPDFに変換する方法は?
IronPDFを使用してASP.NET CoreでRazor PagesをPDFに変換するには、RenderRazorToPdfメソッドを使用します。このメソッドはCSHTMLファイルを簡単にPDF文書に変換することができ、ガイドに示されたサンプルコードで例示されています。
`RenderRazorToPdf`メソッドとは何ですか?
RenderRazorToPdfメソッドは、HTMLとC#コードで構成されるRazor Pagesを高品質のPDF文書に変換することを可能にするIronPDFの機能です。これは、ASP.NET Coreアプリケーション内でのPDF生成プロセスを簡素化します。
IronPDFを使用してRazor PagesをPDFに変換するために特定のNuGetパッケージが必要ですか?
はい、IronPdf.Extensions.RazorとIronPdfの両方のNuGetパッケージをインストールして、ASP.NET Core Web AppでRazor PagesをPDF文書に効果的にレンダリングする必要があります。
Razor PagesからPDFを生成するためにモデルクラスが必要なのはなぜですか?
モデルクラスはPDF文書に表示されるデータ構造を定義するために重要です。これによりデータバインディングが容易になり、Razor PagesがPDFに含めるコンテンツを動的に生成できるようになります。
IronPDFによって生成されたPDFの外観をカスタマイズできますか?
はい、IronPDFのRenderingOptionsを使用して、ページ番号、余白、テキスト、HTMLヘッダーとフッターなどの側面をカスタマイズできます。これにより、ブランドやデザインの好みに合わせてPDFの一貫性を維持することができます。
ASP.NET CoreアプリケーションでPDF生成ページのナビゲーションを設定するにはどうすればよいですか?
ナビゲーションを設定するには、Pagesフォルダ内の_Layout.cshtmlファイルを編集して、PDF生成ページへのリンクを統合します。プロジェクトで使用されているページ名に基づいてルートが正しく定義されていることを確認してください。
IronPDFはPDF文書管理にどのような機能を提供しますか?
IronPDFは、PDF圧縮、デジタル署名統合、文書のマージおよび分割、回転、注釈、ブックマーク作成、透かしスタンプなどの機能を提供し、包括的なPDF文書管理機能を提供します。
IronPDFを使用したASP.NET Core Web Appの完全な例はどこで見つけられますか?
Razor PageからPDFへの変換をデモンストレーションしたIronPDFを使用したASP.NET Core Web Appの完全な例は、ガイド内で提供されているリンクからダウンロードでき、プロジェクトファイルとして圧縮されています。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。