Whether to print headers or footers when printing in Chrome.
Nobody has claimed this yet.
- Dominant language
- HTML
- Stars
- 718
- Forks
- 228
- PR merge metrics
- No merged PRs in 30d
Description
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.
It seems that everything is supported except for setting the header and footer.
The following are the issues I raised in Selenium:
https://github.com/SeleniumHQ/selenium/issues/12763#issuecomment-1724774701
The following is the information I have looked up:
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/print/PrintOptions.html
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the WebDriver print command and the Selenium PrintOptions documentation cited in the issue, then compare the supported fields with Chrome's header and footer behavior. The Selenium issue provides related context. Done would require a clarified WebDriver API decision for displayHeaderFooter, headerTemplate, and footerTemplate, rather than an application-level workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- api, web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100