Beispiel #1
0
        public async Task <Stream> PdfStreamAsync(PdfOptions options)
        {
            var paperWidth  = 8.5m;
            var paperHeight = 11m;

            if (!string.IsNullOrEmpty(options.Format))
            {
                if (!_paperFormats.ContainsKey(options.Format.ToLower()))
                {
                    throw new ArgumentException("Unknown paper format");
                }

                var format = _paperFormats[options.Format.ToLower()];
                paperWidth  = format.Width;
                paperHeight = format.Height;
            }
            else
            {
                if (options.Width != null)
                {
                    paperWidth = ConvertPrintParameterToInches(options.Width);
                }
                if (options.Height != null)
                {
                    paperHeight = ConvertPrintParameterToInches(options.Height);
                }
            }

            var marginTop    = ConvertPrintParameterToInches(options.MarginOptions.Top);
            var marginLeft   = ConvertPrintParameterToInches(options.MarginOptions.Left);
            var marginBottom = ConvertPrintParameterToInches(options.MarginOptions.Bottom);
            var marginRight  = ConvertPrintParameterToInches(options.MarginOptions.Right);

            JObject result = await _client.SendAsync("Page.printToPDF", new
            {
                landscape           = options.Landscape,
                displayHeaderFooter = options.DisplayHeaderFooter,
                headerTemplate      = options.HeaderTemplate,
                footerTemplate      = options.FooterTemplate,
                printBackground     = options.PrintBackground,
                scale = options.Scale,
                paperWidth,
                paperHeight,
                marginTop,
                marginBottom,
                marginLeft,
                marginRight,
                pageRanges = options.PageRanges
            });

            var buffer = Convert.FromBase64String(result.GetValue("data").Value <string>());

            return(new MemoryStream(buffer));
        }
Beispiel #2
0
        public async Task PdfAsync(string file, PdfOptions options)
        {
            var stream = await PdfStreamAsync(options);

            using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                byte[] bytesInStream = new byte[stream.Length];
                stream.Read(bytesInStream, 0, bytesInStream.Length);
                fs.Write(bytesInStream, 0, bytesInStream.Length);
            }
        }