In many real-world apps, we often need to make PDF reports from database data — like employee details, invoices, or sales summaries.
Creating these PDFs by hand can be time-consuming and tricky, especially with design tools or heavy libraries.
In this tutorial, I’ll show how you can use C#, SQL Server, and IronPDF to quickly turn an HTML table into a polished PDF report.
This guide is perfect for .NET beginners who already know basic C#/SQL and want an easy, practical way to add PDF generation in their projects.
Tools & Setup
.NET Version: .NET 8 or .NET 10 (we’ll use .NET 10 for this example).
Database: SQL Server (any edition — local, Express, or cloud).
IDE: Visual Studio 2022 or Visual Studio Code.
NuGet Package: IronPDF (for creating PDFs easily in C#).
Basic Knowledge: Some understanding of C# and SQL commands.
To install IronPDF, open the Package Manager Console in Visual Studio and run:
Install-Package IronPdf
Setting up a simple demo table
To keep things easy, we’ll create a small Employees table in SQL Server and fill it with some sample data. This table will hold a few columns like name, department, and salary — enough to build our PDF report later.
`CREATE TABLE Employees (
Id INT IDENTITY(1,1) PRIMARY KEY,
FullName NVARCHAR(100) NOT NULL,
Department NVARCHAR(50) NOT NULL,
Salary DECIMAL(10, 2) NOT NULL
);`
**Add a few demo records using these INSERT statements:
**
`INSERT INTO Employees (FullName, Department, Salary)
VALUES
('Ali Khan', 'IT', 120000),
('Sara Ahmed', 'HR', 95000),
('Usman Tariq', 'Finance', 110000),
('Zara Malik', 'Marketing', 90000);`
You can run these SQL commands in SQL Server Management Studio (SSMS) or any SQL tool you prefer.
Reading Employees from SQL Server in C
Next, we’ll load the Employees data from SQL Server into a C# list. This will give us a strongly-typed list that we can later use to build our HTML table and PDF. To keep things simple, we’ll use ADO.NET with SqlConnection and SqlCommand, but you could also use EF Core if you prefer.
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
public class Employee
{
public int Id { get; set; }
public string FullName { get; set; } = string.Empty;
public string Department { get; set; } = string.Empty;
public decimal Salary { get; set; }
}
public List<Employee> GetEmployees()
{
var employees = new List<Employee>();
// TODO: apna server name / instance yahan dalen
var connectionString = @"Server=CYBERSPACE\SQLEXPRESS;Database=Employee_DB;Trusted_Connection=True;";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(
"SELECT Id, FullName, Department, Salary FROM Employees",
connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
employees.Add(new Employee
{
Id = reader.GetInt32(0),
FullName = reader.GetString(1),
Department = reader.GetString(2),
Salary = reader.GetDecimal(3)
});
}
}
}
return employees;
}
This method opens a SQL connection, reads all rows from the Employees table, and returns them as a C# List that we’ll use in the next step to build our HTML.
Generating an HTML table for the report
Now that we have a list of employees in C#, we can turn it into a simple HTML table string. IronPDF will later convert this HTML into a PDF file, so we just need a clean, readable HTML layout. A basic table with some light styling is more than enough for a nice-looking report.
public string BuildEmployeesHtml(List<Employee> employees)
{
var html = @"
<html>
<head>
<meta charset='UTF-8'>
<title>Employee Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { text-align: center; }
p.generated { text-align: center; color: #666; font-size: 12px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
tr:nth-child(even) { background-color: #fafafa; }
</style>
</head>
<body>
<h1>Employee Report</h1>
<p class='generated'>Generated on " + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + @"</p>
<table>
<thead>
<tr>
<th>Id</th>
<th>Full Name</th>
<th>Department</th>
<th>Salary</th>
</tr>
</thead>
<tbody>";
foreach (var emp in employees)
{
html += $@"
<tr>
<td>{emp.Id}</td>
<td>{emp.FullName}</td>
<td>{emp.Department}</td>
<td>{emp.Salary:N0}</td>
</tr>";
}
html += @"
</tbody>
</table>
</body>
</html>";
return html;
}
This method builds a complete HTML page with a styled table that displays all employees from our list in a clean, report-friendly layout.
Converting the HTML to a PDF with IronPDF
Now we’ll take the HTML string we built and turn it into a real PDF file using IronPDF. IronPDF’s ChromePdfRenderer renders HTML almost like a modern browser, so your CSS styles and table layout will look clean and professional in the PDF. All we need to do is pass our HTML string to the renderer and save the result to disk.
using System;
using IronPdf;
public void GenerateEmployeeReportPdf()
{
// 1. Get data from SQL
var employees = GetEmployees();
// 2. Build HTML string
var html = BuildEmployeesHtml(employees);
// 3. Create a PDF from HTML
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(html);
// 4. Save the PDF file
var outputPath = @"C:\Reports\EmployeeReport.pdf";
pdfDocument.SaveAs(outputPath);
Console.WriteLine($"PDF report generated at: {outputPath}");
}
This method reuses the existing GetEmployees and BuildEmployeesHtml methods, then uses IronPDF’s ChromePdfRenderer to render the HTML into a PDF and save it as EmployeeReport.pdf on your machine.
Putting It All Together – The Main Method
Now let’s create a simple Main method that runs everything in order. This is what you would call in your console app to generate the employee PDF report from SQL Server. It will handle the full flow: read data, build HTML, and create the PDF.
static void Main()
{
try
{
Console.WriteLine("Starting Employee Report Generation...");
// Call the method that does the full PDF generation flow
var generator = new Program(); // if methods are instance-based
generator.GenerateEmployeeReportPdf(); // or just GenerateEmployeeReportPdf() if static
Console.WriteLine("Report generated successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
When you run this program, it will:
Connect to SQL Server and fetch all employees into a C# list.
Build an HTML table from that list.
Use IronPDF to convert the HTML into a PDF file.
Save the PDF to the path you configured (for example, C:\Reports\EmployeeReport.pdf).
In this tutorial, we walked through a simple, end-to-end flow for generating a PDF report from SQL Server data using C#, HTML, and IronPDF. We created a small Employees table, loaded the data in C#, turned it into an HTML table, and finally rendered that HTML into a polished PDF file.
This approach is easy to extend to real-world scenarios like invoices, salary slips, or admin dashboards, because you only need to adjust your SQL query and HTML layout. If you want to explore more advanced features like headers/footers, page breaks, editing existing PDFs, or rendering full Razor views, you can check the official IronPDF documentation and examples on their website:
.



Top comments (0)