Skip to footer content
USING IRONPDF

How to Create an ASP.NET Core MVC PdfViewer with IronPDF

Modern browsers include a built-in PDF viewer that automatically activates when a web application serves PDF files with the correct MIME type. This eliminates the need for third-party tools or plugins, allowing users to display PDF documents directly in their browser. IronPDF, a frequently updated .NET PDF library, makes it simple to generate, render, and display PDF files within ASP.NET Core MVC applications.

In this article, we'll be showing you how to create an ASP.NET Core MVC PDFViewer web application using IronPDF's Chrome-based rendering engine. The primary focus of this guide is achieving pixel-perfect results while maintaining high performance.

Get stated with IronPDF now.
green arrow pointer

How Do Modern Browsers Display PDF Files?

Modern browsers such as Chrome, Firefox, Edge, and Safari include native PDF viewer functionality. When your ASP.NET Core application returns a file with the application/pdf content type, the browser renders the PDF document inline without requiring Adobe Acrobat or external plugins. This built-in PDF viewer supports essential features such as text selection, printing, zoom controls, bookmarks, and page navigation, creating a complete document-viewing experience.

To securely serve existing files, it is best practice to use the hosting environment to locate them rather than relying on directory paths that might change between development and production. Additionally, using a file stream is often more memory-efficient than loading whole byte arrays for large documents.

