ASP.NET MVC ビューからPDFを生成する(コード例チュートリアル)
既存のHTMLファイルまたは文字列、既存のPDFドキュメント、およびASP.NET MVCのPDFを提供することが可能です。 以下のチュートリアルでは、C#プロジェクトにおいてMVCビューをPDFに変換する方法を説明しています。
ASP.NET MVC ビューからPDFを生成するチュートリアル
- ASP.NET MVCビューからPDFを生成するライブラリをダウンロード
- Visual Studioにインストールする
- ASP.NET MVCでPDFを提供
- 既存のPDFファイルを提供する
- 既存のHTMLファイルまたは文字列を提供
ステップ 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;");
}
'''
'''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
より高度な例として、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();
'''
'''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();
'''
'''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 in browser and change the file name
Response.BinaryWrite(PDF.BinaryData)
Response.Flush()
Response.End()