Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add print functionality to javascript package #8801

Closed
space-rodeo opened this issue Oct 17, 2020 · 3 comments
Closed

Add print functionality to javascript package #8801

space-rodeo opened this issue Oct 17, 2020 · 3 comments
Labels

Comments

@space-rodeo
Copy link

🚀 Feature Proposal

The latest WebDrivers have added the print functionality from the latest W3C working draft but the js library does not have a command to use this.

Motivation

The closest functionality that exists in selenium is taking a screenshot but that does not cover all cases for printing. There are many libraries that depend on selenium specifically for saving the html as pdf but do not use the latest WebDrivers. This will save all those developers from needing to create alternate libraries and avoid bugs.

Example

const driver = await new Builder()
    .forBrowser('firefox')
    .setFirefoxOptions(new firefox.Options().headless().windowSize(windowSize))
    .build();
await driver.get('https://www.npmjs.com/package/selenium-webdriver');
const pdfBuffer = await driver.print(printOptions);
fs.writeFileSync('website.pdf', pdfBuffer);
@ghost ghost added the needs-triaging label Oct 17, 2020
@harsha509
Copy link
Member

HI @space-rodeo ,

Print Page is the new WebDriver API. Not only in JS, this feature is not implemented in any other language binding yet.

Am already working on implementing this feature in PR #8597 with couple of changes. This feature will be available in next Alpha release.

Thank you!

@space-rodeo
Copy link
Author

Great! This can be done with current library by adding this code:

import command from 'selenium-webdriver/lib/command');

// This function only needs to be done once for the driver
function addPrintCommand(driver) {
    const executor = driver.getExecutor();
    executor.defineCommand('print', 'POST', '/session/:sessionId/print');
}

function printToPdf(printOptions) {
    const pdfBase64 = await driver.execute(new command.Command('print').setParameters(printOptions));
    return Buffer.from(pdfBase64, 'base64');
}

Thought I'd leave this here for anyone stumbling upon it before release.

Thank you!

@harsha509
Copy link
Member

Landed with PR #8880. Will be available in next beta release.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants