For those who want more precise answer here is my code that works on Windows 7.
private async void SaveToFile(string url)
{
try
{
WebSocketFactory webSocketFactory = CreateWebSocket;
LaunchOptions launchOptions = new LaunchOptions()
{
Headless = true,
WebSocketFactory = webSocketFactory
};
var browser = await Puppeteer.LaunchAsync(launchOptions, product: Product.Chrome);
var page = await browser.NewPageAsync();
await page.GoToAsync(url);
await page.PdfAsync("MyPdf.pdf", new PdfOptions() { Format = PuppeteerSharp.Media.PaperFormat.A4 });
Task.WaitAll(new Task[2] {
page.DisposeAsync().AsTask(),
browser.DisposeAsync().AsTask() });
}
}
}
catch (Exception objException)
{
CommonMsgBox.Warning(objException.Message, "Error");
}
}
private static async Task<System.Net.WebSockets.WebSocket> CreateWebSocket(Uri url, IConnectionOptions options, CancellationToken cancellationToken)
{
var result = new System.Net.WebSockets.Managed.ClientWebSocket();
result.Options.KeepAliveInterval = TimeSpan.Zero;
await result.ConnectAsync(url, cancellationToken).ConfigureAwait(false);
return result;
}
System.Net.WebSockets.Managed
with all dependencies can be downloaded from NuGet.
EDIT: Puppeteer have sometimes strange behavior and its better not to wrap its component with using
otherwise it hangs in some situations. I have removed this wrappings from code above.