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.
Create HTML templates and include placeholders for 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 });
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);
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);
Render a report with the list of products from the standard NWind sample database using a {{mustache}} HTML template.