Skip to footer content
USING IRONPDF

How to Generate PDF Files in Dotnet Core

Creating PDF documents in .NET Core applications is a common task for developers building web applications that need to generate PDF files for invoices, reports, and other business documents. IronPDF provides a powerful PDF library that simplifies generating PDFs in ASP.NET Core through its Chrome rendering engine, delivering pixel-perfect PDFs every time. In this article, we'll take a look at how you can handle various PDF generation tasks in the .NET environment.

DotNet Core Generate PDF Files, How Does it Work?

IronPDF uses a WebKit rendering engine based on Google Chrome to render HTML content into PDF files. This approach to PDF creation means you can leverage your existing HTML markup and CSS knowledge for creating PDFs without learning complex PDF generation capabilities or dealing with a steep learning curve. The PDF library handles generating PDF documents from web pages automatically.

The library's fluent API allows developers to generate PDF documents from HTML pages, URLs, or HTML content strings. When converting HTML to PDF, IronPDF preserves complex layouts, CSS styling, JavaScript execution, and even web content that relies on dynamic content. This makes it the right tool for .NET developers who need feature-rich PDF conversion capabilities in their .NET applications. The .NET library excels at generating PDF files with perfect accuracy.

How to Install IronPDF via NuGet Package Manager?

Getting started with IronPDF requires just a single command-line tool installation through the NuGet Package Manager. Open the Package Manager Console in Visual Studio and run the following command:

Install-Package IronPdf

How to Generate PDF Documents from HTML Strings?

The simplest way to create a PDF document is by converting HTML content directly. Here's a basic "Hello World" example showing how to generate PDF files:

using IronPdf;

// Create a PDF from HTML string
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
pdf.SaveAs("hello.pdf");
using IronPdf;

// Create a PDF from HTML string
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
pdf.SaveAs("hello.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

In the above code, we created a new PDF document with just a few lines of code. The ChromePdfRenderer class provides the core functionality for generating PDF files from HTML. The pdf object contains your PDF document ready for saving to a file path.

Advanced HTML to PDF Conversion

For a more detailed example demonstrating HTML to PDF capabilities, let's create an invoice document with HTML markup and CSS styling:

using IronPdf;

var html = @"
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; font-size: 14px; }
        .invoice-header { 
            background-color: #2c3e50; 
            color: white; 
            padding: 20px;
            font-family: 'Helvetica', sans-serif;
        }
        .invoice-details { margin: 20px 0; }
        table { width: 100%; border-collapse: collapse; }
        th, td {
            padding: 12px; 
            text-align: left; 
            border-bottom: 1px solid #ddd;
        }
        .total { 
            font-size: 1.2em; 
            font-weight: bold; 
            text-align: right;
        }
    </style>
</head>
<body>
    <div class='invoice-header'>
        <h1>Invoice #2024-001</h1>
        <p>Date: January 15, 2024</p>
    </div>
    <div class='invoice-details'>
        <h3>Bill To: John Doe</h3>
        <table>
            <tr>
                <th>Item</th>
                <th>Quantity</th>
                <th>Price</th>
            </tr>
            <tr>
                <td>Professional License</td>
                <td>1</td>
                <td>$799</td>
            </tr>
        </table>
        <p class='total'>Total: $799.00</p>
    </div>
</body>
</html>";

var renderer = new ChromePdfRenderer();
// Configure rendering options for the PDF document
renderer.RenderingOptions.MarginTop = 10;
renderer.RenderingOptions.MarginBottom = 10;

var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("invoice.pdf");
using IronPdf;

var html = @"
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; font-size: 14px; }
        .invoice-header { 
            background-color: #2c3e50; 
            color: white; 
            padding: 20px;
            font-family: 'Helvetica', sans-serif;
        }
        .invoice-details { margin: 20px 0; }
        table { width: 100%; border-collapse: collapse; }
        th, td {
            padding: 12px; 
            text-align: left; 
            border-bottom: 1px solid #ddd;
        }
        .total { 
            font-size: 1.2em; 
            font-weight: bold; 
            text-align: right;
        }
    </style>
