Convert MVC View to PDF
Published: 2022-01-20 07:04:05

---
canonical_url: https://ironpdf.com/docs/questions/asp-net-mvc-pdf-binary/
---

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](https://ironpdf.com/packages/IronPdf.Package.For.MVC.View.PDF.zip) or through the [NuGet page](https://www.nuget.org/packages/IronPdf).

`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](https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.fileresult) method. With IronPDF you can use [MVC](https://dotnet.microsoft.com/apps/aspnet/mvc) to return a PDF file.

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

**C#:**
```
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;");
}
```

**VB:**
```
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
```

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](https://ironpdf.com/tutorials/vb-net-pdf/) contexts is also possible.

**C#:**
```
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:**
```
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()
```

---

# 4. Serve Existing HTML File or String

**C#:**
```
 var Renderer = new IronPdf.ChromePdfRenderer();
 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();
```

**VB:**
```
Dim Renderer = New IronPdf.ChromePdfRenderer()
 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()
```