Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN python3 -m venv --system-site-packages /opt/translate/venv
COPY requirements.txt constraints.txt /opt/translate/
RUN /opt/translate/venv/bin/pip3 install -r /opt/translate/requirements.txt -c /opt/translate/constraints.txt

RUN /opt/translate/venv/bin/python3 -c 'import pyppeteer.command; pyppeteer.command.install()'
RUN /opt/translate/venv/bin/playwright install

COPY trans/static/fonts/ /usr/local/share/fonts/

Expand Down
4 changes: 2 additions & 2 deletions Translation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@

# Settings related to production of PDFs from the Markdown sourcs

PYPPETEER_PDF_OPTIONS = {
PLAYWRIGHT_PDF_OPTIONS = {
'margin': {
'left': '0.75in',
'right': '0.75in',
'top': '0.62in',
'bottom': '1in',
},
'format': 'A4',
'printBackground': True,
'print_background': True,
}

# This is standard A4
Expand Down
1 change: 0 additions & 1 deletion constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pyasn1_modules==0.4.0
pycairo==1.20.1
pyee==11.1.0
Pygments==2.18.0
pyppeteer==2.0.0
python-dateutil==2.9.0.post0
python-markdown-math==0.8
pytz==2024.1
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ ipython
moratab
openpyxl
pikepdf
playwright
psycopg2
pycairo
pyppeteer
python-markdown-math
pytz
raven
Expand Down
21 changes: 10 additions & 11 deletions trans/utils/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.http import HttpResponse
from django.template.loader import render_to_string

from pyppeteer import launch
from playwright.sync_api import sync_playwright, Playwright

from trans.context_processors import ioi_settings
from trans.models import Translation, User
Expand Down Expand Up @@ -57,7 +57,7 @@ def build_pdf(translation: Translation, task_type: str) -> str:
with TemporaryDirectory(dir=_temp_dir_path()) as temp_dir:
temp_dir_path = Path(temp_dir)
loop = asyncio.get_event_loop()
browser_pdf_path = loop.run_until_complete(_convert_html_to_pdf(html, temp_dir_path))
browser_pdf_path = _convert_html_to_pdf(html, temp_dir_path)
transformed_pdf_path = temp_dir_path / 'transformed.pdf'
if settings.USE_CPDF:
_add_page_numbers_to_pdf(browser_pdf_path, transformed_pdf_path, task.name)
Expand Down Expand Up @@ -146,21 +146,20 @@ def _temp_dir_path() -> Path:
return temp_path


async def _convert_html_to_pdf(html: str, temp_dir_path: Path) -> Path:
def _convert_html_to_pdf(html: str, temp_dir_path: Path) -> Path:
html_file = temp_dir_path / 'source.html'
pdf_file = temp_dir_path / 'browser.pdf'

try:
with open(html_file, 'w') as f:
f.write(html)
browser = await launch(options={'args': ['--no-sandbox']})
page = await browser.newPage()
await page.goto('file://{}'.format(html_file), {
'waitUntil': 'networkidle2',
})
await page.emulateMedia('print')
await page.pdf({'path': str(pdf_file), **settings.PYPPETEER_PDF_OPTIONS})
await browser.close()
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
page.goto('file://{}'.format(html_file), wait_until = "networkidle")
# page.emulateMedia('print')
page.pdf(path = str(pdf_file), **settings.PLAYWRIGHT_PDF_OPTIONS)
browser.close()
except Exception as e:
logger.error(e)

Expand Down