Convert MVC View to PDF

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 DLL ZIP file or through the NuGet page.


 PM > 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 MVC 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;");

}
'''
'''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

  Using 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 Using
End Function
VB   C#

For a more advanced example, you might use your HTML View to generate a 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 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. 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 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()

 Using 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()
 End Using
VB   C#