Updated October 20, 2024
Share:

ASP.NET MVC Generate PDF from View (Code Example Tutorial)

It is possible to serve an existing HTML file or string, an existing PDF document, as well as a PDF in ASP.NET MVC. We outline how in the tutorial below, making it easy to convert MVC view to PDF in your C# project.


Step 1

1. Install IronPDF

In order to serve existing PDF files, HTML files or strings, as well as serving a PDF in ASP.NET MVC, we can use the C# PDF Library from IronPDF. Download it free for development and get started with the tutorial below. Access it via IronPDF DLL ZIP file or through the IronPDF NuGet package.


Install-Package IronPdf

How to Tutorial

2. Serve PDF in ASP.NET MVC

To serve a PDF document in ASP.NET MVC requires generating a FileResult method. With IronPDF you can use ASP.NET MVC framework to return a PDF file.

This method may then be served by your controller as shown below.

/**
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;");

}

For a more advanced example, you might use your HTML View to generate an HTML string and then convert it to PDF as shown above.


3. Serve Existing PDF File

To directly serve a PDF file in other ASP.NET contexts is also possible.

/**
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();

4. Serve Existing HTML File or String

/**
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();
Chaknith Bin

Chaknith Bin

Software Engineer

 LinkedIn

Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.