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.

Serve PDF in ASPNET MVC
public FileResult GetHTMLPageAsPDF(long id) {

  //Create a PDF Document

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

}
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
Jump to Article
Serve Existing PDF
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();
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()
Jump to Article
Serve Existing HTML File or String
var Renderer = new IronPdf.HtmlToPdf();

 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();
Dim Renderer = New IronPdf.HtmlToPdf()

 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()
Jump to Article
Try IronPDF free for development


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.

public FileResult GetHTMLPageAsPDF(long id) {

  //Create a PDF Document

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

}
public FileResult GetHTMLPageAsPDF(long id) {

  //Create a PDF Document

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

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

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.

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

 var Renderer = new IronPdf.HtmlToPdf();

 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();
 var Renderer = new IronPdf.HtmlToPdf();

 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();
Dim Renderer = New IronPdf.HtmlToPdf()

 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#