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

Whether to print headers or footers when printing in Chrome. #1761

Open
jaychoubaby opened this issue Sep 20, 2023 · 0 comments
Open

Whether to print headers or footers when printing in Chrome. #1761

jaychoubaby opened this issue Sep 20, 2023 · 0 comments

Comments

@jaychoubaby
Copy link

I am using selenium to drive chrome, rendering HTML pages into PDF, and I have succeeded. However, I found that I cannot set the styles for headers and footers, nor can I control their visibility. But when operating with chrome in the browser, these functions are indeed available. Since I can use the method webDriver.print(new PrintOptions()).getContent(); in selenium to set parameters such as orientation, scale, background, shrinkToFit, pageSize, pageMargin, and pageRanges through PrintOptions(), why can't other properties be set together? After checking the source code and information, I found that it is not supported and raised an issue on the selenium project. The response was that this requires webdriver support.
image
It seems that everything is supported except for setting the header and footer.
The following are the issues I raised in Selenium:
SeleniumHQ/selenium#12763 (comment)

The following is the information I have looked up:
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/print/PrintOptions.html
image
I have reviewed the source code and documentation, but it seems that I cannot find a way to set --print-header-template in PrintOptions. According to my search on Python methods, it appears that this can only be set in ChromeOptions. Is there a better way for me to set this property in PrintOptions or RemoteWebDriver?

Also, I have another question. Currently, I am using springboot integrated with selenium. Here is my code:

@Data
public class CpPrintOptions extends PrintOptions {


    public Map<String, Object> toMap() {
        final Map<String, Object> options = new HashMap<>(7);
        options.put("page", getPageSize());
        options.put("orientation", getOrientation().toString());
        options.put("scale", getScale());
        options.put("shrinkToFit", getShrinkToFit());
        options.put("background", getBackground());
        final String[] effectivePageRanges = getPageRanges();
        if (effectivePageRanges != null) {
            options.put("pageRanges", effectivePageRanges);
        }
        options.put("margin", getPageMargin());
        options.put("displayHeaderFooter", true);
        options.put("headerTemplate", "<span style=\"font-size:8px; margin-left: 5px\">Page <span class=pageNumber></span> of <span class=totalPages></span></span>");
        options.put("footerTemplate", "<span style=\"font-size:8px; margin-left: 5px\">Generated on {datetime.now().strftime(\"%m/%d/%Y at %H:%M\")} - url = <span class=url></span></span>");
        return options;
    }
}

I tried to integrate PrintOptions() and specify the properties, but it seems not to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant