How to Serve PDF Files as Byte Arrays in C# and VB?

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

To Serve an 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();
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#

To Serve an Existing HTML File or string as a PDF


 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 As var = New IronPdf.HtmlToPdf
Dim PDF As var = 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#

Rendering a PDF in ASP.Net using an MVC View

To serve a PDF document in ASP.Net MVC requires generating a FileResult method.

This method may then be served by your controller.


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 above.