Skip to main content Skip to footer

Generate PDF Reports from HTML Templates in .NET 6+

Generate bound PDF reports from HTML templates with mustache syntax using Document Solutions for PDF (DsPdf).

Download Free Trial (v 7.1.1)

Overview

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

Key Features of Document Solutions for PDF Reports

Generate PDF Reports from HTML Templates in .NET 6+
  1. Automate PDF report generation: Unleash the power of automation by using the .NET 6+ DsPdf 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: DsPdf 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.
Use a .NET PDF API and HTML templates to easily create invoices

Establish Invoices

  • Use PDF HTML templates to create invoices
  • Highlight key information for your clients
Design timesheets for online, digital, or graphical signatures by employees and managers within a PDF document

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
Create ready-to-use HTML templates to pull data and populate into a PDF in .NET applications

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 in .NET apps

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 from HTML Templates

Create HTML templates and include placeholders for data Example

Step 1: HTML Templates

Create HTML templates and include placeholders for data.

Programmatically Bind HTML Templates with Data using a .NET PDF API

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 });
Convert Bound HTML to PDF using C#/VB.NET

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();
htmlPage.SaveAsPdf(tmp);
Customize the PDF Page when programmatically generating templates from HTML

Step 4: Customize the 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>" +
                    "</div>",
    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

Try rendering the HTML template

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

Read the blog Try the demo View Documentation

Trusted by the World's Leading Companies

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

Spiro Skias - CGI