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();
String str = driver.getPageSource();
For further understanding, you can refer to the Selenium Course.