ProductListTemplate.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Data
Imports System.Linq
Imports System.Reflection
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Html

'' This sample shows how to render a report (the list of products
'' from the standard NWind sample database) using a {{mustache}}
'' HTML template.
''
'' The data query and HTML formatting are similar to those used
'' in the ProductList sample. Unlike that sample though, here
'' we use the HTML template file ProductListTemplate.html
'' loaded from a resource, and bind it to data using {{mustache}}.
'' Changing the template file (preserving the {{mustache}} bindings)
'' can be used to easily customize the look of the report.
''
'' This sample uses the Stubble.Core package to bind data to the template.
''
'' Please see notes in comments at the top of HelloWorldHtml
'' sample code for details on adding GcHtml to your projects.
Public Class ProductListTemplate
    Sub CreatePDF(ByVal stream As Stream)
        Using ds = New DataSet()
            '' Fetch data:
            ds.ReadXml(Path.Combine("Resources", "data", "GcNWind.xml"))

            Dim dtProds = ds.Tables("Products")
            Dim dtSupps = ds.Tables("Suppliers")

            Dim products =
                From prod In dtProds.Select()
                Join supp In dtSupps.Select()
                On prod("SupplierID") Equals supp("SupplierID")
                Order By prod("ProductName")
                Select New With {
                    .ProductID = prod("ProductID"),
                    .ProductName = prod("ProductName"),
                    .Supplier = supp("CompanyName"),
                    .QuantityPerUnit = prod("QuantityPerUnit"),
                    .UnitPrice = $"{prod("UnitPrice"):C}"
                }

            '' Load the template - HTML file with {{mustache}} data references:
            Dim template = File.ReadAllText(Path.Combine("Resources", "Misc", "ProductListTemplate.html"))
            '' Bind the template to data:
            Dim builder = New Stubble.Core.Builders.StubbleBuilder()
            Dim boundTemplate = builder.Build().Render(template, New With {.Query = products})
            Dim tmp = Path.GetTempFileName()
            '' Render the bound HTML:
            Using re = New GcHtmlRenderer(boundTemplate)
                '' PdfSettings allow to provide options for HTML to PDF conversion:
                Dim pdfSettings = New PdfSettings() With
                {
                    .Margins = New Margins(0.2F, 1, 0.2F, 1),
                    .IgnoreCSSPageSize = True,
                    .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:
                re.RenderToPdf(tmp, pdfSettings)
            End Using
            '' Copy the created PDF from the temp file to target stream
            Using ts = File.OpenRead(tmp)
                ts.CopyTo(stream)
            End Using
            '' Clean up:
            File.Delete(tmp)
        End Using
        '' Done.
    End Sub
End Class