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。在下面的教程中,我們概述了如何將 MVC 視圖轉換為 PDF,讓您的 C# 項目更輕鬆。


第一步

1. 安裝IronPDF

為了處理現有的PDF文件、HTML文件或字串,以及在ASP.NET MVC中生成PDF,我們可以使用來自IronPDF的C# PDF庫。免費下載以進行開發並開始使用下面的教程。可通過訪問 DLL ZIP 檔案 或透過 NuGet 頁面.


Install-Package IronPdf

如何操作教程

2. 在 ASP.NET MVC 中提供 PDF 服務

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


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 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#