Beispiel #1
0
        /// <summary>
        ///     Instructs Chrome to print the page
        /// </summary>
        /// <param name="pageSettings"><see cref="PageSettings" /></param>
        /// <param name="countdownTimer">If a <see cref="CountdownTimer"/> is set then
        /// the method will raise an <see cref="ConversionTimedOutException"/> in the
        /// <see cref="CountdownTimer"/> reaches zero before finishing the printing to pdf</param>
        /// <remarks>
        ///     See https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
        /// </remarks>
        /// <exception cref="ConversionException">Raised when Chrome returns an empty string</exception>
        /// <exception cref="ConversionTimedOutException">Raised when <paramref name="countdownTimer"/> reaches zero</exception>
        internal PrintToPdfResponse PrintToPdf(PageSettings pageSettings, CountdownTimer countdownTimer = null)
        {
            var message = new Message {
                Method = "Page.printToPDF"
            };

            message.AddParameter("landscape", pageSettings.Landscape);
            message.AddParameter("displayHeaderFooter", pageSettings.DisplayHeaderFooter);
            message.AddParameter("printBackground", pageSettings.PrintBackground);
            message.AddParameter("scale", pageSettings.Scale);
            message.AddParameter("paperWidth", pageSettings.PaperWidth);
            message.AddParameter("paperHeight", pageSettings.PaperHeight);
            message.AddParameter("marginTop", pageSettings.MarginTop);
            message.AddParameter("marginBottom", pageSettings.MarginBottom);
            message.AddParameter("marginLeft", pageSettings.MarginLeft);
            message.AddParameter("marginRight", pageSettings.MarginRight);
            message.AddParameter("pageRanges", pageSettings.PageRanges ?? string.Empty);
            message.AddParameter("ignoreInvalidPageRanges", pageSettings.IgnoreInvalidPageRanges);

            var result = countdownTimer == null
                ? _pageConnection.SendAsync(message).GetAwaiter().GetResult()
                : _pageConnection.SendAsync(message).Timeout(countdownTimer.MillisecondsLeft).GetAwaiter().GetResult();

            var printToPdfResponse = PrintToPdfResponse.FromJson(result);

            if (string.IsNullOrEmpty(printToPdfResponse.Result?.Data))
            {
                throw new ConversionException("Conversion failed");
            }

            return(printToPdfResponse);
        }
Beispiel #2
0
        /// <summary>
        /// Instructs Chrome to print the page
        /// </summary>
        /// <param name="pageSettings"><see cref="PageSettings"/></param>
        /// <remarks>
        /// See https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
        /// </remarks>
        /// <returns>
        ///     <code>
        ///         var uri = new Uri("http://www.google.nl");
        ///         var result = NavigateTo(uri);
        ///         // Succes when value contains the Uri result.InnerResult.Value
        ///         // contains the requested uri.
        ///         if (result.Result.ExceptionDetails != null)
        ///             throw new Exception("Navigation failed");
        ///     </code>
        /// </returns>
        /// <exception cref="ChromeException">Raised when an error is returned by Chrome</exception>
        internal PrintToPdfResponse PrintToPdf(PageSettings pageSettings)
        {
            var message = new Message
            {
                Id     = MessageId,
                Method = "Page.printToPDF"
            };

            message.AddParameter("landscape", pageSettings.Landscape);
            message.AddParameter("displayHeaderFooter", pageSettings.DisplayHeaderFooter);
            message.AddParameter("printBackground", pageSettings.PrintBackground);
            message.AddParameter("scale", pageSettings.Scale);
            message.AddParameter("paperWidth", pageSettings.PaperWidth);
            message.AddParameter("paperHeight", pageSettings.PaperHeight);
            message.AddParameter("marginTop", pageSettings.MarginTop);
            message.AddParameter("marginBottom", pageSettings.MarginBottom);
            message.AddParameter("marginLeft", pageSettings.MarginLeft);
            message.AddParameter("marginRight", pageSettings.MarginRight);
            message.AddParameter("pageRanges", pageSettings.PageRanges);
            message.AddParameter("ignoreInvalidPageRanges", pageSettings.IgnoreInvalidPageRanges);

            var converted = false;
            PrintToPdfResponse response = null;

            _webSocket.MessageReceived += (sender, args) =>
            {
                response = PrintToPdfResponse.FromJson(args.Message);
                if (response.Result?.Data != null)
                {
                    converted = true;
                }
            };

            WebSocketSend(message.ToJson());

            while (!converted)
            {
                Thread.Sleep(1);
            }

            return(response);
        }