Examples

HTML to PDF with C# and Selenium

Install Selenium for .NET

dotnet add package Selenium.WebDriver
dotnet add package Selenium.Support

Selenium.WebDriver 4.11+ includes Selenium Manager, which downloads the right chromedriver automatically. Selenium.Support is only needed for WebDriverWait in the wait example. The examples use C# top-level statements — put them in Program.cs of a console project and run with dotnet run.

Convert HTML to PDF and save to file

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

var options = new ChromeOptions();
options.AddArgument("--headless=new");

using var driver = new ChromeDriver(options);

var html = "<html><body><h1>Hello World</h1></body></html>";
driver.Navigate().GoToUrl("data:text/html;charset=utf-8," + html);

var pdf = driver.Print(new PrintOptions());
pdf.SaveAsFile("html.pdf");

driver.Quit();

Find out how to change the size of the viewport with C# and Selenium.

That was 15 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.