Skip to footer content
USING IRONPDF

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

Create an ASP.NET Core MVC PDF viewer using IronPDF's Chrome-based rendering engine to display PDF files inline in browsers, generate dynamic PDFs from HTML content, and control whether users view or download documents—all without external plugins or dependencies.

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 show you how to create an ASP.NET Core MVC PDF viewer 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's 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");
    }
}
$vbLabelText   $csharpLabel

This simple approach works effectively for serving static PDF files stored on your server. For more advanced scenarios, you might want to load PDFs from memory or Azure Blob Storage, which can improve scalability and reduce server storage requirements.

What Does a PDF Look Like When Displayed in the Browser?

A PDF document about 'What is a PDF?' displayed in a web browser at localhost:7162/Pdf/ViewPdf, showing formatted text content in a PDF viewer interface with zoom controls and navigation options

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. The rendering engine supports modern web standards including CSS3, JavaScript ES6+, and web fonts.

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");
    }
}
$vbLabelText   $csharpLabel

How Does HTML Content Appear After PDF Generation?

PDF viewer displaying a Monthly Sales Report with formatted header text and generation date, demonstrating HTML-to-PDF conversion with custom CSS styling applied through IronPDF

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, making it ideal for creating reports, invoices, and other documents that require consistent formatting. The controller handles the request and returns the rendered output to users.

For additional information and code examples on HTML to PDF conversion, explore IronPDF's comprehensive documentation. You can also generate PDFs from CSHTML Razor views, URLs, or even Markdown content.

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. How the browser handles the response depends on the Content-Disposition header. Understanding this distinction is crucial for providing the right user experience.

using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class PdfController : Controller
{
    public IActionResult DisplayInline()
    {
        var renderer = new ChromePdfRenderer();
        // Configure rendering options for better output
        renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;
        renderer.RenderingOptions.MarginTop = 25;
        renderer.RenderingOptions.MarginBottom = 25;

        PdfDocument pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_42___");
        // Display PDF inline in browser
        return File(pdf.BinaryData, "application/pdf");
    }

    public IActionResult DownloadPdf()
    {
        var renderer = new ChromePdfRenderer();
        // Set additional options for downloaded PDFs
        renderer.RenderingOptions.PaperSize = PdfPaperSize.Letter;
        renderer.RenderingOptions.EnableJavaScript = true;

        PdfDocument pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_43___");
        // 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();
        // Configure rendering options for better output
        renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;
        renderer.RenderingOptions.MarginTop = 25;
        renderer.RenderingOptions.MarginBottom = 25;

        PdfDocument pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_42___");
        // Display PDF inline in browser
        return File(pdf.BinaryData, "application/pdf");
    }

    public IActionResult DownloadPdf()
    {
        var renderer = new ChromePdfRenderer();
        // Set additional options for downloaded PDFs
        renderer.RenderingOptions.PaperSize = PdfPaperSize.Letter;
        renderer.RenderingOptions.EnableJavaScript = true;

        PdfDocument pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_43___");
        // Prompt download with specified filename
        return File(pdf.BinaryData, "application/pdf", "webpage-report.pdf");
    }
}
$vbLabelText   $csharpLabel

When Should I Use Inline Display Instead of Download?

Screenshot showing Wikipedia's main page converted to PDF format and displayed inline in a browser window with PDF viewer controls including zoom, page navigation, and print options

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 needs or project requirements. For enhanced control, you can also implement custom headers or configure paper size and orientation settings.

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. The page model can generate and return PDF files using the same IronPDF functionality. This pattern works particularly well for applications already using Razor Pages for their architecture.

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;
        renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;

        // Add header and footer
        renderer.RenderingOptions.TextHeader.CenterText = "Invoice Document";
        renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
        renderer.RenderingOptions.TextFooter.FontSize = 10;

        string html = $@"
            <html>
            <head>
                <style>
                    body {{ font-family: 'Segoe UI', Arial, sans-serif; padding: 40px; }}
                    h1 {{ color: #1a5490; border-bottom: 2px solid #1a5490; padding-bottom: 10px; }}
                    .invoice-details {{ margin: 20px 0; }}
                    table {{ width: 100%; border-collapse: collapse; }}
                    th, td {{ padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }}
                </style>
            </head>
            <body>
                <h1>Invoice #{id}</h1>
                <div class='invoice-details'>
                    <p><strong>Date:</strong> {DateTime.Now:yyyy-MM-dd}</p>
                    <p><strong>Due Date:</strong> {DateTime.Now.AddDays(30):yyyy-MM-dd}</p>
                </div>
                <table>
                    <tr>
                        <th>Description</th>
                        <th>Amount</th>
                    </tr>
                    <tr>
                        <td>Professional Services</td>
                        <td>$1,500.00</td>
                    </tr>
                </table>
                <p style='margin-top: 40px;'>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;
        renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;

        // Add header and footer
        renderer.RenderingOptions.TextHeader.CenterText = "Invoice Document";
        renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
        renderer.RenderingOptions.TextFooter.FontSize = 10;

        string html = $@"
            <html>
            <head>
                <style>
                    body {{ font-family: 'Segoe UI', Arial, sans-serif; padding: 40px; }}
                    h1 {{ color: #1a5490; border-bottom: 2px solid #1a5490; padding-bottom: 10px; }}
                    .invoice-details {{ margin: 20px 0; }}
                    table {{ width: 100%; border-collapse: collapse; }}
                    th, td {{ padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }}
                </style>
            </head>
            <body>
                <h1>Invoice #{id}</h1>
                <div class='invoice-details'>
                    <p><strong>Date:</strong> {DateTime.Now:yyyy-MM-dd}</p>
                    <p><strong>Due Date:</strong> {DateTime.Now.AddDays(30):yyyy-MM-dd}</p>
                </div>
                <table>
                    <tr>
                        <th>Description</th>
                        <th>Amount</th>
                    </tr>
                    <tr>
                        <td>Professional Services</td>
                        <td>$1,500.00</td>
                    </tr>
                </table>
                <p style='margin-top: 40px;'>Thank you for your business!</p>
            </body>
            </html>";

        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
$vbLabelText   $csharpLabel

What Rendering Options Are Available for PDF Customization?

PDF viewer displaying Invoice #20 with professional formatting, including styled headers, due date information, and a thank you message in a dark-themed browser interface

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. You can also add headers and footers, configure page numbers, or set up custom paper sizes. For additional information, explore IronPDF's rendering options documentation.

Advanced features include watermarking, PDF compression, and digital signatures. You can also implement form creation or merge multiple PDFs for more complex document workflows.

What Are the Key Takeaways for Building PDF Viewers?

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

Key benefits of using IronPDF for PDF viewing in ASP.NET Core include:

  • No external viewer plugins required
  • Chrome rendering engine ensures accurate HTML/CSS rendering
  • Support for JavaScript and dynamic content
  • Flexible options for inline viewing and file downloads
  • Extensive customization options for professional output
  • Cross-platform support including Linux and Docker

Whether building a simple document viewer to display PDF files or implementing a complete report generation system, IronPDF provides the tools and features needed for seamless PDF integration. The library integrates smoothly with your ASP.NET Core web application, supporting both traditional MVC controllers and modern Razor Pages approaches.

For production deployments, consider performance optimization techniques such as async rendering and proper memory management. You can also explore advanced features like PDF/A compliance for long-term archival or PDF security for sensitive documents.

Start your free trial to explore IronPDF's full capabilities, or purchase a license for production use. Visit our comprehensive documentation to learn more about advanced features and best practices.

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