ASP.NET MVC Generar PDF desde vista (Tutorial de ejemplo de código)

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

Es posible servir un archivo o cadena HTML existente, un documento PDF existente, así como un PDF en ASP.NET MVC. En el siguiente tutorial le explicamos cómo hacerlo, para que le resulte más fácil convertir una vista MVC a PDF en su proyecto C#.


Primer paso

1. Instalar IronPDF

Para servir archivos PDF existentes, archivos HTML o cadenas, así como para servir un PDF en ASP.NET MVC, podemos utilizar la biblioteca C# PDF Library de IronPDF. Descárgalo gratis para el desarrollo y empieza con el tutorial de abajo. Acceda a través de DLL Archivo ZIP o a través del Página NuGet.

Install-Package IronPdf


Tutorial

2. Servir PDF en ASP.NET MVC

Para servir un documento PDF en ASP.NET MVC es necesario generar un archivo ArchivoResultado método. Con IronPDF puede utilizar MVC para devolver un archivo PDF.

Este método puede ser servido por su controlador como se muestra a continuación.

/**
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;");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Para un ejemplo más avanzado, podría utilizar su Vista HTML para generar una cadena HTML y luego convertirla a PDF como se muestra arriba.


3. Servir archivo PDF existente

Para atender directamente a un PDF en otro archivo ASP.NET contextos también es posible.

/**
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();
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

4. Servir archivo o cadena HTML existente

/**
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();
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#