'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS 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 DsHtml to your projects.
Public Class ProductListTemplate
    Sub CreatePDF(ByVal stream As Stream)
        Using ds = New DataSet()
            '' Fetch data:
            ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.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()
            '' Render the bound HTML:
            Dim boundTemplate = builder.Build().Render(template, New With {.Query = products})
            Dim tmp = Path.GetTempFileName()
            '' Create an instance of GcHtmlBrowser that is used to render HTML:
            Using browser = Util.NewHtmlBrowser()
                '' PdfSettings specifies options for HTML to PDF conversion:
                Dim pdfOptions = New PdfOptions() With {
                    .Margins = New PdfMargins(0.2F, 1, 0.2F, 1),
                    .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) MESCIUS 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:
                Using htmlPage = browser.NewPage(boundTemplate)
                    htmlPage.SaveAsPdf(tmp, pdfOptions)
                End Using
            End Using
            '' Copy the created PDF from the temp file to target stream
            Using ts = File.OpenRead(tmp)
            End Using
            '' Clean up:
        End Using
        '' Done.
    End Sub
End Class