try-playwright
try-playwright copied to clipboard
Example generating PDF in Java uses the wrong method
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
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")));
}
}
}
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!