public static void Main() { var webpageFile = $"{Environment.CurrentDirectory}\\HTMLPage1.html"; var html = File.ReadAllText(webpageFile); File.WriteAllText(webpageFile, html.Replace("[Name]", "Sir/Madam")); var chromeOptions = new ChromeOptions(); chromeOptions.AddArgument("--headless"); chromeOptions.AddArgument("--disable-gpu"); chromeOptions.AddExcludedArgument("enable-automation"); chromeOptions.AddAdditionalCapability("useAutomationExtension", false); var driver = new OpenQA.Selenium.Chrome.ChromeDriver(chromeOptions); driver.Navigate().GoToUrl($"file:///{webpageFile}"); var result = (Dictionary <string, object>)driver.ExecuteChromeCommandWithResult("Page.printToPDF", new Dictionary <string, object> { { "displayHeaderFooter", false }, { "transferMode", "ReturnAsBase64" } }); File.WriteAllBytes($"{Environment.CurrentDirectory}\\example_letter.pdf", Convert.FromBase64String((string)result["data"])); driver.Quit(); driver.Dispose(); }
public object ExecuteChromeCommandWithResult(string commandName, Dictionary <string, object> commandParameters) { object obj; OpenQA.Selenium.Chrome.ChromeDriver chromeDriver = this.ChromeDriver; if (chromeDriver != null) { obj = chromeDriver.ExecuteChromeCommandWithResult(commandName, commandParameters); } else { obj = null; } return(obj); }