Example #1
0
        public async Task <byte[]> ScreenshotPNG(string url, IEnumerable <CookieParam> cookies, ViewPortOptions viewport, ScreenshotOptions options, RevisionInfo revision)
        {
            LaunchOptions launchOptions = new LaunchOptions()
            {
                ExecutablePath  = revision.ExecutablePath,
                Args            = BrowserArgs,
                Headless        = true,
                DefaultViewport = viewport,
                Timeout         = 0
            };

            browser = await Puppeteer.LaunchAsync(launchOptions);

            try {
                var page = await browser.NewPageAsync();

                try {
                    NavigationOptions navigationOptions = new NavigationOptions()
                    {
                        Timeout = 0
                    };

                    string originalUserAgent = await browser.GetUserAgentAsync();

                    await page.SetUserAgentAsync($"{originalUserAgent} {USER_AGENT_SUFFIX}");

                    if (cookies.Any())
                    {
                        await page.SetCookieAsync(cookies.ToArray());
                    }

                    await page.GoToAsync(url, navigationOptions);

                    return(await page.ScreenshotDataAsync(options));
                } finally {
                    await Cleanup(page);
                }
            } finally {
                Cleanup(browser);
            }
        }
Example #2
0
        public async Task <byte[]> PrintPDF(string url, IEnumerable <CookieParam> cookies, ViewPortOptions viewport, PdfOptions options, RevisionInfo revision)
        {
            LaunchOptions launchOptions = new LaunchOptions()
            {
                ExecutablePath  = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
                Args            = BrowserArgs,
                Headless        = true,
                DefaultViewport = viewport,
                Timeout         = 0
            };

            browser = await Puppeteer.LaunchAsync(launchOptions);

            try {
                var page = await browser.NewPageAsync();

                try {
                    WaitUntilNavigation[] waitUntilNavigations = { WaitUntilNavigation.Networkidle0 };
                    NavigationOptions     navigationOptions    = new NavigationOptions()
                    {
                        Timeout   = 0,
                        WaitUntil = waitUntilNavigations
                    };

                    string originalUserAgent = await browser.GetUserAgentAsync();

                    await page.SetUserAgentAsync($"{originalUserAgent} {USER_AGENT_SUFFIX}");

                    if (cookies.Any())
                    {
                        await page.SetCookieAsync(cookies.ToArray());
                    }

                    await page.GoToAsync(url, navigationOptions);
                    await InjectCustomStylesAsync(page, ref options);

                    bool hasPageNumbers = await page.EvaluateFunctionAsync <bool>("(window.UltimatePDF? window.UltimatePDF.hasPageNumbers : function() { return false; })");

                    if (hasPageNumbers)
                    {
                        /*
                         * When the layout has page numbers, we first retrieve a
                         * first blank pdf to calculate the number of pages.
                         * Then, knowing how many pages, we can layout the headers and footers,
                         * and retrieve the final pdf.
                         */
                        byte[] blankContents = await page.PdfDataAsync(options);

                        using (var blankPdf = new PDFUtils(blankContents)) {
                            await page.EvaluateFunctionAsync("window.UltimatePDF.layoutPages", blankPdf.Pages);

                            return(await page.PdfDataAsync(options));
                        }
                    }
                    else
                    {
                        return(await page.PdfDataAsync(options));
                    }
                } finally {
                    await Cleanup(page);
                }
            } finally {
                Cleanup(browser);
            }
        }