ProductListTemplate.vb
'' 
'' このコードは、DioDocs for PDF のサンプルの一部として提供されています。
'' 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

'' このサンプルは、{{mustache}} HTML テンプレートを使用してレポート
'' (標準の NWind サンプルデータベースの商品リスト)をレンダリングする方法を示します。
'' 
'' データクエリと HTML 形式は、ProductList サンプルで使用されているものと似ています。
'' ただし、そのサンプルとは異なり、ここではリソースからロードされた HTML テンプレート
'' ファイル ProductListTemplate.html を使用し、
'' {{mustache}} を使用してデータにバインドします。変更したテンプレートファイル
'' ({{mustache}} バインディングを保持)を使用することで、レポートの外観を簡単に
'' カスタマイズできます。
'' 
'' このサンプルでは、Stubble.Core パッケージを
'' 使用してデータをテンプレートにバインドします。
'' 
'' GcHtml をプロジェクトに追加する方法の詳細については、HelloWorldHtml サンプル
'' コードの上部にあるコメントのメモを参照してください。
Public Class ProductListTemplate
    Sub CreatePDF(ByVal stream As Stream)
        Using ds = New DataSet()
            '' データを取得します。
            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}"
                }

            '' テンプレートをロードします - {{mustache}}データ参照を含む HTML ファイル。
            Dim template = File.ReadAllText(Path.Combine("Resources", "Misc", "ProductListTemplate.html"))
            '' テンプレートをデータにバインドします。
            Dim builder = New Stubble.Core.Builders.StubbleBuilder()
            '' バインドされたHTMLをレンダリングします。
            Dim boundTemplate = builder.Build().Render(template, New With {.Query = products})
            Dim tmp = Path.GetTempFileName()
            '' HTML のレンダリングに使用する GcHtmlBrowser のインスタンスを生成します。
            Using browser = Util.NewHtmlBrowser()
                '' PdfOptions では、HTML から PDF への変換のオプションを提供できます。
                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>" +
                        "</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>"
                }
                '' 生成された HTML を一時ファイルにレンダリングします。
                Using htmlPage = browser.NewPage(boundTemplate)
                    htmlPage.SaveAsPdf(tmp, pdfOptions)
                End Using
            End Using
            '' 作成した PDF を一時ファイルからターゲットストリームにコピーします。
            Using ts = File.OpenRead(tmp)
                ts.CopyTo(stream)
            End Using
            '' 一時ファイルを削除します。
            File.Delete(tmp)
        End Using
        '' PDF ドキュメントを保存します。
    End Sub
End Class