ASP.NET MVC ビューからPDFを生成する(コード例チュートリアル)

チャクニット・ビン
チャクニット・ビン
2023年1月25日
更新済み 2024年10月20日
共有:
This article was translated from English: Does it need improvement?
Translated
View the article in English

既存のHTMLファイルまたは文字列、既存のPDFドキュメント、およびASP.NET MVCのPDFを提供することが可能です。 以下のチュートリアルでは、C#プロジェクトにおいてMVCビューをPDFに変換する方法を説明しています。


ステップ 1

1. IronPDFをインストール

既存のPDFファイル、HTMLファイルまたは文字列を提供するために、またASP.NET MVCでPDFを提供するためには、IronPDFのC# PDFライブラリを使用することができます。 開発用に無償ダウンロードして、以下のチュートリアルで始めましょう。 以下を通してアクセスできますIronPDF DLL ZIPファイルまたは通じてIronPDF NuGetパッケージ.

Install-Package IronPdf

チュートリアルの方法

ASP.NET MVCでPDFを提供

ASP.NET MVCでPDFドキュメントを提供するには、次を生成する必要がありますファイル結果メソッド。 IronPDFを使用してASP.NET MVCフレームワークPDFファイルを返す。

このメソッドは、以下に示すようにコントローラーによって提供されます。

/**
Serve PDF in ASPNET MVC
anchor-serve-pdf-in-asp-net-mvc
**/
public FileResult GetHTMLPageAsPDF(long id) {

  //Create a PDF Document

  using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf and MVC</h1>");

  //return a pdf document from a view

  var contentLength = PDF.BinaryData.Length;

  Response.AppendHeader("Content-Length", contentLength.ToString());

  Response.AppendHeader("Content-Disposition", "inline; filename=Document_" + id + ".pdf");

  return File(PDF.BinaryData, "application/pdf;");

}
/**
Serve PDF in ASPNET MVC
anchor-serve-pdf-in-asp-net-mvc
**/
public FileResult GetHTMLPageAsPDF(long id) {

  //Create a PDF Document

  using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf and MVC</h1>");

  //return a pdf document from a view

  var contentLength = PDF.BinaryData.Length;

  Response.AppendHeader("Content-Length", contentLength.ToString());

  Response.AppendHeader("Content-Disposition", "inline; filename=Document_" + id + ".pdf");

  return File(PDF.BinaryData, "application/pdf;");

}

より高度な例として、HTMLビューを使ってHTML文字列を生成し、それを上記のようにPDFに変換することができます。


既存のPDFファイルを提供する

ユーザーに直接サービスを提供する他のASP.NETコンテキストでのPDFファイルも可能です。

/**
Serve Existing PDF
anchor-serve-existing-pdf-file
**/
Response.Clear();

Response.ContentType = "application/pdf";

Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");

// edit this line to display in browser and change the file name

Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"));

// gets our pdf as a byte array and then sends it to the buffer

Response.Flush();

Response.End();
/**
Serve Existing PDF
anchor-serve-existing-pdf-file
**/
Response.Clear();

Response.ContentType = "application/pdf";

Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");

// edit this line to display in browser and change the file name

Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"));

// gets our pdf as a byte array and then sends it to the buffer

Response.Flush();

Response.End();

4. 既存のHTMLファイルまたは文字列を提供

/**
Serve Existing HTML File or String
anchor-serve-existing-html-file-or-string
**/
var Renderer = new IronPdf.ChromePdfRenderer();

using var PDF = Renderer.RenderHTMLFileAsPdf("Project/MyHtmlDocument.html");

// or to convert an HTML string

//var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");

Response.Clear();

Response.ContentType = "application/pdf";

Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");

// edit this line to display in browser and change the file name

Response.BinaryWrite( PDF.BinaryData );

Response.Flush();

Response.End();
/**
Serve Existing HTML File or String
anchor-serve-existing-html-file-or-string
**/
var Renderer = new IronPdf.ChromePdfRenderer();

using var PDF = Renderer.RenderHTMLFileAsPdf("Project/MyHtmlDocument.html");

// or to convert an HTML string

//var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");

Response.Clear();

Response.ContentType = "application/pdf";

Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");

// edit this line to display in browser and change the file name

Response.BinaryWrite( PDF.BinaryData );

Response.Flush();

Response.End();
チャクニット・ビン
ソフトウェアエンジニア
ChaknithはIronXLとIronBarcodeで作業しています。彼はC#と.NETに深い専門知識を持ち、ソフトウェアの改善と顧客サポートを支援しています。ユーザーとの対話から得た彼の洞察は、より良い製品、文書、および全体的な体験に貢献しています。