To create a PDF using the Selenium Chrome driver, you can follow the below steps:
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("plugins.always_open_pdf_externally", true);
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
driver
instance:driver.navigate().to("https://example.com");
executeCdpCommand()
method to send a command to Chrome. To create a PDF, send the following command:HashMap<String, Object> params = new HashMap<>();
params.put("landscape", false);
params.put("displayHeaderFooter", false);
params.put("printBackground", true);
params.put("preferCSSPageSize", true);
params.put("marginTop", 0);
params.put("marginBottom", 0);
params.put("marginLeft", 0);
params.put("marginRight", 0);
params.put("scale", 1);
driver.executeCdpCommand("Page.printToPDF", params);
printToPDF()
method, as shown below:byte[] pdfContent = driver.executeCdpCommand("Page.printToPDF", params).get("data").toString().getBytes();
try (FileOutputStream fos = new FileOutputStream("sample.pdf")) {
fos.write(pdfContent);
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-16 18:21:52 +0000
Seen: 16 times
Last updated: Jun 16 '23
How can a python script be executed using a chrome extension?
How can Chrome be set up to detect SSL certificates from Wamp Apache?
How can I add an Angular back button to the stack router outlet in Chrome?
How to turn off the automatic update feature of Google Chrome on Windows 11?
How can I use Puppeteer with an existing installation of Chrome on MacOS?
Is there a way to add JS code to Chrome without using an extension?
How to change the URL in a Chrome extension?
How to look for information within a JSON object using Google Chrome DevTools?
Can you reword the phrase "SPI Bitbang driver in Linux"?
What is the JDBC driver jar for com.simba.spark.jdbc4.Driver?