Skip to content

Commit

Permalink
Switch from pyppeteer to playwright.
Browse files Browse the repository at this point in the history
Fixes #1262.

This doesn't really improve a whole lot. playwright notably disables the
Chromium sandbox by default, so we don't need to do that explicitly any more,
but other than that the code looks largely the same.

The generated PDFs look slightly different, I think because playwright
install-deps installs more fonts, and we then end up using Arial for the
receipts.

Because this is substantially invasive and in the critical path for generating
receipts I don't think this is worthwhile merging for 2024.
  • Loading branch information
lukegb authored and marksteward committed Apr 19, 2024
1 parent ad1bf14 commit 1e42447
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 195 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ FROM ghcr.io/emfcamp/website-base-dev:latest
COPY pyproject.toml poetry.lock Makefile /app/
WORKDIR /app

RUN poetry install && poetry run pyppeteer-install
RUN poetry install \
&& poetry run playwright install-deps \
&& poetry run playwright install chromium

ENV SHELL=/bin/bash
ENTRYPOINT ["./docker/dev_entrypoint.sh"]
5 changes: 4 additions & 1 deletion Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ FROM ghcr.io/emfcamp/website-base:latest
COPY . /app/
WORKDIR /app

RUN poetry install --no-dev && poetry run pyppeteer-install && rm -Rf ./static
RUN poetry install --no-dev \
&& poetry run playwright install-deps \
&& poetry run playwright install chromium \
&& rm -Rf ./static

COPY --from=static /app/static /app/static

Expand Down
43 changes: 18 additions & 25 deletions apps/common/receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from lxml import etree
import asyncio

from flask import render_template, current_app as app
from flask import render_template
from markupsafe import Markup
from pyppeteer.launcher import launch
from playwright.async_api import async_playwright
import barcode
from barcode.writer import ImageWriter, SVGWriter
import segno
Expand Down Expand Up @@ -63,29 +63,22 @@ def render_pdf(url, html):
# you're running a dev server, use app.run(processes=2)

async def to_pdf():
browser = await launch(
# Handlers don't work as we're not in the main thread.
handleSIGINT=False,
handleSIGTERM=False,
handleSIGHUP=False,
# --no-sandbox is necessary as we're running as root (in docker!)
args=["--no-sandbox"],
)
page = await browser.newPage()

async def request_intercepted(request):
app.logger.debug("Intercepted URL: %s", request.url)
if request.url == url:
await request.respond({"body": html})
else:
await request.continue_()

page.on("request", lambda r: asyncio.ensure_future(request_intercepted(r)))
await page.setRequestInterception(True)

await page.goto(url)
pdf = await page.pdf(format="A4")
await browser.close()
async with async_playwright() as p:
browser = await p.chromium.launch(
# Handlers don't work as we're not in the main thread.
handle_sigint=False,
handle_sigterm=False,
handle_sighup=False,
)
context = await browser.new_context()
page = await browser.new_page()

await page.route(url, lambda route: route.fulfill(body=html))
await page.goto(url)
pdf = await page.pdf(format="A4")
await page.close()
await context.close()
await browser.close()
return pdf

loop = asyncio.new_event_loop()
Expand Down
6 changes: 4 additions & 2 deletions docker/Dockerfile.base
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ COPY pyproject.toml poetry.lock /app/
# doesn't confuse poetry.
ENV POETRY_VIRTUALENVS_IN_PROJECT=false

# All the X11 packages are required for chromium/pyppetteer which we use for
# All the X11 packages are required for chromium/playwright which we use for
# generating PDFs.
#
# It would be nice to be able to build an image without build tools and dev
Expand All @@ -26,5 +26,7 @@ RUN apt-get update && \
libxrandr2 libasound2 libatk1.0-0 libatk-bridge2.0-0 libgtk-3-0 libzbar0 \
coinor-cbc curl && \
pip3 install poetry==1.7.1 && \
poetry install && poetry run pyppeteer-install && \
poetry install && \
poetry run playwright install-deps && \
poetry run playwright install chromium && \
rm -rf /var/lib/apt/lists/*
2 changes: 1 addition & 1 deletion logging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ loggers:
propagate: false
iso8601:
level: WARN
pyppeteer:
playwright:
level: WARN
websockets.protocol:
level: WARN
Expand Down
Loading

0 comments on commit 1e42447

Please sign in to comment.