Esempio n. 1
0
        public async Task <byte[]> RenderUrl(string url, HtmlRendererOptions htmlOptions = null,
                                             PdfOptions pdfOptions = null)
        {
            async void OnPageRequest(object sender, RequestEventArgs args)
            {
                if (args.Request.IsNavigationRequest)
                {
                    await args.Request.ContinueAsync(new Payload
                    {
                        Headers = htmlOptions.RequestHeaders
                    });
                }
                else
                {
                    await args.Request.ContinueAsync();
                }
            }

            await InitializeBrowser();

            var page = await Browser.NewPageAsync();

            if (htmlOptions?.RequestHeaders != null && htmlOptions.RequestHeaders.Count > 0)
            {
                await page.SetRequestInterceptionAsync(true);

                page.Request += OnPageRequest;
            }

            var pdfData = await RunInternal(url, htmlOptions, pdfOptions, page).ContinueWith(t =>
            {
                if (t.IsCompletedSuccessfully)
                {
                    return((object)t.Result);
                }

                return(t.Exception);
            });

            await page.CloseAsync();

            await page.DisposeAsync();

            if (htmlOptions?.RequestHeaders != null && htmlOptions.RequestHeaders.Count > 0)
            {
                page.Request -= OnPageRequest;
            }

            if (pdfData is byte[] result)
            {
                return(result);
            }

            if (pdfData is Exception ex)
            {
                throw ex;
            }

            throw new InvalidOperationException("Something went terribly wrong.");
        }
Esempio n. 2
0
        private static async Task <byte[]> RunInternal(string url, HtmlRendererOptions htmlOptions,
                                                       PdfOptions pdfOptions, Page page)
        {
            await(htmlOptions?.RunBeforePageLoad?.Invoke(page) ?? Task.CompletedTask);
            await page.GoToAsync(url);

            await(htmlOptions?.RunBeforePdf?.Invoke(page) ?? Task.CompletedTask);
            var pdfData = pdfOptions == null ? await page.PdfDataAsync() : await page.PdfDataAsync(pdfOptions);

            await(htmlOptions?.RunAfterPdf?.Invoke(page) ?? Task.CompletedTask);
            return(pdfData);
        }
Esempio n. 3
0
        public async Task <byte[]> RenderHtml(string html, HtmlRendererOptions htmlOptions = null,
                                              PdfOptions pdfOptions = null)
        {
            await InitializeBrowser();

            var page = await Browser.NewPageAsync();

            await(htmlOptions?.RunBeforePageLoad?.Invoke(page) ?? Task.CompletedTask);
            await page.SetContentAsync(html);

            await(htmlOptions?.RunBeforePdf?.Invoke(page) ?? Task.CompletedTask);
            var pdfData = pdfOptions == null ? await page.PdfDataAsync() : await page.PdfDataAsync(pdfOptions);

            await(htmlOptions?.RunAfterPdf?.Invoke(page) ?? Task.CompletedTask);
            await page.CloseAsync();

            await page.DisposeAsync();

            return(pdfData);
        }