Exemplo n.º 1
0
        /// <summary>
        /// Builds an HTML footer for the PDF publication
        /// from informations contained in the conversion parameters.
        /// </summary>
        /// <param name="parameters">The parameters of the conversion.</param>
        /// <returns>A string containing the HTML footer for the PDF publication.</returns>
        private static string BuildFooter(HtmlToPdfConverterOptions parameters)
        {
            var date   = DateTime.Now.ToString(Resources.DATE_FORMAT, new CultureInfo(Resources.CULTURE_EN_US)).ToUpper();
            var footer = string.Format(Resources.FOOTER, parameters.ExportClassification, parameters.ExportControl, date, Resources.PAGE_NUMBER);

            return(footer);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Converts an HTML stream to PDF using the given options
 /// </summary>
 /// <param name="input">The input stream.</param>
 /// <param name="output">The output stream.</param>
 /// <param name="options">The options to use for the conversion.</param>
 public static void Convert(Stream input, Stream output, HtmlToPdfConverterOptions options)
 {
     using (var sr = new StreamReader(input))
     {
         var html = sr.ReadToEnd();
         Convert(html, output, options);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Converts an HTML page to PDF using the given options
        /// </summary>
        /// <param name="html">The HTML content to convert.</param>
        /// <param name="output">The output stream.</param>
        /// <param name="options">The options to use for the conversion.</param>
        public static void Convert(string html, Stream output, HtmlToPdfConverterOptions options)
        {
            // Sets the license
            SetLicense();

            // Initialize parameters
            var opts = InitParameters(html, options);

#if DEBUG
            using (var fw = File.CreateText(@"C:\temp\js_out.txt"))
            {
                HtmlToPdf.DebugConsole = fw;
                HtmlToPdf.ConvertHtml(html, output, opts);
            }
#else
            // Convert HTMl to PDF
            HtmlToPdf.ConvertHtml(html, output, opts);
#endif
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initialise the parameters of the conversion,
        /// using the class parameters and the given parameters
        /// </summary>
        /// <param name="options">custom user parameter values</param>
        /// <returns>An HtmlToPdfOptions object</returns>
        private static HtmlToPdfOptions InitParameters(string html, HtmlToPdfConverterOptions options)
        {
            var parameters = (HtmlToPdfConverterOptions)Options.Clone();

            if (options != null)
            {
                parameters.BaseUrl                 = options.BaseUrl ?? parameters.BaseUrl;
                parameters.ExportControl           = string.IsNullOrEmpty(options.ExportControl) ? parameters.ExportControl : options.ExportControl;
                parameters.ExportClassification    = string.IsNullOrEmpty(options.ExportClassification) ? parameters.ExportClassification : options.ExportClassification;
                parameters.OutputArea              = options.OutputArea.IsEmpty ? parameters.OutputArea : options.OutputArea;
                parameters.PageSize                = options.PageSize.IsEmpty ? parameters.PageSize : options.PageSize;
                parameters.RepeatTableHeaderFooter = options.RepeatTableHeaderFooter ?? parameters.RepeatTableHeaderFooter;
                parameters.Timeout                 = options.Timeout ?? parameters.Timeout;
                parameters.VisibleElementIds       = options.VisibleElementIds.Length == 0 ? parameters.VisibleElementIds : options.VisibleElementIds;
                parameters.InvisibleElementIds     = options.InvisibleElementIds.Length == 0 ? parameters.InvisibleElementIds : options.InvisibleElementIds;
            }

            // Try to parse the HTML source as XML.
            XDocument doc = null;

            try
            {
                doc = XDocument.Parse(html);
            }  catch (Exception) { }

            // Build the HTML header if the source HTML could be parsed as XML.
            var header = (doc != null) ? BuildHeader(doc) : string.Empty;
            // Build the HTML footer.
            var footer = BuildFooter(parameters);

            // Options that are always set
            var opts = new HtmlToPdfOptions
            {
                PageSize   = parameters.PageSize,
                OutputArea = parameters.OutputArea,
                NoLink     = true,
                RepeatTableHeaderAndFooter = parameters.RepeatTableHeaderFooter ?? true,
                FooterHtmlPosition         = parameters.PageSize.Height - _regionAfter,
                FooterHtmlFormat           = footer,
                HeaderHtmlPosition         = 0F,
                HeaderHtmlFormat           = header,
            };

            // BaseUrl
            if (parameters.BaseUrl != null)
            {
                opts.BaseUrl = parameters.BaseUrl.AbsoluteUri;
            }

            // Handling of the visible ids list
            if (parameters.VisibleElementIds != null && parameters.VisibleElementIds.Length > 0)
            {
                opts.VisibleElementIds = string.Join(";", parameters.VisibleElementIds);
            }

            // Handling of the invisible ids list
            if (parameters.InvisibleElementIds != null && parameters.InvisibleElementIds.Length > 0)
            {
                opts.InvisibleElementIds = string.Join(";", parameters.InvisibleElementIds);
            }

            return(opts);
        }