ASP.NET MVC 從視圖生成 PDF(代碼示例教程)

This article was translated from English: Does it need improvement?
Translated
View the article in English

可以在ASP.NET MVC中提供現有的HTML文件或字符串、現有的PDF文檔以及PDF。 我們在下面的教程中說明了如何輕鬆地在您的C#項目中將MVC視圖轉換成PDF。


第一步

1. 安裝 IronPDF

為了服務現有的PDF文件、HTML文件或字符串,以及在ASP.NET MVC中提供PDF,我們可以使用IronPDF的C# PDF庫。 免費下載開發版並開始以下教程。 透過以下方式存取IronPDF DLL 壓縮文件或透過IronPDF NuGet 套件.

Install-Package IronPdf

如何操作教程

2. 在 ASP.NET MVC 中提供 PDF

要在ASP.NET MVC中提供PDF文檔需要生成FileResult方法。 使用 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
VB   C#

對於更高階的範例,您可能會使用 HTML 視圖來生成 HTML 字串,然後如上述所示將其轉換為 PDF。


3. 提供現有的 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()
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 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()
VB   C#