microsoft / playwright-python

Python version of the Playwright testing and automation library.
https://playwright.dev/python/
Apache License 2.0
11.81k stars 899 forks source link

PrintToPDF is not implemented error when not headless #297

Closed jtrip closed 3 years ago

jtrip commented 3 years ago

Hello, just getting started with playwright-python. I am running Python 3.7.7 on macOS Mojave (10.14.6) I created a venv, activated it, and ran pip install playwright and python -m playwright install.

Using a simple load page and create pdf script, works great.

from playwright import sync_playwright

with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.newPage()
        page.goto('http://whatsmyuseragent.org/')
        page.pdf(path='test.pdf')
        browser.close()

However running the same with: browser = p.chromium.launch(headless=False)

results in an exception

Traceback (most recent call last):
  File "~/Projects/playwright-test/main.py", line 19, in <module>
    main()
  File "~/Projects/playwright-test/main.py", line 14, in main
    page.pdf(path='test.pdf')
  File "~/Projects/playwright-test/venv/lib/python3.7/site-packages/playwright/sync_api.py", line 5197, in pdf
    path=path,
  File "~/Projects/playwright-test/venv/lib/python3.7/site-packages/playwright/sync_base.py", line 107, in _sync
    return future.result()
  File "~/Projects/playwright-test/venv/lib/python3.7/site-packages/playwright/page.py", line 779, in pdf
    encoded_binary = await self._channel.send("pdf", params)
  File "~/Projects/playwright-test/venv/lib/python3.7/site-packages/playwright/connection.py", line 39, in send
    result = await callback.future
playwright.helper.Error: Protocol error (Page.printToPDF): PrintToPDF is not implemented

So it appears PrintToPDF is implemented partially, as it only seems to work when running headless.

mxschmitt commented 3 years ago

it's an upstream limitation: https://playwright.dev/#version=v1.6.1&path=docs%2Fapi.md&q=pagepdfoptions

NOTE Generating a pdf is currently only supported in Chromium headless.

You can use xvfb-run to run your browsers headful to make screenshots in Docker etc. Example:

xvfb-run node foo.js

an inside foo.js you start Chromium headless: false

jtrip commented 3 years ago

Thanks for the reference and tip! I'll go ahead and close this for now then. 😄

dufferzafar commented 1 year ago

@mxschmitt Has PrintToPdf been implemented now?

mxschmitt commented 1 year ago

Its still a Chromium only feature: https://playwright.dev/docs/api/class-page#page-pdf