Generate PDF Reports from HTML Templates in .NET 6

Generate bound PDF reports from HTML templates with mustache syntax using GrapeCity Documents for PDF (GcPdf).

Download Free Trial (v 6.0.5)


Generating documents through a template saves not only time but also avoids possible errors. With GrapeCity Documents for PDF(GcPdf), you can bind HTML Templates with data sources and generate full professional PDF reports, such as Invoices, Paychecks, and much more.

Key Features of GrapeCity Documents for PDF Reports

  1. Automate PDF report generation: Unleash the power of automation by using the .NET 6 GcPdf API to create template documents with the ability to define comprehensive syntax.

  2. Customize PDF documents-add margins, headers and footers: Modify reports, dynamically insert data, images, and tables in your reports; organize the pages by adding headers and footers.

  3. Bind with any data source: GcPdf supports binding HTML templates to various data sources to an HTML template.

  4. Manage pagination of PDF pages: Programmatically generate formatted paginated PDF reports.

  5. Create HTML Templates and convert to PDF: Easily render all HTML templates to content, with a bound data source, to a PDF document.

Establish Invoices

  • Use PDF HTML templates to create invoices
  • Highlight key information for your clients

Create Timesheets

  • Easily create and share timesheet data with employees for sign-off and approval–all while protecting data from erroneous changes
  • Design timesheets for online, digital, or graphical signatures by employees and managers

Design Brochures

  • Create ready-to-use templates to pull data and populate daily sales brochures for sales personnel to stay on top of ever-changing inventory
  • Build printable, on-demand lists of available vehicles so consumers can research and compare vehicle information and pricing

Create and Distribute Customized Letters

  • Create and distribute form letters from data within various systems
  • Pull existing data, populate appropriate HTML fields, and produce PDF outputs for consistent formatting across platforms

Steps to Generate PDF Reports

Step 1: HTML Templates

Create HTML templates and include placeholders for data.

Step 2: Bind HTML Templates with Data

Use the Stubble.Core package and its StubbleBuilder class to bind the template.

// Bind the template to data
var builder = new Stubble.Core.Builders.StubbleBuilder();
var boundTemplate = builder.Build().Render(TemplatePath, new { Query = products });

Step 3: Convert Bound HTML to PDF

Use GcHtmlBrowser and HtmlPage to pass the bound HTML template and render to PDF.

using GrapeCity.Documents.Html;

// Create an instance of GcHtmlBrowser to render HTML:
var path = new BrowserFetcher().GetDownloadedPath();
using var browser = new GcHtmlBrowser(path);

// Render the bound HTML:
using var htmlPage = browser.NewPage(boundTemplate);
// Render the generated HTML to the temporary file:
var tmp = Path.GetTempFileName();

Step 4: Customize a PDF

Margins, header, and footer can all be added to the PDF file while converting the bound HTML template with PdfOptions.

// PdfOptions specifies options for HTML to PDF conversion:
var pdfOptions = new PdfOptions()
    Margins = new PdfMargins(0.2f, 1, 0.2f, 1),
    PreferCSSPageSize = false,
    DisplayHeaderFooter = true,
    HeaderTemplate = "<div style='color:#1a5276; font-size:12px; width:1000px; margin-left:0.2in; margin-right:0.2in'>" +
                    "<span style='float:left;'>Product Price List</span>" +
                    "<span style='float:right'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>" +
    FooterTemplate = "<div style='color: #1a5276; font-size:12em; width:1000px; margin-left:0.2in; margin-right:0.2in;'>" +
                    "<span>(c) GrapeCity, Inc. All Rights Reserved.</span>" +
                    "<span style='float:right'>Generated on <span class='date'></span></span></div>"
// Render the generated HTML to the temporary file:
var tmp = Path.GetTempFileName();
htmlPage.SaveAsPdf(tmp, pdfOptions);

Resources and Demo

Try rendering the HTML template

Render a report with the list of products from the standard NWind sample database using a {{mustache}} HTML template.

"The speed to build what we needed and the level of ease of use of controls are why we chose GrapeCity. It was a perfect fit."

Spiro Skias - CGI