There is a method called getPageSource() in selenium webdriver.
It returns string, so you can either store it in a file or can print it in the console.
WebDriver driver = new ChromeDriver();
driver.get("https://www.googel.com/");
String str = driver.getPageSource();
System.out.println(str);
Try this.
For further understanding, you can refer to the Selenium Course.