public async Task <byte[]> CaptureImage(string url, int?width, int?height) { if (string.IsNullOrWhiteSpace(url)) { url = "http://www.rdacorp.com/"; } if (!width.HasValue) { width = 1280; } if (!height.HasValue) { height = 1024; } var newSessionInfo = await CreateNewSession(); try { using (var session = new ChromeSession(newSessionInfo.WebSocketDebuggerUrl)) { await session.Page.Enable(); //Set the viewport size await session.Emulation.SetDeviceMetricsOverride(new ChromeDevTools.Runtime.Emulation.SetDeviceMetricsOverrideCommand() { Width = (long)width, Height = (long)height, DeviceScaleFactor = 0, Mobile = false, }); using (var navigatorWatcher = new NavigatorWatcher(session)) { await navigatorWatcher.Start(); var navigateResult = await session.Page.Navigate(new NavigateCommand { Url = url }, millisecondsTimeout : 120 * 1000); await navigatorWatcher.WaitForNetworkIdle(); } var screenshot = await session.Page.CaptureScreenshot(new CaptureScreenshotCommand() { Format = "png", Clip = new Viewport() { X = 0, Y = 0, Width = width.Value, Height = height.Value, Scale = 1.0, } }, millisecondsTimeout : 120 * 1000); if (!String.IsNullOrWhiteSpace(screenshot.Data)) { return(Convert.FromBase64String(screenshot.Data)); } return(new byte[] { }); } } finally { await CloseSession(newSessionInfo); } }
public async Task <byte[]> ConvertHtmlToPdf(string html, int?width, int?height, bool?landscape, bool?printBackground) { if (string.IsNullOrWhiteSpace(html)) { html = "<html><body>Hello, World!</body></html>"; } if (!width.HasValue) { width = 1280; } if (!height.HasValue) { height = 1024; } var newSessionInfo = await CreateNewSession(); try { using (var session = new ChromeSession(newSessionInfo.WebSocketDebuggerUrl)) { await session.Page.Enable(); //Set the viewport size await session.Emulation.SetDeviceMetricsOverride(new ChromeDevTools.Runtime.Emulation.SetDeviceMetricsOverrideCommand() { Width = (long)width, Height = (long)height, DeviceScaleFactor = 0, Mobile = false, }); var frameTreeResponse = await session.Page.GetFrameTree(new GetFrameTreeCommand()); using (var navigatorWatcher = new NavigatorWatcher(session)) { await navigatorWatcher.Start(); var setDocumentContentResult = session.Page.SetDocumentContent(new SetDocumentContentCommand() { FrameId = frameTreeResponse.FrameTree.Frame.Id, Html = html }, millisecondsTimeout: 120 * 1000); await navigatorWatcher.WaitForNetworkIdle(); } var pdf = await session.Page.PrintToPDF(new PrintToPDFCommand() { PrintBackground = printBackground, Landscape = landscape, }, millisecondsTimeout : 120 * 1000); if (!String.IsNullOrWhiteSpace(pdf.Data)) { return(Convert.FromBase64String(pdf.Data)); } return(new byte[] { }); } } finally { await CloseSession(newSessionInfo); } }
public async Task <byte[]> CapturePdf(string url, int?width, int?height, bool?landscape, bool?printBackground) { if (string.IsNullOrWhiteSpace(url)) { url = "http://www.rdacorp.com/"; } if (!width.HasValue) { width = 1280; } if (!height.HasValue) { height = 1024; } var newSessionInfo = await CreateNewSession(); try { using (var session = new ChromeSession(newSessionInfo.WebSocketDebuggerUrl)) { //Set the viewport size await session.Emulation.SetDeviceMetricsOverride(new ChromeDevTools.Runtime.Emulation.SetDeviceMetricsOverrideCommand() { Width = (long)width, Height = (long)height, DeviceScaleFactor = 0, Mobile = false, }); using (var navigatorWatcher = new NavigatorWatcher(session)) { await navigatorWatcher.Start(); var navigateResult = await session.Page.Navigate(new NavigateCommand { Url = url }, millisecondsTimeout : 120 * 1000); await navigatorWatcher.WaitForNetworkIdle(); } var pdf = await session.Page.PrintToPDF(new PrintToPDFCommand() { PrintBackground = printBackground, Landscape = landscape, }, millisecondsTimeout : 120 * 1000); if (!String.IsNullOrWhiteSpace(pdf.Data)) { return(Convert.FromBase64String(pdf.Data)); } return(new byte[] { }); } } finally { await CloseSession(newSessionInfo); } }