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 8.0.0)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
- 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.
- 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.
- Bind with any data source: DsPdf supports binding HTML templates to various data sources to an HTML template.
- Manage pagination of PDF pages: Programmatically generate formatted paginated PDF reports.
- Create HTML Templates and convert to PDF: Easily render all HTML templates to content, with a bound data source, to a PDF document.
Steps to Generate PDF Reports from HTML Templates
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(); htmlPage.SaveAsPdf(tmp);
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
Render a report with the list of products from the standard NWind sample database using a {{mustache}} HTML template.