RazorビューをPDFにヘッドレス変換する方法
「ヘッドレスレンダリング」という用語は、グラフィカルユーザーインターフェースなしでWebコンテンツをレンダリングするプロセスを指します。(GUI (グラフィカルユーザーインターフェース))またはブラウザウィンドウ。 以下の内容を日本語に翻訳してください:
はい、以下の内容を日本語に翻訳します:
While the document editor provides a rich interface for editing, IronPDF for .NET ensures that you can convert and render perfect PDFs from any HTML content. IronOCR for .NET, on the other hand, is optimized for extracting text from scanned images and PDFs, offering high accuracy and ease of use. With IronXL for .NET, you can read, write, and manipulate Excel files without needing to install Microsoft Excel. These tools from Iron Software are designed to streamline your workflow and enhance productivity.IronPdf.Extensions.Razorパッケージは非常に便利ですが、ヘッドレスレンダリング機能は提供していません。 ヘッドレスレンダリングは、IronPDF.Extensions.Razorパッケージが対応できないユースケースギャップを埋めることができます。
以下を利用しますRazor.Templating.CoreCSHTMLから変換するためのパッケージ(Razorビュー)HTMLに変換し、その後IronPDFを使用してPDFドキュメントを生成します。
この記事は以下のYouTube動画に触発されました:
IronPDFを始めましょう
今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。
RazorビューをPDFにヘッドレス変換する方法
Razor.Templating.Core パッケージ** をインストールして、ASP.NET Core Web App で Razor Views を html ドキュメントに変換します。
Install-Package Razor.Templating.Core
RazorビューをPDFにレンダリング
ASP.NET Coreウェブアプリが必要です。(モデル-ビュー-コントローラ)ビューをPDFファイルに変換するプロジェクト。
ビューを追加
- 「Home」フォルダーを右クリックします。 「add」と「Add View」を選択してください。
-
空のRazorビューを作成し、"Data.cshtml "と名付けます。
Data.cshtmlファイルを編集
PDFにレンダリングしたいHTML文字列を追加してください:
<table class="table">
<tr>
<th>Name</th>
<th>Title</th>
<th>Description</th>
</tr>
<tr>
<td>John Doe</td>
<td>Software Engineer</td>
<td>Experienced software engineer specializing in web development.</td>
</tr>
<tr>
<td>Alice Smith</td>
<td>Project Manager</td>
<td>Seasoned project manager with expertise in agile methodologies.</td>
</tr>
<tr>
<td>Michael Johnson</td>
<td>Data Analyst</td>
<td>Skilled data analyst proficient in statistical analysis and data visualization.</td>
</tr>
</table>
<table class="table">
<tr>
<th>Name</th>
<th>Title</th>
<th>Description</th>
</tr>
<tr>
<td>John Doe</td>
<td>Software Engineer</td>
<td>Experienced software engineer specializing in web development.</td>
</tr>
<tr>
<td>Alice Smith</td>
<td>Project Manager</td>
<td>Seasoned project manager with expertise in agile methodologies.</td>
</tr>
<tr>
<td>Michael Johnson</td>
<td>Data Analyst</td>
<td>Skilled data analyst proficient in statistical analysis and data visualization.</td>
</tr>
</table>
Program.cs ファイルを編集
Program.cs "ファイルの中に、以下のコードを追加してください。 以下のコードは、Razor.Templating.CoreライブラリのRenderAsync
メソッドを使用してRazorビューをHTMLに変換します。 次に、ChromePdfRendererクラスをインスタンス化し、返されるHTML文字列をRenderHtmlAsPdf
メソッドに渡します。 ユーザーは、RenderingOptions を利用して、カスタム追加などのさまざまな機能にアクセスできます。テキスト、HTML ヘッダーとフッターを含む次のPDFでカスタムマージンを定義し、適用するページ番号.
app.MapGet("/PrintPdf", async () =>
{
IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
string html = await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(html, "./wwwroot");
return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf");
});
app.MapGet("/PrintPdf", async () =>
{
IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
string html = await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(html, "./wwwroot");
return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf");
});
app.MapGet("/PrintPdf", Async Function()
IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All
Dim html As String = Await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml")
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html, "./wwwroot")
Return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf")
End Function)
アセットリンクの変更
「Views」フォルダ ->「Shared」フォルダ -> 「_Layout.cshtml」ファイルに移動します。リンクタグ内の「~/」を「./」に変更します。
これは重要です。なぜなら、"~/"はIronPDFとうまく機能しないからです。
プロジェクトを実行
これにより、プロジェクトを実行してPDFドキュメントを生成する方法を示します。

PDFを出力
ASP.NET Core MVC プロジェクトをダウンロード
このガイドの完全なコードをダウンロードできます。これは、ASP.NET Core Web AppとしてVisual Studioで開くことができるzipファイルとして提供されます。(モデル-ビュー-コントローラ)プロジェクト