ProductListTemplate.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Collections.Generic
  8. Imports System.Data
  9. Imports System.Linq
  10. Imports System.Reflection
  11. Imports GrapeCity.Documents.Pdf
  12. Imports GrapeCity.Documents.Html
  13.  
  14. '' This sample shows how to render a report (the list of products
  15. '' from the standard NWind sample database) using a {{mustache}}
  16. '' HTML template.
  17. ''
  18. '' The data query and HTML formatting are similar to those used
  19. '' in the ProductList sample. Unlike that sample though, here
  20. '' we use the HTML template file ProductListTemplate.html
  21. '' loaded from a resource, and bind it to data using {{mustache}}.
  22. '' Changing the template file (preserving the {{mustache}} bindings)
  23. '' can be used to easily customize the look of the report.
  24. ''
  25. '' This sample uses the Stubble.Core package to bind data to the template.
  26. ''
  27. '' Please see notes in comments at the top of HelloWorldHtml
  28. '' sample code for details on adding DsHtml to your projects.
  29. Public Class ProductListTemplate
  30. Sub CreatePDF(ByVal stream As Stream)
  31. Using ds = New DataSet()
  32. '' Fetch data:
  33. ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"))
  34.  
  35. Dim dtProds = ds.Tables("Products")
  36. Dim dtSupps = ds.Tables("Suppliers")
  37.  
  38. Dim products =
  39. From prod In dtProds.Select()
  40. Join supp In dtSupps.Select()
  41. On prod("SupplierID") Equals supp("SupplierID")
  42. Order By prod("ProductName")
  43. Select New With {
  44. .ProductID = prod("ProductID"),
  45. .ProductName = prod("ProductName"),
  46. .Supplier = supp("CompanyName"),
  47. .QuantityPerUnit = prod("QuantityPerUnit"),
  48. .UnitPrice = $"{prod("UnitPrice"):C}"
  49. }
  50.  
  51. '' Load the template - HTML file with {{mustache}} data references:
  52. Dim template = File.ReadAllText(Path.Combine("Resources", "Misc", "ProductListTemplate.html"))
  53. '' Bind the template to data:
  54. Dim builder = New Stubble.Core.Builders.StubbleBuilder()
  55. '' Render the bound HTML:
  56. Dim boundTemplate = builder.Build().Render(template, New With {.Query = products})
  57. Dim tmp = Path.GetTempFileName()
  58. '' Create an instance of GcHtmlBrowser that is used to render HTML:
  59. Using browser = Util.NewHtmlBrowser()
  60. '' PdfSettings specifies options for HTML to PDF conversion:
  61. Dim pdfOptions = New PdfOptions() With {
  62. .Margins = New PdfMargins(0.2F, 1, 0.2F, 1),
  63. .DisplayHeaderFooter = True,
  64. .HeaderTemplate = "<div style='color:#1a5276 font-size:12px width:1000px margin-left:0.2in margin-right:0.2in'>" +
  65. "<span style='float:left'>Product Price List</span>" +
  66. "<span style='float:right'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>" +
  67. "</div>",
  68. .FooterTemplate = "<div style='color: #1a5276 font-size:12em width:1000px margin-left:0.2in margin-right:0.2in'>" +
  69. "<span>(c) MESCIUS inc. All Rights Reserved.</span>" +
  70. "<span style='float:right'>Generated on <span class='date'></span></span></div>"
  71. }
  72. '' Render the generated HTML to the temporary file:
  73. Using htmlPage = browser.NewPage(boundTemplate)
  74. htmlPage.SaveAsPdf(tmp, pdfOptions)
  75. End Using
  76. End Using
  77. '' Copy the created PDF from the temp file to target stream
  78. Using ts = File.OpenRead(tmp)
  79. ts.CopyTo(stream)
  80. End Using
  81. '' Clean up:
  82. File.Delete(tmp)
  83. End Using
  84. '' Done.
  85. End Sub
  86. End Class
  87.