public void CreatePDF(Stream stream) { using (var ds = new DataSet()) { // Fetch data: ds.ReadXml(Path.Combine("Resources", "data", "GcNWind.xml")); DataTable dtProds = ds.Tables["Products"]; DataTable dtSupps = ds.Tables["Suppliers"]; var products = from prod in dtProds.Select() join supp in dtSupps.Select() on prod["SupplierID"] equals supp["SupplierID"] orderby prod["ProductName"] select new { 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: var template = File.ReadAllText(Path.Combine("Resources", "Misc", "ProductListTemplate.html")); // Bind the template to data: var builder = new Stubble.Core.Builders.StubbleBuilder(); var boundTemplate = builder.Build().Render(template, new { Query = products }); var tmp = Path.GetTempFileName(); // Render the bound HTML: using (var re = new GcHtmlRenderer(boundTemplate)) { // PdfSettings allow to provide options for HTML to PDF conversion: var pdfSettings = new PdfSettings() { 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); } // Copy the created PDF from the temp file to target stream: using (var ts = File.OpenRead(tmp)) ts.CopyTo(stream); // Clean up: File.Delete(tmp); } // Done. }
public void CreatePDF() { using (var ds = new DataSet()) { // Fetch data: ds.ReadXml(@"C:\Users\barisel\source\repos\IronPdfSample\IronPdfSample\Resources\data\GcNWind.xml"); DataTable dtProds = ds.Tables["Statement"]; var accounting = from prod in dtProds.Select() orderby prod["AccountSuffix"] select new { TranDate = prod["TranDate"], AccountNumber = prod["AccountNumber"], AccountSuffix = prod["AccountSuffix"], CurrencyCode = prod["CurrencyCode"], Explanation = prod["Explanation"], DebitAmount = $"{prod["DebitAmount"]} TRY", CreditAmount = $"{prod["CreditAmount"]} TRY", Balance = $"{prod["Balance"]} TRY", CustomerName = prod["CustomerName"] }; var headerField = new { Name = "BARIŞ ELVANOĞLU", Address = "SULTAN ORHAN MAH.KIŞLA CD. 1136 SK." }; var template = File.ReadAllText(@"C:\Users\barisel\source\repos\IronPdfSample\IronPdfSample\Resources\Misc\ProductListTemplate.html"); var builder = new Stubble.Core.Builders.StubbleBuilder(); var boundTemplate = builder.Build().Render(template, new { Query = accounting, Header = headerField }); var tmp = Path.GetTempFileName(); var Renderer = new HtmlToPdf(); Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4; Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print; Renderer.PrintOptions.CustomCssUrl = "https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"; //Set the width of the resposive virtual browser window in pixels //Renderer.PrintOptions.MarginBottom = 0; Renderer.PrintOptions.Footer = new SimpleHeaderFooter() { RightText = "Sayfa {page}", FontSize = 10 }; var PDF = Renderer.RenderHtmlAsPdf(boundTemplate); Renderer.PrintOptions.PrintHtmlBackgrounds = true; //PDF.WatermarkPage("<h2 style='color:red'>Denizbank kaşe imza</h2>", 1,PdfDocument.WaterMarkLocation.MiddleCenter, 50, -45, "https://www.nuget.org/packages/IronPdf"); var OutputPath = "HtmlToPDF.pdf"; PdfDocument.Merge(new PdfDocument(@"C:\Users\barisel\source\repos\IronPdfSample\IronPdfSample\bin\Debug\netcoreapp3.1\Watermarked.pdf"), PDF).SaveAs("Combined.Pdf"); PDF.SaveAs(OutputPath); //C:\Users\barisel\source\repos\IronPdfSample\IronPdfSample\bin\Debug\netcoreapp3.1 } }
static void Main(string[] args) { var builder = new Stubble.Core.Builders.StubbleBuilder(); builder.Configure(opts => { opts.SetPartialTemplateLoader(new DictionaryLoader( new Dictionary <string, string> { { "Body", BodyTemplate }, } )); }); var renderer = builder.Build(); var data = new Dictionary <string, object> { { "Url", "https://github.com" } }; var output = renderer.Render(BaseTemplate, data); Console.WriteLine(output); }