I can think of two ways to achieve this.
Use print.always_print_silent flag
WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.addPreference("print.always_print_silent", true);
options.addPreference("print.printer_Microsoft_Print_to_PDF.print_to_file", true);
WebDriver driver = new FirefoxDriver(options);
driver.get("http://localhost:3000");
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("window.print();");
Another way can be to use TakeScreenshot from selenium.
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
First get width and height
JavascriptExecutor js = (JavascriptExecutor) driver;
int totalHeight = ((Number) js.executeScript("return document.body.scrollHeight")).intValue();
int viewportHeight = ((Number) js.executeScript("return window.innerHeight")).intValue();
use javascript to keep scrolling and keep taking snaps.
int scrollPosition = 0;
List<File> screenshots = new ArrayList<>();
while (scrollPosition < totalHeight) {
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
screenshots.add(screenshot);
scrollPosition += viewportHeight;
js.executeScript("window.scrollBy(0, arguments[0]);", viewportHeight);
Thread.sleep(1000); // sleep to handle load time
}
This way you will have the snaps in form of PNG, you can then use any PDF library (e.g. PDFBox) to add the images to pdf and save it as a file.
You can refer Java: Create PDF pages from images using PDFBox 1 library for the task.