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

[🐛 Bug][java]: Setting page ranges of PrintOptions does not work when print pdf #11172

Closed
silentsoft opened this issue Oct 26, 2022 · 2 comments · Fixed by #11174
Closed

Comments

@silentsoft
Copy link
Contributor

What happened?

Description

Setting page ranges of PrintOptions does not work when print pdf.

If I want to print pdf for google.com with 25cm x 5cm paper size for page 1 and 2 only:

    public static void main(String[] args) throws Exception {
        WebDriverManager.chromedriver().setup();

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setHeadless(true);

        ChromeDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://google.com");

        PrintOptions printOptions = new PrintOptions();
        printOptions.setPageSize(new PageSize(5, 25));
        printOptions.setPageRanges("1-2");
        Pdf pdf = driver.print(printOptions);

        Path output = Paths.get(System.getProperty("user.dir"), "result.pdf");
        Files.write(output, OutputType.BYTES.convertFromBase64Png(pdf.getContent()));

        driver.quit();
    }

This line is ignored.

    printOptions.setPageRanges("1-2");

스크린샷 2022-10-26 23 50 50

Reason

toMap() function in PrintOptions.java class should set pageRanges as a key not effectivePageRanges.

As-is

  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("effectivePageRanges", effectivePageRanges);
    }
    options.put("margin", getPageMargin());

   return options;
  }

To-be

  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());

   return options;
  }

Test

Super simple hack code to figure out that pageRanges works or not:

    public static void main(String[] args) throws Exception {
        WebDriverManager.chromedriver().setup();

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setHeadless(true);

        ChromeDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://google.com");

        PrintOptions printOptions = new PrintOptions() {
            @Override
            public Map<String, Object> toMap() {
                Map<String, Object> map = super.toMap();
                map.put("pageRanges", new String[]{"1-2"});
                return map;
            }
        };
        printOptions.setPageSize(new PageSize(5, 25));
        Pdf pdf = driver.print(printOptions);

        Path output = Paths.get(System.getProperty("user.dir"), "result.pdf");
        Files.write(output, OutputType.BYTES.convertFromBase64Png(pdf.getContent()));

        driver.quit();
    }

스크린샷 2022-10-27 00 00 22

How can we reproduce the issue?

public static void main(String[] args) throws Exception {
    // WebDriverManager.chromedriver().setup();

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setHeadless(true);

    ChromeDriver driver = new ChromeDriver(chromeOptions);
    driver.get("https://google.com");

    PrintOptions printOptions = new PrintOptions();
    printOptions.setPageSize(new PageSize(5, 25));
    printOptions.setPageRanges("1-2");
    Pdf pdf = driver.print(printOptions);

    Path output = Paths.get(System.getProperty("user.dir"), "result.pdf");
    Files.write(output, OutputType.BYTES.convertFromBase64Png(pdf.getContent()));

    driver.quit();
}

Relevant log output

N/A

Operating System

macOS Monterey 12.1

Selenium version

Java 4.5.3

What are the browser(s) and version(s) where you see this issue?

Chrome 106. But I think this affects all browsers.

What are the browser driver(s) and version(s) where you see this issue?

Chrome Driver 106.0.5249.61

Are you using Selenium Grid?

No

@github-actions
Copy link

@silentsoft, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

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

Successfully merging a pull request may close this issue.

1 participant