Skip to footer content
USING IRONPDF

Creating a .NET Core PDF Generator with IronPDF

What Makes a Reliable .NET Core PDF Generator?

Building PDF documents in .NET Core applications requires a PDF library that handles HTML content, maintains formatting, and supports cross-platform deployment. Honestly, if you're developing ASP.NET Core web APIs or console applications, a robust .NET Core PDF generator streamlines the whole process of creating documents from various sources. It's a massive time-saver.

Start your free trial and discover why developers choose IronPDF for mission-critical PDF generation in production environments.

IronPDF stands out as a comprehensive .NET Core PDF library. It uses a Chrome rendering engine to create PDF documents with pixel-perfect accuracy. This approach means you don't need to learn complex PDF APIs or struggle with layout issues; you can just leverage your existing HTML and CSS skills to generate PDF files. The library’s extensive documentation and code examples make implementation straightforward.

How Does IronPDF Simplify Generating PDF Documents in .NET Core?

IronPDF transforms the traditionally complex task of PDF generation into straightforward code that any .NET developer can implement. The library uses the ChromePdfRenderer class to convert HTML strings, files, or URLs directly into PDF format. This fluent API approach provides extensive customization options while maintaining high performance across different platforms.

The real power lies in how IronPDF handles converting HTML content into professional PDF files. Instead of manually positioning or needing to draw elements, developers write standard HTML with CSS styling, and the library handles the conversion seamlessly. The resulting PDF files are not mere images of text; they are fully-featured documents where users can select and search for text.

Past basic PDF generation, you can use IronPDF's advanced editing tools to edit PDF documents. With these, you can merge documents, add watermarks, annotations, and more. Check out the related tutorial to see more example source code for these tools.

Installing IronPDF via the NuGet Package Manager

Getting started with IronPDF in Visual Studio requires just one NuGet package installation. Open the NuGet Package Manager Console, ensure your project name is selected in the 'Default project' dropdown, and run the following command:

Install-Package IronPdf
Install-Package IronPdf
SHELL

This single NuGet package provides all the functionality needed to create, edit, and generate PDF files in your .NET Core applications. The installation automatically configures your project for PDF generation across Windows, Linux, and Docker environments. It also offers support for various .NET versions including .NET Framework 4.6.2+, .NET Core 3.1+, and .NET Standard 2.0+.

Creating Your First PDF Document from HTML

Let's create PDF documents using a practical invoice document example. This demonstrates how to generate PDF files from HTML content with proper formatting and data binding:

using IronPdf;
using System.IO;
using System.Text;
// Initialize the Chrome renderer
var renderer = new ChromePdfRenderer();
// Configure rendering options
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
// Create HTML content for invoice
var htmlBuilder = new StringBuilder();
htmlBuilder.Append(@"
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; font-size: 14px; }
            .invoice-header { background: #f0f0f0; padding: 20px; }
            table { width: 100%; border-collapse: collapse; }
            th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
        </style>
    </head>
    <body>
        <div class='invoice-header'>
            <h1>Invoice #INV-2024-001</h1>
            <p>Date: " + DateTime.Now.ToString("MM/dd/yyyy") + @"</p>
        </div>
        <table>
            <tr><th>Item</th><th>Quantity</th><th>Price</th></tr>");
// Example of dynamically adding table rows with a for loop
for (int i = 0; i < 3; i++)
{
    htmlBuilder.Append($"<tr><td>Product #{i + 1}</td><td>{i + 1}</td><td>$25.00</td></tr>");
}
htmlBuilder.Append(@"
        </table>
        <p><strong>This is a new paragraph with a summary.</strong></p>
    </body>
    </html>");
// Generate PDF from HTML string
PdfDocument pdfObject = renderer.RenderHtmlAsPdf(htmlBuilder.ToString());
// Save the PDF file
pdfObject.SaveAs("invoice.pdf");
using IronPdf;
using System.IO;
using System.Text;
// Initialize the Chrome renderer
var renderer = new ChromePdfRenderer();
// Configure rendering options
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
// Create HTML content for invoice
var htmlBuilder = new StringBuilder();
htmlBuilder.Append(@"
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; font-size: 14px; }
            .invoice-header { background: #f0f0f0; padding: 20px; }
            table { width: 100%; border-collapse: collapse; }
            th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
        </style>
    </head>
    <body>
        <div class='invoice-header'>
            <h1>Invoice #INV-2024-001</h1>
            <p>Date: " + DateTime.Now.ToString("MM/dd/yyyy") + @"</p>
        </div>
        <table>
            <tr><th>Item</th><th>Quantity</th><th>Price</th></tr>");
// Example of dynamically adding table rows with a for loop
for (int i = 0; i < 3; i++)
{
    htmlBuilder.Append($"<tr><td>Product #{i + 1}</td><td>{i + 1}</td><td>$25.00</td></tr>");
}
htmlBuilder.Append(@"
        </table>
        <p><strong>This is a new paragraph with a summary.</strong></p>
    </body>
    </html>");
// Generate PDF from HTML string
PdfDocument pdfObject = renderer.RenderHtmlAsPdf(htmlBuilder.ToString());
// Save the PDF file
pdfObject.SaveAs("invoice.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code creates a professional invoice document by combining HTML markup with dynamic data. Note how we added a custom font size in the CSS and dynamically generated table rows using a for loop. We also included a new paragraph element (<p>). The RenderHtmlAsPdf method returns a PdfDocument object, which gives you full control over the generated file. For more advanced HTML to PDF scenarios, explore the HTML to PDF tutorial.

Output

The below screenshot shows our example invoice perfectly rendered into a PDF document format.

Creating a .NET Core PDF Generator with IronPDF: Image 1 - Example output for HTML to PDF

Generate PDF Files from URLs and Web Pages

IronPDF excels at converting existing web pages into PDF files. This capability proves invaluable when generating PDF documents from reporting dashboards or web-based forms:

// Create a new ChromePdfRenderer instance
var renderer = new ChromePdfRenderer();
// Set custom page size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
renderer.RenderingOptions.WaitFor.RenderDelay(1000);
// Convert a URL to PDF
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
// Save to file path
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "webpage.pdf");
pdfDocument.SaveAs(filePath);
// Create a new ChromePdfRenderer instance
var renderer = new ChromePdfRenderer();
// Set custom page size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
renderer.RenderingOptions.WaitFor.RenderDelay(1000);
// Convert a URL to PDF
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
// Save to file path
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "webpage.pdf");
pdfDocument.SaveAs(filePath);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The library handles JavaScript execution, loads external resources like images and stylesheets, and maintains responsive layout during conversion. This makes it perfect for creating reports from existing web applications. Learn more about converting URLs to PDF in the detailed guide.

Creating a .NET Core PDF Generator with IronPDF: Image 2 - URL to PDF example output

Advanced PDF Features for Complex Reports

Professional PDF documents often require additional elements beyond basic content. IronPDF provides methods to enhance your PDF documents with headers, footers, and watermarks. The headers and footers API offers complete control over document presentation:

// Create renderer with advanced options
var renderer = new ChromePdfRenderer();
// Add headers and footers
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    MaxHeight = 25,
    HtmlFragment = "<div style='text-align:center'>Company Report</div>"
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
    MaxHeight = 25,
    HtmlFragment = "<div>Page {page} of {total-pages}</div>"
};
// Generate PDF with form fields
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
string formHtml = @"
    <form>
        <label>Name:</label>
        <input type='text' name='name'/>
        <label>Email:</label>
        <input type='email' name='email'/>
        <button type='submit'>Submit</button>
    </form>";
PdfDocument formDocument = renderer.RenderHtmlAsPdf(formHtml);
// Create renderer with advanced options
var renderer = new ChromePdfRenderer();
// Add headers and footers
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    MaxHeight = 25,
    HtmlFragment = "<div style='text-align:center'>Company Report</div>"
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
    MaxHeight = 25,
    HtmlFragment = "<div>Page {page} of {total-pages}</div>"
};
// Generate PDF with form fields
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
string formHtml = @"
    <form>
        <label>Name:</label>
        <input type='text' name='name'/>
        <label>Email:</label>
        <input type='email' name='email'/>
        <button type='submit'>Submit</button>
    </form>";
PdfDocument formDocument = renderer.RenderHtmlAsPdf(formHtml);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This example demonstrates how to add consistent headers across all pages and create interactive form fields within the PDF document. The system automatically handles page numbering and form field rendering.

Creating a .NET Core PDF Generator with IronPDF: Image 3 - Form PDF example output

Optimizing Performance with Async Operations in ASP.NET Core

For web applications handling multiple PDF generation requests, async operations improve responsiveness:

public async Task<byte[]> GeneratePdfAsync(string htmlContent)
{
    var renderer = new ChromePdfRenderer();
    // Configure for optimal performance
    renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
    // Generate PDF asynchronously
    PdfDocument PDF = await renderer.RenderHtmlAsPdfAsync(htmlContent);
    // Return as byte array for API responses
    return PDF.BinaryData;
}
// Usage in ASP.NET Core controller
[HttpPost]
public async Task<IActionResult> CreateInvoice([FromBody] InvoiceData data)
{
    string HTML = BuildInvoiceHtml(data);
    byte[] pdfBytes = await GeneratePdfAsync(HTML);
    return File(pdfBytes, "application/pdf", "invoice.pdf");
}
public async Task<byte[]> GeneratePdfAsync(string htmlContent)
{
    var renderer = new ChromePdfRenderer();
    // Configure for optimal performance
    renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
    // Generate PDF asynchronously
    PdfDocument PDF = await renderer.RenderHtmlAsPdfAsync(htmlContent);
    // Return as byte array for API responses
    return PDF.BinaryData;
}
// Usage in ASP.NET Core controller
[HttpPost]
public async Task<IActionResult> CreateInvoice([FromBody] InvoiceData data)
{
    string HTML = BuildInvoiceHtml(data);
    byte[] pdfBytes = await GeneratePdfAsync(HTML);
    return File(pdfBytes, "application/pdf", "invoice.pdf");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This pattern allows ASP.NET Core applications to generate PDF files efficiently without blocking threads, a massive improvement over older web technologies like classic ASP where file generation was often a cumbersome process. The byte array output works perfectly for API endpoints that need to return files directly to clients.

Notice how the File() method returns the PDF with the correct application/pdf content type, ensuring browsers handle the response correctly. When working with large PDF documents or multiple concurrent requests, this async approach maintains optimal system performance. For more insights on async patterns, consult the official ASP.NET Core documentation.

Deployment Considerations

IronPDF supports deployment across various environments. For Docker containers, ensure you include the necessary dependencies in your Dockerfile as outlined in the Docker deployment guide. The library works seamlessly on Windows Server, Linux distributions, and cloud platforms like Azure and AWS. Each environment may require specific configuration for fonts and rendering, but the core API remains consistent. The Microsoft documentation on .NET Core deployment provides additional best practices for production environments.

Start Building Your PDF Generator Today

IronPDF transforms PDF generation in .NET Core from a complex challenge into a straightforward implementation. With support for HTML content, rich set of features, and consistent cross-platform behavior, it's the ideal choice for developers who need to generate PDF documents reliably.

Ready to implement your own PDF document generator? Start with a free trial to explore all features without limitations. The documentation provides extensive examples and guides to help you create professional PDF files that meet your exact requirements. Whether you're building invoice systems, generating complex reports, or converting existing web content, IronPDF provides the tools to deliver pixel-perfect results.

For production deployments, explore licensing options that fit your project scale. The investment in a quality PDF library pays dividends through reduced development time and consistent, professional output across all your .NET applications.

Frequently Asked Questions

What is the primary function of IronPDF in .NET Core?

IronPDF is primarily used to convert HTML to PDF in .NET Core applications, enabling developers to create invoices, reports, and other documents with pixel-perfect rendering.

How does IronPDF ensure pixel-perfect rendering?

IronPDF ensures pixel-perfect rendering by using advanced rendering techniques that accurately convert HTML, CSS, and JavaScript into high-quality PDF documents.

Can IronPDF be used to generate reports in .NET Core?

Yes, IronPDF is capable of generating detailed reports in .NET Core by converting HTML-based report templates into professional-grade PDF documents.

Is it possible to convert web pages to PDF using IronPDF?

Absolutely, IronPDF can convert entire web pages to PDF, preserving the layout and style specified in the original HTML and CSS.

What are some common use cases for IronPDF?

Common use cases for IronPDF include generating invoices, creating business reports, converting HTML forms to PDF, and archiving web content.

Does IronPDF support .NET Core applications?

Yes, IronPDF fully supports .NET Core applications, making it a versatile choice for developers working across different .NET platforms.

How does IronPDF handle CSS and JavaScript in HTML to PDF conversion?

IronPDF processes CSS and JavaScript during the conversion process to ensure that the visual layout and dynamic content of the HTML are accurately represented in the PDF.

Can IronPDF generate PDFs from HTML strings?

Yes, IronPDF can generate PDFs from HTML strings, allowing developers to dynamically create PDF documents from HTML content generated within their applications.

Is it possible to customize the appearance of PDFs with IronPDF?

IronPDF provides extensive customization options, allowing developers to control the appearance of PDFs by specifying custom headers, footers, and styles using HTML and CSS.

What advantages does IronPDF offer over other .NET PDF libraries?

IronPDF offers several advantages, including ease of integration with .NET Core, high-quality rendering, support for complex document layouts, and robust handling of HTML, CSS, and JavaScript.

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