try-playwright icon indicating copy to clipboard operation
try-playwright copied to clipboard

Example generating PDF in Java uses the wrong method

Open Mattsi-Jansky opened this issue 2 years ago • 1 comments

The following "Generate a PDF" example in Java is calling the wrong method: https://try.playwright.tech/?e=generate-pdf&l=java

The current example is as follows:

package org.example;

import com.microsoft.playwright.*;
import java.nio.file.Paths;

public class WebKitScreenshot {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.webkit().launch();
      Page page = browser.newPage();
      page.navigate("http://whatsmyuseragent.org/");
      page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("example.png")));
    }
  }
}

The final line is calling the screenshot method, which won't (I think?) generate a PDF. I believe it should be calling the PDF method, here: https://playwright.dev/java/docs/api/class-page#page-pdf

Mattsi-Jansky avatar Sep 08 '22 10:09 Mattsi-Jansky

I think something like this would be correct?

package org.example;

import com.microsoft.playwright.*;
import java.nio.file.Paths;

public class ChromePdf {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chrome().launch();
      Page page = browser.newPage();
      page.navigate("http://whatsmyuseragent.org/");
      page.pdf(new Page.PdfOptions().setPath(Paths.get("example.pdf")));
    }
  }
}

Mattsi-Jansky avatar Sep 08 '22 10:09 Mattsi-Jansky

Sorry for the late reply! There is no generate PDF example for Java, thats why it falls back to the screenshot example, which is the first example in the list on the right side.

Closing as per above, thanks!

mxschmitt avatar Jun 17 '23 21:06 mxschmitt