ASP.NET Core: IronPDFでPDFを即時生成
HTML コンテンツを洗練された PDF に変換し、ブラウザーに直接ストリーミングすることで、 ASP.NET Coreでプロフェッショナルな PDF ドキュメントを動的に生成します。ディスク ストレージは必要なく、一時ファイルの管理も必要ありません。
ASP.NET Coreで最新の Web アプリケーションを構築する場合、オンデマンドでPDF ドキュメントを作成する機能が繰り返し必要になります。 支払いが完了したらすぐに請求書をダウンロードする必要があります。 監査人が"エクスポート"をクリックすると、コンプライアンス レポートが即座に表示される必要があります。ユーザーが何か問題が起きたのではないかと疑う前に、証明書が準備されている必要があります。 IronPDF は、Chromium ベースのPDF ライブラリを通じてこれらすべてのシナリオを処理します。このライブラリは、ディスクに何も書き込まずに、HTML (CSS、 JavaScript、Web フォントを含む) をピクセル精度の PDF 出力に変換します。
このガイドでは、ライブラリのインストール、HTML 文字列からの請求書の生成、Entity Framework データからのレポートのストリーミング、ページ ヘッダーとセキュリティ設定の適用、トラフィック量の多いASP.NETアプリケーションのパフォーマンスを維持するためのベスト プラクティスの採用など、知っておく必要のあるすべての内容について説明します。
オンザフライで PDF を作成するとはどういう意味ですか?
"オンザフライ"とは、ドキュメントが HTTP 要求の瞬間にメモリ内に構築され、呼び出し元に直接送信されることを意味します。 PDF ファイルはファイル システムに書き込まれず、バックグラウンド ジョブは作業をキューに入れず、要求間の結果はキャッシュに保存されません。
このアプローチはいくつかの理由で重要です。 まず、クラウド展開ターゲット(Azure App Service、AWS Lambda、Docker コンテナー) は、多くの場合、ローカル ファイル システムが一時的または読み取り専用である環境で実行されます。 このような環境では、PDF を一時フォルダーに生成してそれを読み戻すことは危険です。 2 番目に、ディスク書き込みを回避すると、攻撃対象領域が縮小されます。つまり、後続のリクエストによって誤って間違ったユーザーに提供される可能性のある残留ファイルが存在しないことになります。 3 番目に、メモリのみの生成では、クリティカル パス内の 2 つの I/O 操作 (書き込みと読み取り) がなくなるため、通常は高速になります。
IronPDF のChromePdfRenderer は、生成されたすべてのドキュメントに対して .BinaryData プロパティと .Stream プロパティを公開します。 どちらもASP.NET Core FileResult に直接渡すことができるため、ストリーミングは実際には 1 行で済みます。
ASP.NET CoreプロジェクトにIronPDFをインストールするにはどうすればよいでしょうか?
パッケージ マネージャー コンソールまたは.NET CLI を使用してNuGetパッケージを追加します。
Install-Package IronPdf
dotnet add package IronPdf
Install-Package IronPdf
dotnet add package IronPdf
パッケージをインストールしたら、アプリケーションの起動時にライセンス キーを設定します (通常は、最初のレンダラーが作成される前に、Program.cs で設定します)。
using IronPdf;
// Place license activation before any IronPDF call
License.LicenseKey = "YOUR-LICENSE-KEY";
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
// Register ChromePdfRenderer as a singleton so the Chromium engine
// is initialised once and reused across all requests.
builder.Services.AddSingleton<ChromePdfRenderer>();
var app = builder.Build();
app.MapDefaultControllerRoute();
app.Run();
using IronPdf;
// Place license activation before any IronPDF call
License.LicenseKey = "YOUR-LICENSE-KEY";
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
// Register ChromePdfRenderer as a singleton so the Chromium engine
// is initialised once and reused across all requests.
builder.Services.AddSingleton<ChromePdfRenderer>();
var app = builder.Build();
app.MapDefaultControllerRoute();
app.Run();
Imports IronPdf
' Place license activation before any IronPDF call
License.LicenseKey = "YOUR-LICENSE-KEY"
Dim builder = WebApplication.CreateBuilder(args)
builder.Services.AddControllersWithViews()
' Register ChromePdfRenderer as a singleton so the Chromium engine
' is initialised once and reused across all requests.
builder.Services.AddSingleton(Of ChromePdfRenderer)()
Dim app = builder.Build()
app.MapDefaultControllerRoute()
app.Run()
ChromePdfRenderer をシングルトンとして登録することが重要です。 レンダラーは、初めて使用されるときに内部の Chromium サブプロセスを開始します。 リクエストごとに新しいインスタンスを作成すると、呼び出しごとに起動コストが発生し、負荷がかかったときに数百ミリ秒の遅延が発生します。 シングルトン インスタンスはスレッドセーフであり、追加の構成なしで同時レンダリング要求を処理します。
プライベート フィード用のNuGet.config セットアップを含むインストール オプションの詳細については、インストールの概要をご覧ください。
HTML 文字列から請求書 PDF を生成するにはどうすればよいですか?
最も一般的なオンザフライの使用例は、リクエストごとに内容は変わりますが、レイアウトは一定のままであるトランザクション ドキュメント (請求書、領収書、注文確認書など) を生成することです。
パターンは次のとおりです。補間されたデータを含む HTML 文字列を作成し、それを RenderHtmlAsPdf に渡し、バイナリ結果をファイルのダウンロードとして返します。
using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class DocumentController : Controller
{
private readonly ChromePdfRenderer _renderer;
public DocumentController(ChromePdfRenderer renderer)
{
_renderer = renderer;
}
[HttpGet("invoice/{orderId:int}")]
public IActionResult GetInvoice(int orderId)
{
// In a real application, fetch this from your database or order service.
var order = GetOrderData(orderId);
string html = $"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
body {{ font-family: Arial, sans-serif; margin: 40px; color: #333; }}
h1 {{ color: #1a56db; }}
table {{ width: 100%; border-collapse: collapse; margin-top: 24px; }}
th, td {{ padding: 10px 14px; border: 1px solid #d1d5db; text-align: left; }}
th {{ background: #f3f4f6; }}
tfoot td {{ font-weight: bold; }}
</style>
</head>
<body>
<h1>Invoice #{order.InvoiceNumber}</h1>
<p>Date: {DateTime.UtcNow:yyyy-MM-dd} | Customer: {order.CustomerName}</p>
<table>
<thead><tr><th>Item</th><th>Qty</th><th>Unit Price</th><th>Subtotal</th></tr></thead>
<tbody>
{string.Join("", order.Items.Select(i =>
$"<tr><td>{i.Name}</td><td>{i.Quantity}</td>" +
$"<td>${i.UnitPrice:F2}</td><td>${i.Quantity * i.UnitPrice:F2}</td></tr>"))}
</tbody>
<tfoot>
<tr><td colspan="3">Total</td><td>${order.Items.Sum(i => i.Quantity * i.UnitPrice):F2}</td></tr>
</tfoot>
</table>
</body>
</html>
""";
var pdf = _renderer.RenderHtmlAsPdf(html);
return File(pdf.BinaryData, "application/pdf", $"invoice-{orderId}.pdf");
}
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class DocumentController : Controller
{
private readonly ChromePdfRenderer _renderer;
public DocumentController(ChromePdfRenderer renderer)
{
_renderer = renderer;
}
[HttpGet("invoice/{orderId:int}")]
public IActionResult GetInvoice(int orderId)
{
// In a real application, fetch this from your database or order service.
var order = GetOrderData(orderId);
string html = $"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
body {{ font-family: Arial, sans-serif; margin: 40px; color: #333; }}
h1 {{ color: #1a56db; }}
table {{ width: 100%; border-collapse: collapse; margin-top: 24px; }}
th, td {{ padding: 10px 14px; border: 1px solid #d1d5db; text-align: left; }}
th {{ background: #f3f4f6; }}
tfoot td {{ font-weight: bold; }}
</style>
</head>
<body>
<h1>Invoice #{order.InvoiceNumber}</h1>
<p>Date: {DateTime.UtcNow:yyyy-MM-dd} | Customer: {order.CustomerName}</p>
<table>
<thead><tr><th>Item</th><th>Qty</th><th>Unit Price</th><th>Subtotal</th></tr></thead>
<tbody>
{string.Join("", order.Items.Select(i =>
$"<tr><td>{i.Name}</td><td>{i.Quantity}</td>" +
$"<td>${i.UnitPrice:F2}</td><td>${i.Quantity * i.UnitPrice:F2}</td></tr>"))}
</tbody>
<tfoot>
<tr><td colspan="3">Total</td><td>${order.Items.Sum(i => i.Quantity * i.UnitPrice):F2}</td></tr>
</tfoot>
</table>
</body>
</html>
""";
var pdf = _renderer.RenderHtmlAsPdf(html);
return File(pdf.BinaryData, "application/pdf", $"invoice-{orderId}.pdf");
}
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Public Class DocumentController
Inherits Controller
Private ReadOnly _renderer As ChromePdfRenderer
Public Sub New(renderer As ChromePdfRenderer)
_renderer = renderer
End Sub
<HttpGet("invoice/{orderId:int}")>
Public Function GetInvoice(orderId As Integer) As IActionResult
' In a real application, fetch this from your database or order service.
Dim order = GetOrderData(orderId)
Dim html As String = $"
<!DOCTYPE html>
<html lang=""en"">
<head>
<meta charset=""utf-8"">
<style>
body {{ font-family: Arial, sans-serif; margin: 40px; color: #333; }}
h1 {{ color: #1a56db; }}
table {{ width: 100%; border-collapse: collapse; margin-top: 24px; }}
th, td {{ padding: 10px 14px; border: 1px solid #d1d5db; text-align: left; }}
th {{ background: #f3f4f6; }}
tfoot td {{ font-weight: bold; }}
</style>
</head>
<body>
<h1>Invoice #{order.InvoiceNumber}</h1>
<p>Date: {DateTime.UtcNow:yyyy-MM-dd} | Customer: {order.CustomerName}</p>
<table>
<thead><tr><th>Item</th><th>Qty</th><th>Unit Price</th><th>Subtotal</th></tr></thead>
<tbody>
{String.Join("", order.Items.Select(Function(i) $"<tr><td>{i.Name}</td><td>{i.Quantity}</td>" +
$"<td>${i.UnitPrice:F2}</td><td>${i.Quantity * i.UnitPrice:F2}</td></tr>"))}
</tbody>
<tfoot>
<tr><td colspan=""3"">Total</td><td>${order.Items.Sum(Function(i) i.Quantity * i.UnitPrice):F2}</td></tr>
</tfoot>
</table>
</body>
</html>
"
Dim pdf = _renderer.RenderHtmlAsPdf(html)
Return File(pdf.BinaryData, "application/pdf", $"invoice-{orderId}.pdf")
End Function
End Class
RenderHtmlAsPdf は、Google Chrome と同じ Chromium エンジンを使用して、完全な HTML ドキュメント (CSS グリッド、Flexbox、Web フォント、さらにはインライン SVG) を処理します。 返された PdfDocument は、BinaryData (a byte[]) と Stream (a MemoryStream) を公開します。 BinaryData を File() に "application/pdf" およびファイル名とともに渡すと、ブラウザ ダウンロードがトリガーされます。
ピクセル単位の忠実度が必要なレイアウトについては、レスポンシブ CSS、カスタム フォント、 JavaScriptレンダリングについて説明したHTML から PDF へのレンダリング ガイドを参照してください。
生成された請求書 PDF はどのようになりますか?

ダウンロード ダイアログなしで PDF をブラウザーに直接ストリーミングするにはどうすればよいですか?
PDF をインラインで提供するには (ダウンロードするのではなく、ブラウザの組み込みビューアで開く)、2 つの小さな変更が必要です。Content-Disposition を inline に設定し、File() 呼び出しからファイル名を省略します。
[HttpPost("report/preview")]
public async Task<IActionResult> PreviewReport([FromBody] ReportRequest request)
{
string html = BuildReportHtml(request);
var pdfDocument = await _renderer.RenderHtmlAsPdfAsync(html);
// "inline" tells the browser to display rather than download.
Response.Headers["Content-Disposition"] = "inline; filename=report.pdf";
return new FileContentResult(pdfDocument.BinaryData, "application/pdf");
}
[HttpPost("report/preview")]
public async Task<IActionResult> PreviewReport([FromBody] ReportRequest request)
{
string html = BuildReportHtml(request);
var pdfDocument = await _renderer.RenderHtmlAsPdfAsync(html);
// "inline" tells the browser to display rather than download.
Response.Headers["Content-Disposition"] = "inline; filename=report.pdf";
return new FileContentResult(pdfDocument.BinaryData, "application/pdf");
}
Imports Microsoft.AspNetCore.Mvc
<HttpPost("report/preview")>
Public Async Function PreviewReport(<FromBody> request As ReportRequest) As Task(Of IActionResult)
Dim html As String = BuildReportHtml(request)
Dim pdfDocument = Await _renderer.RenderHtmlAsPdfAsync(html)
' "inline" tells the browser to display rather than download.
Response.Headers("Content-Disposition") = "inline; filename=report.pdf"
Return New FileContentResult(pdfDocument.BinaryData, "application/pdf")
End Function
ASP.NET Coreコントローラーでは、非同期オーバーロード RenderHtmlAsPdfAsync の使用が推奨されます。これは、Chromium がレンダリングしている間にスレッドプールのスレッドが解放され、同時負荷下でもサーバーの応答性が維持されるためです。
メモリベースの PDF 生成はどのように機能しますか?

pdfDocument.BinaryData バイト配列は、完全に管理されたメモリ内に存在します。 中間ファイル パスは関係しません。 Content-Disposition ヘッダーは、PDF をインラインで表示するか、ダウンロードとして提供するかを制御します。これは、HTTP 仕様で定義されているブラウザの動作です。 Azure Blob Storage へのストリーミングを含む MemoryStream アプローチの詳細については、 PDF メモリ ストリーム ドキュメントをご覧ください。
Entity Framework Core クエリ結果から PDF を生成するにはどうすればよいでしょうか?
ほとんどのビジネスアプリケーションは、レポートデータを呼び出し時に構築するのではなく、データベースから取得します。以下のパターンは、 Entity Framework Core にクエリを実行し、HTML テーブルを構築して PDF を返します。これらはすべて、単一のコントローラーアクション内で実行されます。
[HttpGet("report/monthly")]
public async Task<IActionResult> MonthlyReport(int year, int month)
{
// Pull aggregated transaction data from EF Core.
var rows = await _dbContext.Transactions
.Where(t => t.Date.Year == year && t.Date.Month == month)
.GroupBy(t => t.Category)
.Select(g => new { Category = g.Key, Count = g.Count(), Total = g.Sum(t => t.Amount) })
.OrderByDescending(g => g.Total)
.ToListAsync();
string tableRows = string.Join("", rows.Select(r =>
$"<tr><td>{r.Category}</td><td>{r.Count}</td><td>${r.Total:F2}</td></tr>"));
string html = $"""
<html><body style="font-family:Arial,sans-serif;padding:32px">
<h1>Monthly Report -- {month:D2}/{year}</h1>
<table style="width:100%;border-collapse:collapse">
<thead>
<tr style="background:#e5e7eb">
<th style="padding:8px;border:1px solid #d1d5db">Category</th>
<th style="padding:8px;border:1px solid #d1d5db">Transactions</th>
<th style="padding:8px;border:1px solid #d1d5db">Total</th>
</tr>
</thead>
<tbody>{tableRows}</tbody>
</table>
</body></html>
""";
var pdf = _renderer.RenderHtmlAsPdf(html);
pdf.MetaData.Title = $"Monthly Report {month:D2}/{year}";
pdf.MetaData.Author = "Reporting System";
return File(pdf.BinaryData, "application/pdf", $"report-{year}-{month:D2}.pdf");
}
[HttpGet("report/monthly")]
public async Task<IActionResult> MonthlyReport(int year, int month)
{
// Pull aggregated transaction data from EF Core.
var rows = await _dbContext.Transactions
.Where(t => t.Date.Year == year && t.Date.Month == month)
.GroupBy(t => t.Category)
.Select(g => new { Category = g.Key, Count = g.Count(), Total = g.Sum(t => t.Amount) })
.OrderByDescending(g => g.Total)
.ToListAsync();
string tableRows = string.Join("", rows.Select(r =>
$"<tr><td>{r.Category}</td><td>{r.Count}</td><td>${r.Total:F2}</td></tr>"));
string html = $"""
<html><body style="font-family:Arial,sans-serif;padding:32px">
<h1>Monthly Report -- {month:D2}/{year}</h1>
<table style="width:100%;border-collapse:collapse">
<thead>
<tr style="background:#e5e7eb">
<th style="padding:8px;border:1px solid #d1d5db">Category</th>
<th style="padding:8px;border:1px solid #d1d5db">Transactions</th>
<th style="padding:8px;border:1px solid #d1d5db">Total</th>
</tr>
</thead>
<tbody>{tableRows}</tbody>
</table>
</body></html>
""";
var pdf = _renderer.RenderHtmlAsPdf(html);
pdf.MetaData.Title = $"Monthly Report {month:D2}/{year}";
pdf.MetaData.Author = "Reporting System";
return File(pdf.BinaryData, "application/pdf", $"report-{year}-{month:D2}.pdf");
}
Imports Microsoft.AspNetCore.Mvc
Imports System.Threading.Tasks
Imports System.Linq
<HttpGet("report/monthly")>
Public Async Function MonthlyReport(year As Integer, month As Integer) As Task(Of IActionResult)
' Pull aggregated transaction data from EF Core.
Dim rows = Await _dbContext.Transactions _
.Where(Function(t) t.Date.Year = year AndAlso t.Date.Month = month) _
.GroupBy(Function(t) t.Category) _
.Select(Function(g) New With {Key .Category = g.Key, Key .Count = g.Count(), Key .Total = g.Sum(Function(t) t.Amount)}) _
.OrderByDescending(Function(g) g.Total) _
.ToListAsync()
Dim tableRows As String = String.Join("", rows.Select(Function(r) $"<tr><td>{r.Category}</td><td>{r.Count}</td><td>${r.Total:F2}</td></tr>"))
Dim html As String = $"
<html><body style='font-family:Arial,sans-serif;padding:32px'>
<h1>Monthly Report -- {month:D2}/{year}</h1>
<table style='width:100%;border-collapse:collapse'>
<thead>
<tr style='background:#e5e7eb'>
<th style='padding:8px;border:1px solid #d1d5db'>Category</th>
<th style='padding:8px;border:1px solid #d1d5db'>Transactions</th>
<th style='padding:8px;border:1px solid #d1d5db'>Total</th>
</tr>
</thead>
<tbody>{tableRows}</tbody>
</table>
</body></html>
"
Dim pdf = _renderer.RenderHtmlAsPdf(html)
pdf.MetaData.Title = $"Monthly Report {month:D2}/{year}"
pdf.MetaData.Author = "Reporting System"
Return File(pdf.BinaryData, "application/pdf", $"report-{year}-{month:D2}.pdf")
End Function
pdf.MetaData.Title と pdf.MetaData.Author を設定すると、その情報が PDF のドキュメント プロパティに埋め込まれ、コンプライアンス追跡やドキュメント管理システムに役立ちます。 より洗練されたレポート レイアウトの場合は、 CSS 印刷スタイル、明示的なページ区切り、埋め込みグラフ画像を検討してください。
生成された PDF にヘッダー、フッター、セキュリティを適用するにはどうすればよいですか?
制作ドキュメントでは、ドキュメントのタイトルを含むヘッダー、ページ番号のフッター、および不正な印刷やコピーを防ぐアクセス制御が頻繁に必要になります。 IronPDF の ChromePdfRenderOptions はこれらすべての要件をカバーします。
[HttpPost("document/secured")]
public async Task<IActionResult> GenerateSecuredDocument([FromBody] SecuredDocRequest request)
{
var renderOptions = new ChromePdfRenderOptions
{
ペーパーサイズ = Pdfペーパーサイズ.A4,
MarginTop = 45,
MarginBottom = 45,
MarginLeft = 25,
MarginRight = 25,
JavaScriptを有効にする = true,
WaitFor = new WaitFor { RenderDelay = 500 }
};
renderOptions.TextHeader = new テキストヘッダーフッター
{
CenterText = request.DocumentTitle,
DrawDividerLine = true,
FontSize = 11
};
renderOptions.TextFooter = new テキストヘッダーフッター
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
FontSize = 9
};
_renderer.RenderingOptions = renderOptions;
var pdf = await _renderer.RenderHtmlAsPdfAsync(request.HtmlContent);
if (request.RequirePassword)
{
pdf.SecuritySettings.OwnerPassword = request.OwnerPassword;
pdf.SecuritySettings.UserPassword = request.UserPassword;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
}
return File(pdf.BinaryData, "application/pdf", $"{request.FileName}.pdf");
}
[HttpPost("document/secured")]
public async Task<IActionResult> GenerateSecuredDocument([FromBody] SecuredDocRequest request)
{
var renderOptions = new ChromePdfRenderOptions
{
ペーパーサイズ = Pdfペーパーサイズ.A4,
MarginTop = 45,
MarginBottom = 45,
MarginLeft = 25,
MarginRight = 25,
JavaScriptを有効にする = true,
WaitFor = new WaitFor { RenderDelay = 500 }
};
renderOptions.TextHeader = new テキストヘッダーフッター
{
CenterText = request.DocumentTitle,
DrawDividerLine = true,
FontSize = 11
};
renderOptions.TextFooter = new テキストヘッダーフッター
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
FontSize = 9
};
_renderer.RenderingOptions = renderOptions;
var pdf = await _renderer.RenderHtmlAsPdfAsync(request.HtmlContent);
if (request.RequirePassword)
{
pdf.SecuritySettings.OwnerPassword = request.OwnerPassword;
pdf.SecuritySettings.UserPassword = request.UserPassword;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
}
return File(pdf.BinaryData, "application/pdf", $"{request.FileName}.pdf");
}
Imports Microsoft.AspNetCore.Mvc
<HttpPost("document/secured")>
Public Async Function GenerateSecuredDocument(<FromBody> request As SecuredDocRequest) As Task(Of IActionResult)
Dim renderOptions As New ChromePdfRenderOptions With {
.ペーパーサイズ = Pdfペーパーサイズ.A4,
.MarginTop = 45,
.MarginBottom = 45,
.MarginLeft = 25,
.MarginRight = 25,
.JavaScriptを有効にする = True,
.WaitFor = New WaitFor With {.RenderDelay = 500}
}
renderOptions.TextHeader = New テキストヘッダーフッター With {
.CenterText = request.DocumentTitle,
.DrawDividerLine = True,
.FontSize = 11
}
renderOptions.TextFooter = New テキストヘッダーフッター With {
.LeftText = "{date} {time}",
.RightText = "Page {page} of {total-pages}",
.FontSize = 9
}
_renderer.RenderingOptions = renderOptions
Dim pdf = Await _renderer.RenderHtmlAsPdfAsync(request.HtmlContent)
If request.RequirePassword Then
pdf.SecuritySettings.OwnerPassword = request.OwnerPassword
pdf.SecuritySettings.UserPassword = request.UserPassword
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint
pdf.SecuritySettings.AllowUserCopyPasteContent = False
End If
Return File(pdf.BinaryData, "application/pdf", $"{request.FileName}.pdf")
End Function
WaitFor.RenderDelay 設定は、HTML に Chart.js や ApexCharts などの非同期で描画を完了するチャート ライブラリが含まれている場合に特に便利です。 300 ~ 500 ミリ秒の遅延を設定すると、Chromium が最終的なレンダリング状態をキャプチャできるようになります。 アーカイブ標準を満たす必要があるドキュメントの場合は、上記のアプローチをPDF/A 準拠とデジタル署名と組み合わせます。
フッターテキスト内の {page} および {total-pages} トークンは、 IronPDFによってレンダリング時に自動的に解決されます。追加のヘッダーとフッターのオプションには、ロゴ配置用の HTML ベースのヘッダーやセクションごとのオーバーライド機能などがあります。
どのようなレンダリング オプションが利用可能ですか?
以下の表は、オンザフライ生成に最も役立つ ChromePdfRenderOptions プロパティをまとめたものです。
| プロパティ | タイプ | 目的 |
|---|---|---|
| ペーパーサイズ | Pdfペーパーサイズ | ページのサイズを設定します(A4、レター、リーガル、カスタム) |
| 上マージン / 下マージン | 整数(mm) | 印刷可能領域の間隔を制御します |
| JavaScriptを有効にする | ブール | キャプチャ前にJS実行を許可する |
| WaitFor.RenderDelay | 整数(ミリ秒) | 非同期レンダリングのためにキャプチャを遅延する |
| テキストヘッダー / テキストフッター | テキストヘッダーフッター | ページのヘッダーとフッターの実行 |
| HTMLヘッダー/HTMLフッター | HtmlHeaderFooter | 画像付きのHTML形式のヘッダー/フッター |
| グレースケール | ブール | モノクロPDFを出力 |
| 用紙幅に合わせる | ブール | 幅の広いコンテンツをページに合わせて拡大縮小します |
大量の PDF 生成におけるパフォーマンスのベスト プラクティスは何ですか?
単一のサーバーで数百の同時 PDF 要求を処理する場合、いくつかのアーキテクチャ上の決定がスループットと待ち時間に大きな影響を与えます。
シングルトンレンダラーの登録。インストールセクションで示したように、DIコンテナにChromePdfRendererをシングルトンとして登録することで、リクエストごとに新しいChromiumサブプロセスを開始するコストを回避できます。MicrosoftのASP.NET Coreパフォーマンスガイダンスによると、オブジェクトの割り当てを最小限に抑えることと、高価なリソースを再利用することが、最も効果的な2つの最適化策です。
常に async を使用します。 RenderHtmlAsPdfAsync は Task<PdfDocument> を返し、Chromium の動作中にコントローラー スレッドを一時停止します。 これにより、スレッド プールが解放され、他の着信要求を並行して処理できるようになります。これが、非同期ドキュメントでWeb ホストにこのオーバーロードを推奨している理由です。 同期オーバーロードは、スレッドのブロッキングが許容されるコンソール ツールまたはバックグラウンド サービスにのみ適しています。
可能な場合は中間配列を省略し、直接ストリーミングします。大きなPDFの場合、バイト配列全体をマテリアライズせずに、.Streamをレスポンスボディに直接書き込むことができます。
[HttpGet("document/large")]
public IActionResult StreamLargeDocument(int documentId)
{
string html = BuildLargeDocumentHtml(documentId);
var pdf = _renderer.RenderHtmlAsPdf(html);
// Stream.Position is already at 0; no seek needed.
return File(pdf.Stream, "application/pdf", $"document-{documentId}.pdf");
}
[HttpGet("document/large")]
public IActionResult StreamLargeDocument(int documentId)
{
string html = BuildLargeDocumentHtml(documentId);
var pdf = _renderer.RenderHtmlAsPdf(html);
// Stream.Position is already at 0; no seek needed.
return File(pdf.Stream, "application/pdf", $"document-{documentId}.pdf");
}
Imports Microsoft.AspNetCore.Mvc
<HttpGet("document/large")>
Public Function StreamLargeDocument(documentId As Integer) As IActionResult
Dim html As String = BuildLargeDocumentHtml(documentId)
Dim pdf = _renderer.RenderHtmlAsPdf(html)
' Stream.Position is already at 0; no seek needed.
Return File(pdf.Stream, "application/pdf", $"document-{documentId}.pdf")
End Function
使用後は廃棄してください。 PdfDocument は IDisposable を実装します。 これを using ステートメントでラップすると、基礎となるメモリ バッファーがすぐに解放されます。これは、多数の大きな PDF を連続して生成するときに重要になります。
using var pdf = _renderer.RenderHtmlAsPdf(html);
byte[] data = pdf.BinaryData;
// pdf is disposed here; data is safely copied to the local array.
return File(data, "application/pdf", "output.pdf");
using var pdf = _renderer.RenderHtmlAsPdf(html);
byte[] data = pdf.BinaryData;
// pdf is disposed here; data is safely copied to the local array.
return File(data, "application/pdf", "output.pdf");
Imports System.IO
Using pdf = _renderer.RenderHtmlAsPdf(html)
Dim data As Byte() = pdf.BinaryData
' pdf is disposed here; data is safely copied to the local array.
Return File(data, "application/pdf", "output.pdf")
End Using
Azure 、AWS、Docker、Linux 環境を対象としたクラウド展開ガイダンスについては、 IronPDFドキュメントに環境固有の構成に関する注意事項が記載されています。 起動後の最初のレンダリングが遅い場合は、最初のユーザー リクエストが到着する前にレンダラーを事前初期化する戦略について、ウォームアップおよびキャッシュ ガイドを参照してください。
生成された PDF に透かしを追加するにはどうすればよいですか?
ストリーミング前に、生成されたドキュメントのすべてのページにテキストまたは画像の透かしを追加できます。
[HttpGet("document/draft/{id:int}")]
public IActionResult GetDraftDocument(int id)
{
string html = BuildDocumentHtml(id);
var pdf = _renderer.RenderHtmlAsPdf(html);
// Stamp "DRAFT" diagonally across every page.
pdf.ApplyWatermark(
"<h1 style='color:rgba(200,0,0,0.25);transform:rotate(-45deg)'>DRAFT</h1>",
rotation: 45,
opacity: 30
);
return File(pdf.BinaryData, "application/pdf", $"draft-{id}.pdf");
}
[HttpGet("document/draft/{id:int}")]
public IActionResult GetDraftDocument(int id)
{
string html = BuildDocumentHtml(id);
var pdf = _renderer.RenderHtmlAsPdf(html);
// Stamp "DRAFT" diagonally across every page.
pdf.ApplyWatermark(
"<h1 style='color:rgba(200,0,0,0.25);transform:rotate(-45deg)'>DRAFT</h1>",
rotation: 45,
opacity: 30
);
return File(pdf.BinaryData, "application/pdf", $"draft-{id}.pdf");
}
<AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=False)>
Public Class HttpGetAttribute
Inherits Attribute
Public Sub New(route As String)
End Sub
End Class
<HttpGet("document/draft/{id:int}")>
Public Function GetDraftDocument(id As Integer) As IActionResult
Dim html As String = BuildDocumentHtml(id)
Dim pdf = _renderer.RenderHtmlAsPdf(html)
' Stamp "DRAFT" diagonally across every page.
pdf.ApplyWatermark(
"<h1 style='color:rgba(200,0,0,0.25);transform:rotate(-45deg)'>DRAFT</h1>",
rotation:=45,
opacity:=30
)
Return File(pdf.BinaryData, "application/pdf", $"draft-{id}.pdf")
End Function
画像の透かしやページごとの制御を含む完全な透かし設定オプションについては、透かしのドキュメントを参照してください。
次のステップは何ですか?
ASP.NET Coreでの動的 PDF 生成は一貫したパターンに従います。HTML を構築し、RenderHtmlAsPdf またはその非同期オーバーロードを呼び出し、FileResult を介して結果を返します。 IronPDF は、どの段階でもディスク I/O を必要とせずに、Chromium レンダリング、CSS アプリケーション、 JavaScript実行など、すべてを処理します。
ここから、アプリケーションの要件に応じていくつかの方向を検討できます。 PDF で複数のソース ドキュメントを結合する必要がある場合は、結合と分割のガイドで、既存の PDF を新しくレンダリングされたページに結合する方法について説明します。 PDF に埋め込まれたフォームにユーザーが入力して送信する必要がある場合は、インタラクティブ フォームのドキュメントでフィールド値を作成して読み取る方法が説明されています。 規制の厳しい業界では、 PDF/A 準拠とPDF/UA アクセシビリティにより、ドキュメントがアーカイブおよびアクセシビリティ標準を満たすことが保証されます。
IronPDF を他の製品と比較して評価している場合は、 iText とIronPDF の比較で技術的な内訳を並べて確認できます。 実稼働に移行する準備ができたら、ライセンスを購入してすべての機能のロックを解除し、優先エンジニアリング サポートにアクセスできるようになります。 完全なAPI リファレンスには、このガイドで説明したすべてのクラスとメソッドが記載されています。
実装中にご質問や問題が発生した場合は、エンジニアリングサポートチームがサポートいたします。Blazor Blazorまたは MAUI に関するプラットフォーム固有の注意事項については、各ホストモデルの構成の違いを解説した専用ガイドをご覧ください。
よくある質問
ASP.NET Core で PDF を動的に生成するにはどうすればよいですか?
IronPDF を使用すると、ファイルをディスクに保存することなく、ASP.NET Core 内で動的に PDF を生成できます。これにより、PDF をブラウザに直接ストリーミングできます。
PDF生成にIronPDFを使用する利点は何ですか?
IronPDF は、.NET Core プロジェクト内で直接動的な PDF 作成を可能にする強力なレンダリング エンジンを提供し、サーバー側のストレージを必要とせずに PDF を即座に生成します。
IronPDF を使用して請求書やレポートを作成できますか?
はい、IronPDF は、請求書、レポート、証明書など、ASP.NET Core アプリケーションでオンザフライで生成されるさまざまな種類のドキュメントの作成に適しています。
IronPDF を使用する場合、サーバー側のストレージは必要ですか?
いいえ、IronPDF を使用すると、サーバー側のストレージを必要とせずに PDF を生成してブラウザーに直接ストリーミングできるため、効率的かつ高速になります。
オンザフライ PDF 生成のメリットを享受できるアプリケーションにはどのようなものがありますか?
最新のWebアプリケーション、特にリアルタイムのドキュメント作成を必要とするアプリは、IronPDFのオンザフライPDF生成から大きなメリットを得られます。
IronPDF は .NET Core プロジェクトをサポートしていますか?
はい、IronPDF は .NET Core プロジェクトと完全に互換性があるため、開発者は PDF 生成機能をアプリケーションにシームレスに統合できます。