using Microsoft.AspNetCore.Mvc;
public class DocumentController : Controller
{
    public IActionResult ViewPdf()
    {
        // Path to an existing PDF file in the wwwroot folder
        string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "documents", "sample.pdf");
        byte[] fileBytes = System.IO.File.ReadAllBytes(path);
        // Return file for inline browser display
        return File(fileBytes, "application/pdf");
    }
}
using Microsoft.AspNetCore.Mvc;
public class DocumentController : Controller
{
    public IActionResult ViewPdf()
    {
        // Path to an existing PDF file in the wwwroot folder
        string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "documents", "sample.pdf");
        byte[] fileBytes = System.IO.File.ReadAllBytes(path);
        // Return file for inline browser display
        return File(fileBytes, "application/pdf");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

PDF Viewer: Displaying our PDF File

How to Create an ASP.NET Core MVC PdfViewer with IronPDF: Image 1 - Sample PDF displayed in the browser

The code above reads an existing PDF file from the server and returns it to the browser. The File() method accepts a byte array and a content type, instructing the browser's document viewer to render the content inline. This approach works across all modern browsers on both desktop and mobile devices, providing a consistent experience for all users.

How Can Developers Generate PDF Documents Dynamically?

Static PDF files are useful, but many web applications require dynamically generated documents. IronPDF's ChromePdfRenderer class converts HTML content into professionally rendered PDF files. Install IronPDF via NuGet packages in Visual Studio to get started.

You can include external assets, such as CSS for your specific theme or JavaScript for charts, directly in the HTML string.

using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class ReportController : Controller
{
    public IActionResult GenerateReport()
    {
        var renderer = new ChromePdfRenderer();
        // HTML content with CSS styling
        string HTML = @"
            <html>
            <head>
                <style>
                    body { font-family: Arial, sans-serif; padding: 40px; }
                    h1 { color: #2c3e50; }
                    .report-body { line-height: 1.6; }
                </style>
            </head>
            <body>
                <h1>Monthly Sales Report</h1>
                <div class='report-body'>
                    <p>Generated: " + DateTime.Now.ToString("MMMM dd, yyyy") + @"</p>
                    <p>This report contains the latest sales figures.</p>
                </div>
            </body>
            </html>";
        PdfDocument PDF = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class ReportController : Controller
{
    public IActionResult GenerateReport()
    {
        var renderer = new ChromePdfRenderer();
        // HTML content with CSS styling
        string HTML = @"
            <html>
            <head>
                <style>
                    body { font-family: Arial, sans-serif; padding: 40px; }
                    h1 { color: #2c3e50; }
                    .report-body { line-height: 1.6; }
                </style>
            </head>
            <body>
                <h1>Monthly Sales Report</h1>
                <div class='report-body'>
                    <p>Generated: " + DateTime.Now.ToString("MMMM dd, yyyy") + @"</p>
                    <p>This report contains the latest sales figures.</p>
                </div>
            </body>
            </html>";
        PdfDocument PDF = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

PDF Generated and then Displayed in our Document Viewer

How to Create an ASP.NET Core MVC PdfViewer with IronPDF: Image 2 - Rendering a PDF document from HTML content, then viewing it

This example demonstrates how IronPDF transforms an HTML string into a PDF document. The ChromePdfRenderer uses a Chromium-based engine, ensuring accurate CSS rendering and JavaScript support. The generated PDF maintains all styling defined in the HTML body, making it ideal for creating reports, invoices, and other documents that require consistent formatting. The controller handles the request, and the app returns the rendered output to users.

For additional information and code examples on HTML to PDF conversion, explore IronPDF's comprehensive documentation.

What Options Exist for Inline Display vs. Download?

Sometimes users need to download PDF files rather than view them in the browser. You might have a link on your home page that directs users to these reports. The specifics of how the browser handles the response depend on the Content-Disposition header.

using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class PdfController : Controller
{
    public IActionResult DisplayInline()
    {
        var renderer = new ChromePdfRenderer();
        PdfDocument PDF = renderer.RenderUrlAsPdf("https://example.com");
        // Display PDF inline in browser
        return File(pdf.BinaryData, "application/pdf");
    }
    public IActionResult DownloadPdf()
    {
        var renderer = new ChromePdfRenderer();
        PdfDocument PDF = renderer.RenderUrlAsPdf("https://example.com");
        // Prompt download with specified filename
        return File(pdf.BinaryData, "application/pdf", "webpage-report.pdf");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class PdfController : Controller
{
    public IActionResult DisplayInline()
    {
        var renderer = new ChromePdfRenderer();
        PdfDocument PDF = renderer.RenderUrlAsPdf("https://example.com");
        // Display PDF inline in browser
        return File(pdf.BinaryData, "application/pdf");
    }
    public IActionResult DownloadPdf()
    {
        var renderer = new ChromePdfRenderer();
        PdfDocument PDF = renderer.RenderUrlAsPdf("https://example.com");
        // Prompt download with specified filename
        return File(pdf.BinaryData, "application/pdf", "webpage-report.pdf");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

PDF Rendered and Displayed Inline

How to Create an ASP.NET Core MVC PdfViewer with IronPDF: Image 3 - PDF document being viewed inline after being rendered from URL content

The difference between these two controller actions lies in the third parameter of the File() method. When you provide a filename, ASP.NET Core automatically adds the Content-Disposition: attachment header, prompting users to download the file. Omitting the filename parameter uses the default inline display mode.

This flexibility allows your app to support both viewing scenarios based on user request or project requirements.

How Can Razor Pages Integrate with .NET Core PDF Generation?

Razor Pages in ASP.NET Core MVC provide another approach for implementing a .NET PDF viewer control. The page model can generate and return PDF files using the same IronPDF functionality.

using IronPdf;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class InvoiceModel : PageModel
{
    public IActionResult OnGet(int id)
    {
        var renderer = new ChromePdfRenderer();
        // Configure rendering options
        renderer.RenderingOptions.MarginTop = 20;
        renderer.RenderingOptions.MarginBottom = 20;
        string HTML = $@"
            <html>
            <body>
                <h1>Invoice #{id}</h1>
                <p>Date: {DateTime.Now:yyyy-MM-dd}</p>
                <p>Thank you for your business!</p>
            </body>
            </html>";
        PdfDocument PDF = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class InvoiceModel : PageModel
{
    public IActionResult OnGet(int id)
    {
        var renderer = new ChromePdfRenderer();
        // Configure rendering options
        renderer.RenderingOptions.MarginTop = 20;
        renderer.RenderingOptions.MarginBottom = 20;
        string HTML = $@"
            <html>
            <body>
                <h1>Invoice #{id}</h1>
                <p>Date: {DateTime.Now:yyyy-MM-dd}</p>
                <p>Thank you for your business!</p>
            </body>
            </html>";
        PdfDocument PDF = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

How to Create an ASP.NET Core MVC PdfViewer with IronPDF: Image 4 - Generated PDF

This Razor Pages example demonstrates how the OnGet handler generates a PDF from a URL parameter. The RenderingOptions property allows customization of margins, page orientation, and other settings. For additional information, explore IronPDF's rendering options documentation.

Conclusion

Creating an ASP.NET Core MVC viewer for PDF documents combines browser-native capabilities with IronPDF's powerful generation features. The built-in PDF viewer in modern browsers handles display, print, and navigation functionality automatically when your ASP controller returns files with the correct MIME type. IronPDF simplifies the creation of professional PDF documents from HTML, URLs, or existing files with full support for CSS, JavaScript, and custom rendering options.

Whether building a simple document viewer to display PDF files or implementing a report generation system, IronPDF provides the tools and features needed for seamless PDF integration. The .NET library integrates smoothly with your ASP.NET Core web application.

Start your free trial to explore IronPDF's full capabilities, or purchase a license for production use.

Frequently Asked Questions

How can I display PDF files in ASP.NET Core MVC applications?

You can display PDF files in ASP.NET Core MVC applications by using IronPDF. It allows you to generate, render, and display PDF files directly in the browser using modern built-in PDF viewers.

Do I need third-party plugins to view PDFs in a browser?

No, modern browsers have built-in PDF viewers that automatically activate when serving PDF files with the correct MIME type. IronPDF can help ensure your PDFs are served correctly.

What is the advantage of using IronPDF in ASP.NET Core MVC?

IronPDF is a .NET PDF library that simplifies the process of generating and rendering PDF documents within ASP.NET Core MVC applications, enhancing productivity and streamlining PDF management.

Can IronPDF work with the existing browser PDF viewers?

Yes, IronPDF works seamlessly with existing browser PDF viewers by ensuring the PDFs are served with the correct MIME type for automatic display in the browser.

Is IronPDF frequently updated?

Yes, IronPDF is a frequently updated .NET PDF library, offering the latest features and improvements for handling PDF documents in ASP.NET Core MVC applications.

How does IronPDF handle PDF generation in web applications?

IronPDF provides robust functionalities for generating PDFs from various content types, allowing developers to create dynamic and interactive PDF documents within web applications.

What MIME type should be used to serve PDF files?

To ensure proper display in browsers, PDF files should be served with the MIME type 'application/pdf'. IronPDF can assist in managing this aspect efficiently.

Can I customize PDF rendering in IronPDF?

Yes, IronPDF offers extensive customization options for rendering PDFs, allowing you to tailor the output to meet specific design and functionality requirements.

Does IronPDF support ASP.NET Core MVC applications only?

While IronPDF is excellent for ASP.NET Core MVC applications, it is also versatile and can be used with other .NET applications to handle PDF functionalities.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More