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

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 文档需要生成一个 文件结果 方法。使用 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#