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