</head>
<body>
    <div class='invoice-header'>
        <h1>Invoice #2024-001</h1>
        <p>Date: January 15, 2024</p>
    </div>
    <div class='invoice-details'>
        <h3>Bill To: John Doe</h3>
        <table>
            <tr>
                <th>Item</th>
                <th>Quantity</th>
                <th>Price</th>
            </tr>
            <tr>
                <td>Professional License</td>
                <td>1</td>
                <td>$799</td>
            </tr>
        </table>
        <p class='total'>Total: $799.00</p>
    </div>
</body>
</html>";

var renderer = new ChromePdfRenderer();
// Configure rendering options for the PDF document
renderer.RenderingOptions.MarginTop = 10;
renderer.RenderingOptions.MarginBottom = 10;

var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("invoice.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This example demonstrates IronPDF's ability to handle complex layouts with CSS styling, including font size and font family settings. The PDF library processes HTML markup to create PDF documents that maintain the exact appearance of your HTML pages. The above code shows how ASP.NET Core applications can generate PDF output for business documents.

Output

How to Generate PDF Files in Dotnet Core: Figure 1 - HTML to PDF output PDF file

How to Convert Web Pages to PDF Files?

IronPDF excels at generating PDFs from live web pages. This functionality is particularly useful for ASP.NET Core web applications that need to capture web content dynamically. The PDF library can generate PDF files from any URL:

using IronPdf;

var renderer = new ChromePdfRenderer();
// Render a webpage URL to PDF
var pdf = renderer.RenderUrlAsPdf("https://example.com");
// Save the PDF document
pdf.SaveAs("webpage.pdf");
using IronPdf;

var renderer = new ChromePdfRenderer();
// Render a webpage URL to PDF
var pdf = renderer.RenderUrlAsPdf("https://example.com");
// Save the PDF document
pdf.SaveAs("webpage.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The .NET library handles JavaScript execution, external resources, and responsive designs automatically when generating PDF files. The renderer object provides access to advanced features for customizing how web pages convert to PDF files.

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.ViewPortWidth = 1920;
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen;
renderer.RenderingOptions.WaitFor.RenderDelay(1000); // Wait for dynamic content

// Render a webpage URL to PDF
var pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
// Save the PDF document
pdf.SaveAs("webpage.pdf");
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.ViewPortWidth = 1920;
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen;
renderer.RenderingOptions.WaitFor.RenderDelay(1000); // Wait for dynamic content

// Render a webpage URL to PDF
var pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
// Save the PDF document
pdf.SaveAs("webpage.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

These customization options ensure that dynamic content loads completely before PDF conversion, resulting in accurate PDF files. The above code demonstrates how ASP.NET Core can generate PDFs from web pages with perfect rendering.

How to Generate PDF Files in Dotnet Core: Figure 2 - Webpage compared to the PDF rendered from the webpage

How to Create PDF Documents in ASP.NET Core Web Applications?

Integrating PDF generation into ASP.NET Core applications is straightforward. The PDF library works seamlessly with ASP.NET Core controllers to generate PDF documents. Here's an example of an API endpoint that creates PDF files:

using Microsoft.AspNetCore.Mvc;
using IronPdf;

[ApiController]
[Route("api/[controller]")]
public class PdfController : ControllerBase
{
    [HttpGet("generate-report")]
    public IActionResult GenerateReport()
    {
        var html = @"
            <h1>Monthly Sales Report</h1>
            <p>Generated on: " + DateTime.Now.ToString() + @"</p>
            <table>
                <tr><th>Product</th><th>Sales</th></tr>
                <tr><td>Product A</td><td>$5,000</td></tr>
                <tr><td>Product B</td><td>$3,500</td></tr>
            </table>";

        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(html);
        // Return PDF file to browser
        return File(pdf.BinaryData, "application/pdf", "output.pdf");
    }
}
using Microsoft.AspNetCore.Mvc;
using IronPdf;

[ApiController]
[Route("api/[controller]")]
public class PdfController : ControllerBase
{
    [HttpGet("generate-report")]
    public IActionResult GenerateReport()
    {
        var html = @"
            <h1>Monthly Sales Report</h1>
            <p>Generated on: " + DateTime.Now.ToString() + @"</p>
            <table>
                <tr><th>Product</th><th>Sales</th></tr>
                <tr><td>Product A</td><td>$5,000</td></tr>
                <tr><td>Product B</td><td>$3,500</td></tr>
            </table>";

        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(html);
        // Return PDF file to browser
        return File(pdf.BinaryData, "application/pdf", "output.pdf");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This controller action creates a new document and sends PDF files directly to the browser. The return statement uses the .NET Core framework's built-in file response handling. ASP.NET Core web applications can generate PDFs for reports, invoices, and other documents easily.

Output PDF File

How to Generate PDF Files in Dotnet Core: Figure 3 - PDF created in ASP.NET

For MVC applications, you can also render HTML from Razor views to create a PDF:

[HttpGet]
public async Task<IActionResult> DownloadInvoice(int id)
{
    // Get invoice data from database
    var model = await GetInvoiceData(id);
    // Render Razor view to HTML string
    var html = await RenderViewToString("Invoice", model);
    // Convert HTML to PDF document
    var renderer = new ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf(html);
    // Return PDF file with appropriate file path
    return File(pdf.BinaryData, "application/pdf", $"invoice-{id}.pdf");
}
[HttpGet]
public async Task<IActionResult> DownloadInvoice(int id)
{
    // Get invoice data from database
    var model = await GetInvoiceData(id);
    // Render Razor view to HTML string
    var html = await RenderViewToString("Invoice", model);
    // Convert HTML to PDF document
    var renderer = new ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf(html);
    // Return PDF file with appropriate file path
    return File(pdf.BinaryData, "application/pdf", $"invoice-{id}.pdf");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code example shows how ASP.NET Core integrates with IronPDF for generating PDF documents from Razor templates. The page rendered from the view becomes a professional PDF document.

Conclusion

IronPDF transforms generating PDF documents in .NET Core into a simple, efficient process. Its Chrome rendering engine ensures perfect fidelity when generating PDF files from HTML content, while the intuitive API eliminates the learning curve typically associated with PDF manipulation. The PDF library provides good documentation and actively maintained support for ASP.NET Core developers.

Whether you're creating PDFs for commercial use or building enterprise web applications, IronPDF provides the tools needed for professional PDF creation with commercial licensing options available. Developers consistently praise IronPDF's ease of use and rendering quality.

Start your free trial today with licensing options designed for teams of all sizes.

Frequently Asked Questions

How can I generate PDF documents in .NET Core?

You can generate PDF documents in .NET Core using IronPDF, which allows you to create PDFs from HTML, URLs, and Razor views using its advanced Chrome rendering engine.

What are the advantages of using IronPDF for PDF generation?

IronPDF provides several advantages, including ease of integration, support for pixel-perfect rendering, and the ability to create PDFs from various sources like HTML and URLs, making it ideal for building web applications that require PDF generation.

Can IronPDF handle complex PDF generation tasks?

Yes, IronPDF is designed to handle complex PDF generation tasks in the .NET environment, providing developers with the tools needed to create detailed and accurate PDF documents.

What is the role of the Chrome rendering engine in IronPDF?

The Chrome rendering engine in IronPDF ensures that the PDFs generated are pixel-perfect, maintaining the fidelity of the original HTML or web content.

Is IronPDF suitable for generating business documents like invoices and reports?

Absolutely, IronPDF is well-suited for generating business documents such as invoices and reports, offering precise rendering and support for various document formats.

What types of input can IronPDF convert into PDF?

IronPDF can convert inputs such as HTML, URLs, and Razor views into PDF documents, providing flexibility in content creation.

Does IronPDF support ASP.NET Core applications?

Yes, IronPDF is fully compatible with ASP.NET Core applications, allowing developers to seamlessly integrate PDF generation capabilities into their web projects.

What are some common use cases for IronPDF in web applications?

Common use cases for IronPDF include generating PDFs for invoices, reports, and any other business documents required by web applications.

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