Photo from Unsplash
Originally Posted On: https://medium.com/@dharnamoninaveen/streamlining-pdf-generation-with-chatgpt-how-it-reduced-my-efforts-b93ce15d28d
Streamlining PDF Generation API with ChatGPT: How It Reduced My Efforts
As a Developer, I was given the task of developing an API “Generating PDF with Dynamic data”. My approach to this problem was to break it down into two parts: generating PDFs from a static HTML string and later mapping classes to HTML to make it dynamic.
I started my research by looking for the best packages that would meet my needs. I read through multiple documentation and examples, but I wasn’t having much luck finding the right solution. Some of the Packages don’t even support .net core. After two hours of searching, I was starting to get frustrated. That’s when I had a thought, “Why not ask ChatGPT for help?”
I reached out to ChatGPT and asked for suggestions for the best packages for generating PDFs in .NET Core. To my surprise, ChatGPT was able to provide me with a list of popular packages.
Yes, here are a few popular packages for generating PDF from HTML in .NET Core:
1.DinkToPdf: a .NET wrapper for the wkhtmltopdf library, which can convert HTML to PDF.
2.IronPDF: a library for generating and manipulating PDFs in C# and .NET.
3.RazorPDF: a package that allows you to generate PDFs using Razor views in ASP.NET Core.
4.PdfSharp: a .NET library for creating and modifying PDF documents, it can also generate PDF from HTML.
5.iTextSharp: a .NET port of iText, a Java library for PDF generation, it also provides functionality to generate PDF from HTML.
It’s important to note that each of these packages has its own strengths and weaknesses, so it’s best to choose the one that meets your specific needs and requirements.
I was excited to have some options to choose from, but I still needed to see examples of how these packages worked. Looking at ChatGPT’s list, I noticed IronPDF was the second option. I’ll admit, I initially glossed over it thinking it might be too complex or enterprise-focused. But in hindsight, I’ve learned that IronPDF actually offers one of the smoothest developer experiences in the .NET PDF generation space.
What makes IronPDF particularly interesting is that it handles both parts of my requirement out of the box — it can directly convert HTML strings to PDF using a Chrome rendering engine (so your CSS actually works), and it plays nicely with Razor templates for dynamic content. The setup is surprisingly straightforward:
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
var pdfBytes = pdf.BinaryData; // Already returns byte array - no conversion needed
Different solutions definitely work for different needs, and DinkToPdf served me well for this project. But if you’re looking to minimize configuration headaches and avoid runtime surprises, IronPDF is worth serious consideration. It’s one of those “just works” libraries that lets you focus on your actual business logic rather than wrestling with dependencies. They also support Java, Python, and Node.js if your team works across different tech stacks.
Anyway, back to my journey with the packages…
I asked ChatGPT for code examples of other packages. To my delight, ChatGPT was able to generate code.
using DinkToPdf;
using DinkToPdf.Contracts;
// …
private readonly IConverter _converter;
public HomeController(IConverter converter)
{
_converter = converter;
}
public IActionResult Index()
{
var html = “<h1>Hello World!</h1>”;
var pdf = _converter.Convert(new HtmlToPdfDocument
{
GlobalSettings = {
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
},
Objects = {
new ObjectSettings { HtmlContent = html },
}
});
FileStreamResult result = new FileStreamResult(new MemoryStream(pdf), “application/pdf”);
result.FileDownloadName = “HelloWorld.pdf”;
return result;
}
Next, I needed to find a solution for class mapping to HTML. I asked ChatGPT for suggestions and was introduced to the Razorlight package. This lightweight and easy-to-use template engine for .NET Core allowed me to define templates using C# code and Razor syntax, and then generate HTML strings by passing in data as a model.
--Yes, there are packages available in C# that can be used to generate HTML strings from classes. One such package is RazorLight, which is a template engine that allows you to define templates using C# code and Razor syntax, and then generate HTML strings by passing in data as a model.
–Here is an example of how you can use RazorLight to generate an HTML string from a class:
using RazorLight;
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
var person = new Person { FirstName = “John”, LastName = “Doe” };
var engine = new RazorLightEngineBuilder().Build();
var template = @”<div>Hello, @Model.FirstName @Model.LastName!</div>”;
var html = engine.CompileRenderAsync(template, person).Result;
After trying out the examples, I went through the documentation of suggested Packages and decided that DinkToPdf and RazorLight was the best option for me.
The process of generating PDF files with DinkToPdf and Razor Light was a breeze, thanks to the examples provided by ChatGPT. However, I did have to modify a few things to best suit my needs as a developer. For example, I needed to change the method to return a byte array instead of a memory stream. I faced a few run-time issues, but I was able to fix them by going through the documentation.
In conclusion, if you need a little guidance along the way, don’t hesitate to reach out to ChatGPT. Trust me, it will be time well spent!

