ASP.NET MVC Generar PDF desde vista (Tutorial de ejemplo de código)
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#.
ASP.NET MVC Generar PDF desde la vista Tutorial
- Descargar ASP.NET MVC Generate PDF from View Library (en inglés)
- Instalar en Visual Studio
- Servir PDF en ASP.NET MVC
- Servir archivo PDF existente
- Servir archivo o cadena HTML existente
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) {
//Crear un documento PDF
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf and MVC</h1>");
//devolver un documento pdf desde una vista
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) {
//Crear un documento PDF
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf and MVC</h1>");
//devolver un documento pdf desde una vista
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
'Crear un documento PDF
Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf and MVC</h1>")
'devolver un documento pdf desde una vista
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
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\"");
// edita esta línea para mostrar ion browser y cambia el nombre del archivo
Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"));
// obtiene nuestro pdf como un array de bytes y lo envía al 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\"");
// edita esta línea para mostrar ion browser y cambia el nombre del archivo
Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"));
// obtiene nuestro pdf como un array de bytes y lo envía al 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""")
' edita esta línea para mostrar ion browser y cambia el nombre del archivo
Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"))
' obtiene nuestro pdf como un array de bytes y lo envía al buffer
Response.Flush()
Response.End()
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");
// o para convertir una cadena HTML
//var PDF = Renderer.RenderHtmlAsPdf("<h1>Hola IronPdf</h1>");
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");
// edita esta línea para mostrar ion browser y cambia el nombre del archivo
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");
// o para convertir una cadena HTML
//var PDF = Renderer.RenderHtmlAsPdf("<h1>Hola IronPdf</h1>");
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");
// edita esta línea para mostrar ion browser y cambia el nombre del archivo
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")
' o para convertir una cadena HTML
'var PDF = Renderer.RenderHtmlAsPdf("<h1>Hola IronPdf</h1>");
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition","attachment;filename=""FileName.pdf""")
' edita esta línea para mostrar ion browser y cambia el nombre del archivo
Response.BinaryWrite(PDF.BinaryData)
Response.Flush()
Response.End()