Ejemplo n.º 1
0
        /// <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);
        }
    }