public HeaderSettingsInstance(ObjectInstance prototype, HeaderSettings headerSettings) : this(prototype) { if (headerSettings == null) { throw new ArgumentNullException("headerSettings"); } m_headerSettings = headerSettings; }
public Task <byte[]> GeneratePdfAsync(string html) { var globalSettings = new GlobalSettings { ColorMode = ColorMode.Color, Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, Margins = new MarginSettings { Top = 25, Bottom = 25 } }; var webSettings = new WebSettings { DefaultEncoding = "utf-8" }; var headerSettings = new HeaderSettings { FontSize = 15, FontName = "Arial", Right = "Page [page] of [toPage]", Line = true }; var footerSettings = new FooterSettings { FontSize = 12, FontName = "Ariel", Center = "This is for demonstration purposes only.", Line = true }; var objectSettings = new ObjectSettings { PagesCount = true, HtmlContent = html, HeaderSettings = headerSettings, FooterSettings = footerSettings, WebSettings = webSettings }; var htmlToPdfDocument = new HtmlToPdfDocument() { GlobalSettings = globalSettings, Objects = { objectSettings }, }; return(Task.Run(() => _converter.Convert(htmlToPdfDocument))); }
private void RegisterControllerDependencies(ContainerBuilder builder) { var recognitionLanguagesSettings = new HeaderSettings { AcceptableValuesForHeader = new Dictionary <string, string[]> { { "Target-Language", new[] { "ukr", "rus", "eng" } } } }; builder.Register(h => recognitionLanguagesSettings) .As <IHeaderSettings>() .InstancePerLifetimeScope(); }
private void ConvertHTMLToPDF(string html) { Log("Converting HTML to PDF"); Log("Ignore errors like 'Qt: Could not initialize OLE (error 80010106)'", LogLevel.Warning); var converter = new BasicConverter(new PdfTools()); var output = _options.Output; if (output == null) { output = Path.Combine(Directory.GetCurrentDirectory(), "export.pdf"); } //somehow the options HeaderSettings.Left/Right/Center don't work in combination with HeaderSettings.HtmlURL var headerSettings = new HeaderSettings { FontSize = 9, Line = !_options.HideHeaderLine, Spacing = 2.812, }; if (string.IsNullOrEmpty(_options.HeaderUrl)) { headerSettings.Left = _options.HeaderLeft; headerSettings.Center = _options.HeaderCenter; headerSettings.Right = _options.HeaderRight; } else { headerSettings.HtmUrl = _options.HeaderUrl; } var footerSettings = new FooterSettings { Line = !_options.HideFooterLine }; if (string.IsNullOrEmpty(_options.FooterUrl)) { footerSettings.Left = _options.FooterLeft; footerSettings.Center = _options.FooterCenter; footerSettings.Right = _options.FooterRight; } else { footerSettings.HtmUrl = _options.FooterUrl; } var doc = new HtmlToPdfDocument() { GlobalSettings = { ColorMode = ColorMode.Color, Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, Out = output, }, Objects = { new ObjectSettings() { PagesCount = true, HtmlContent = html, WebSettings = { DefaultEncoding = "utf-8" }, HeaderSettings = headerSettings, FooterSettings = footerSettings, UseLocalLinks = true } } }; converter.Convert(doc); Log($"PDF created at: {output}"); }
public byte[] GeneratePdfReport(int restaurantId) { var reservationsTableHtml = this.GenerateReservationsTableHtml(restaurantId); var restaurant = this.RestaurantRepository.GetRestaurant(restaurantId); var html = $@" <!DOCTYPE html> <html lang=""en""> <head> <h3>{restaurant.Title}</h3> <style> table {{ font - family: arial, sans-serif; border-collapse: collapse; width: 100%; }} td, th {{ border: 1px solid #dddddd; text-align: left; padding: 8px; }} tr:nth-child(even) {{ background - color: #dddddd; }} </style> </head> <body> <h1>Reservations for: {DateTime.Today.ToString("yyyy-MM-dd")}</h1> {reservationsTableHtml} </body> </html> "; GlobalSettings globalSettings = new GlobalSettings(); globalSettings.ColorMode = ColorMode.Color; globalSettings.Orientation = Orientation.Portrait; globalSettings.PaperSize = PaperKind.A4; globalSettings.Margins = new MarginSettings { Top = 10, Bottom = 10 }; ObjectSettings objectSettings = new ObjectSettings(); objectSettings.PagesCount = true; objectSettings.HtmlContent = html; WebSettings webSettings = new WebSettings(); webSettings.DefaultEncoding = "utf-8"; HeaderSettings headerSettings = new HeaderSettings(); headerSettings.FontSize = 15; headerSettings.FontName = "Ariel"; headerSettings.Right = "Page [page] of [toPage]"; headerSettings.Line = true; FooterSettings footerSettings = new FooterSettings(); footerSettings.FontSize = 12; footerSettings.FontName = "Ariel"; footerSettings.Line = true; objectSettings.HeaderSettings = headerSettings; objectSettings.FooterSettings = footerSettings; objectSettings.WebSettings = webSettings; HtmlToPdfDocument htmlToPdfDocument = new HtmlToPdfDocument() { GlobalSettings = globalSettings, Objects = { objectSettings }, }; return(this.Converter.Convert(htmlToPdfDocument)); }
/// <summary> /// Configure and render pdf /// </summary> /// <param name="optionalGlobalSettings">General configuration settings. Can be null</param> /// <param name="viewsToRenderString">A dictionary that passes the name of the view and the object to a string</param> /// <param name="hasPagesCount">A bool that is meant to say if the PDF will have a page counter or not</param> /// <param name="optionalWebSettings">Web configurations such as enabling JavaScript, setting a background or image. Can be null</param> /// <param name="optionalHeaderSettings">The text and it's configurations that goes on the header. Can be null</param> /// <param name="optionalFooterSettings">The text and it's configurations that goes on the footer. Can be null</param> /// <returns>Converter</returns> public async Task <byte[]> RenderPdfToByteArray(GlobalSettings optionalGlobalSettings, Dictionary <string, object> viewsToRenderString, bool hasPagesCount, WebSettings optionalWebSettings, HeaderSettings optionalHeaderSettings, FooterSettings optionalFooterSettings) { // render the pdf, convert it to byte[] and return the byte[] return(_converter.Convert(await RenderPdf(optionalGlobalSettings, viewsToRenderString, hasPagesCount, optionalWebSettings, optionalHeaderSettings, optionalFooterSettings)));; }
/// <summary> /// Configure and render the pdf /// </summary> /// <param name="optionalGlobalSettings">General configuration settings. Can be null</param> /// <param name="viewsToRenderString">A dictionary that passes the name of the view and the object to a string</param> /// <param name="hasPagesCount">A bool that is meant to say if the PDF will have a page counter or not</param> /// <param name="optionalWebSettings">Web configurations such as enabling JavaScript, setting a background or image. Can be null</param> /// <param name="optionalHeaderSettings">The text and it's configurations that goes on the header. Can be null</param> /// <param name="optionalFooterSettings">The text and it's configurations that goes on the footer. Can be null</param> /// <returns>HtmlToPdfDocument</returns> public async Task <HtmlToPdfDocument> RenderPdf(GlobalSettings optionalGlobalSettings, Dictionary <string, object> viewsToRenderString, bool hasPagesCount, WebSettings optionalWebSettings, HeaderSettings optionalHeaderSettings, FooterSettings optionalFooterSettings) { // If the WebSettings is null, set it to a default if (optionalWebSettings == null) { optionalWebSettings = new WebSettings { DefaultEncoding = "utf-8" } } ; // Intance of HtmlToPdfDocument to set the GlobalSettings HtmlToPdfDocument htmlToPdfDocument = new HtmlToPdfDocument() { GlobalSettings = optionalGlobalSettings }; // A loop that will go through each view and will add it to a object list with its own data foreach (var item in viewsToRenderString) { htmlToPdfDocument.Objects.Add(new ObjectSettings { PagesCount = hasPagesCount, HtmlContent = await _viewRender.RenderToStringAsync(item.Key, item.Value), WebSettings = optionalWebSettings, HeaderSettings = optionalHeaderSettings, FooterSettings = optionalFooterSettings }); } // Return the PDF return(htmlToPdfDocument); } }