Commit 92ba1f48 authored by Pierre Dittgen's avatar Pierre Dittgen
Browse files

Restore chromium headless as default PDF renderer

parent 3b486e57
......@@ -3,6 +3,10 @@ LABEL maintainer="admin-validata@jailbreak.paris"
EXPOSE 5000
RUN apt-get update && \
apt-get install --yes chromium && \
apt-get install --yes xfonts-100dpi xfonts-75dpi xfonts-base
RUN pip install --no-cache-dir gunicorn==20.0.4
ARG uid=1000
......
"""PDF report rendering utilities."""
import json
import logging
import subprocess
import tempfile
from abc import ABC, abstractmethod
from datetime import datetime
from pathlib import Path
import requests
......@@ -25,8 +29,9 @@ class PDFRenderer(ABC):
config.BROWSERLESS_API_URL, config.BROWSERLESS_API_TOKEN
)
log.info("No PDF renderer available")
return None
# fallback
log.info("Creating Chromium headless PDF renderer")
return ChromiumHeadlessPDFRenderer()
class BrowserlessPDFRenderer(PDFRenderer):
......@@ -71,3 +76,37 @@ class BrowserlessPDFRenderer(PDFRenderer):
len(r.content),
)
return r.content
class ChromiumHeadlessPDFRenderer(PDFRenderer):
"""Chromium implementation."""
def render(self, url: str) -> bytes:
# Create temp file to save validation report
# This temp file will be automatically deleted on context exit
with tempfile.NamedTemporaryFile(
prefix="validata_{}_report_".format(datetime.now().timestamp()),
suffix=".pdf",
) as tmpfile:
tmp_pdf_report = Path(tmpfile.name)
# Use chromium headless to generate PDF from validation report page
cmd = [
"chromium",
"--headless",
"--no-sandbox",
"--disable-gpu",
"--disable-dev-shm-usage",
f"--print-to-pdf={tmp_pdf_report}",
url,
]
result = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
if result.returncode != 0:
output = result.stdout.decode("utf-8")
log.error("Command %r returned an error: %r", cmd, output)
raise ValueError("Erreur lors de la génération du rapport PDF")
return tmp_pdf_report.read_bytes()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment