By the end, you should have a good understanding of the different techniques available and how to get started generating PDFs from your ASP.NET Core applications.
The .NET Framework and .NET Core provide some basic built-in functionality for generating PDF documents programmatically without a third-party dependency.
The System.Drawing namespace includes classes like PdfDocument and XGraphics that allow creating PDF files pixel-by-pixel. You can draw text, images, vectors, and more onto a PdfPage and save the result as a PDF file.
// Create a new PdfDocument object var pdfDoc = new PdfDocument(); // Add a page pdfDoc.AddPage(); // Get an XGraphics object for drawing var gfx = XGraphics.FromPdfPage(pdfDoc.Pages[0]); // Draw text gfx.DrawString("Hello World!", font, XBrushes.Black, 0, 0); // Save the document and dispose it pdfDoc.Save("HelloWorld.pdf"); pdfDoc.Dispose();
Enter fullscreen mode
Exit fullscreen mode
This provides a basic building block but has limitations. Text/layout is rendered pixel-by-pixel so it may not scale or print well. There is also no support for things like vector graphics, tables, hyperlinks etc.
For richer PDF generation capabilities, third-party libraries are recommended.
There are a number of high-quality third-party .NET libraries for generating sophisticated PDF documents from code:
iTextSharp is one of the most full-featured and mature open source .NET PDF libraries available. It allows building PDF documents from scratch using elements like paragraphs, tables, images etc. Layout is handled separately from rendering so documents scale well.
// Create a document and open it Document pdfDoc = new Document(PageSize.A4, 25, 25, 25, 25); PdfWriter.GetInstance(pdfDoc, ms); pdfDoc.Open(); // Add content Paragraph p = new Paragraph("Hello World!"); pdfDoc.Add(p); // Close the document pdfDoc.Close();
Enter fullscreen mode
Exit fullscreen mode
PDFSharp is another popular open source .NET library. It uses a page-oriented model and the drawing API is similar to GDI+. This makes it easy to render to PDF from existing GDI/GDI+ code.
// Create a new PDF document PdfDocument pdfDoc = new PdfDocument(); // Add a page PdfPage page = pdfDoc.AddPage(); // Get drawing object and render XGraphics gfx = XGraphics.FromPdfPage(page); gfx.DrawString("Hello World!", font, XBrushes.Black, 0, 0); // Save the document pdfDoc.Save("HelloWorld.pdf");
Enter fullscreen mode
Exit fullscreen mode
wkhtmltopdf is a command line tool that renders HTML/CSS to PDF using the Webkit rendering engine. It can be used from .NET code by launching the process. This allows generating PDFs from dynamic HTML/CSS content.
// Render HTML to a stream var proc = new Process() StartInfo = new ProcessStartInfo FileName = "wkhtmltopdf.exe", Arguments = $"-q - -", RedirectStandardInput = true, RedirectStandardOutput = true > >; proc.Start(); proc.StandardInput.Write(html); proc.StandardInput.Close(); proc.WaitForExit(); // Output stream contains PDF pdf = proc.StandardOutput.BaseStream;
Enter fullscreen mode
Exit fullscreen mode
These are some of the most popular .NET PDF libraries. Each has its strengths - choose based on your specific requirements.
Another common approach is to generate PDF from HTML/CSS content since it's easy to work with. There are a few options for doing this in .NET:
PDFKit is a .NET library that uses the HTML rendering engine WebKit to convert HTML + CSS to PDF documents.
// Create a PDFKit document var pdfDoc = new PdfDocument(); // Add a page and render HTML var page = pdfDoc.AddPage(); page.Render(markup); // Save PDF pdfDoc.Save("HelloWorld.pdf");
Enter fullscreen mode
Exit fullscreen mode
Puppeteer Sharp is a .NET port of the Puppeteer Node.js library for controlling headless Chrome. It can render HTML to PDF using the Chromium rendering engine.
var browser = await Puppeteer.LaunchAsync(options); var page = await browser.NewPageAsync(); await page.GoToAsync(url); await page.PdfAsync(output); await browser.CloseAsync();
Enter fullscreen mode
Exit fullscreen mode
This library uses the HTML Rendering engine in .NET to convert HTML markup to PDF.
var html = @"Hello World!
"; var pdf = HtmlToPdfConverter.ConvertHtmlToPdf(html); File.WriteAllBytes("HelloWorld.pdf", pdf);
Enter fullscreen mode
Exit fullscreen mode
Hence, these options allow rendering dynamic HTML/CSS content to high-fidelity PDF documents from .NET.
There are two main approaches for generating PDFs - on the server or client-side:
Pros:
Cons:
Return HTML/data needed to render PDF
Use client-side library like jsPDF to generate PDF in browser
User downloads/gets link to generated file
Pros:
Cons:
Example Project
To put this into practice, here is a simple ASP.NET Core MVC sample project that generates a PDF on the server:
// Controller action public IActionResult GeneratePdf() // Create document object Document pdfDoc = new Document(PageSize.A4, 25, 25, 25, 25); // Write content WriteParagraph(pdfDoc, "Hello World!"); // Write to memory stream MemoryStream ms = new MemoryStream(); PdfWriter.GetInstance(pdfDoc, ms); pdfDoc.Close(); // Return generated PDF return File(ms.ToArray(), "application/pdf"); > // Helper to add paragraph private static void WriteParagraph(Document pdfDoc, string text) Paragraph p = new Paragraph(text); pdfDoc.Add(p); >
Enter fullscreen mode
Exit fullscreen mode
This demonstrates:
With this approach, the server generates the PDF on request without any client-side processing required.
In this article, we covered several techniques for programmatically generating PDF documents from ASP.NET Core applications. Whether you need a simple solution or full featured generation, .NET provides many high quality options.
The key aspects discussed were:
With the right approach, ASP.NET Core makes developing PDF generation capabilities straightforward. Choose the technique that best fits your specific application requirements.