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

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ライブラリを使用することができます。 開発用に無償ダウンロードして、以下のチュートリアルで始めましょう。 以下を通してアクセスできます DLL ZIPファイル または通じて NuGetページ.

Install-Package IronPdf

チュートリアルの方法

ASP.NET MVCでPDFを提供

ASP.NET MVCでPDFドキュメントを提供するには、次を生成する必要があります ファイル結果 メソッド。 IronPDFを使用して 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;");

}
'''
'''Serve PDF in ASPNET MVC
'''anchor-serve-pdf-in-asp-net-mvc
'''*
Public Function GetHTMLPageAsPDF(ByVal id As Long) As FileResult

  'Create a PDF Document

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

  'return a pdf document from a view

  Dim 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;")

End Function
VB   C#

より高度な例として、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 ion 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 ion 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 ion 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()
VB   C#

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 ion 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 ion 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
'''*
 Dim Renderer = New IronPdf.ChromePdfRenderer()

 Dim 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 ion browser and change the file name

Response.BinaryWrite(PDF.BinaryData)

Response.Flush()

Response.End()
VB   C#