Skip to content
Snippets Groups Projects
Verified Commit d397a439 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Use non-optional Celery for PDF generation

parent 707715bb
No related branches found
No related tags found
1 merge request!464Resolve "Support PDF generation via a headless Chromium running in the background using celery"
import glob
import os
import subprocess # noqa
from typing import Union
from django.http.request import HttpRequest
from django.http.response import HttpResponse
......@@ -11,21 +10,16 @@ from django.utils.translation import gettext as _
from celery_progress.backend import ProgressRecorder
from aleksis.core.celery import app
from aleksis.core.settings import MEDIA_ROOT, MEDIA_URL, STATIC_ROOT
from aleksis.core.util.core_helpers import (
DummyRecorder,
celery_optional,
celery_optional_progress,
is_celery_enabled,
make_temp_file,
path_and_rename,
)
@celery_optional_progress
def generate_pdf(recorder: Union[ProgressRecorder, DummyRecorder], html_code: str, pdf_path: str):
from aleksis.core.util.celery_progress import recorded_task
from aleksis.core.util.core_helpers import make_temp_file, path_and_rename
@recorded_task
def generate_pdf(html_code: str, pdf_path: str, recorder: ProgressRecorder):
"""Generate a PDF file by rendering the HTML code using electron-pdf."""
recorder.total = 1
recorder.set_progress(0, 1)
# Replace /static with STATIC_ROOT to get local file system paths
html_code = html_code.replace("/static", STATIC_ROOT)
......@@ -37,13 +31,17 @@ def generate_pdf(recorder: Union[ProgressRecorder, DummyRecorder], html_code: st
# Start a X framebuffer and run electron-pdf
os.environ["DISPLAY"] = ":99.0"
xfvb_process= subprocess.Popen(["Xvfb", ":99", "-screen", "0", "1024x768x24"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
xfvb_process = subprocess.Popen( # noqa
["Xvfb", ":99", "-screen", "0", "1024x768x24"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.run(["electron-pdf", path, pdf_path]) # noqa
xfvb_process.terminate()
os.remove(path)
recorder.set_progress(1)
recorder.set_progress(1, 1)
def render_pdf(request: HttpRequest, template_name: str, context: dict = None) -> HttpResponse:
......@@ -61,34 +59,30 @@ def render_pdf(request: HttpRequest, template_name: str, context: dict = None) -
html_template = render_to_string(template_name, context)
if is_celery_enabled():
result = generate_pdf(html_template, os.path.join(MEDIA_ROOT, pdf_path))
context = {
"title": _("Progress: Generate PDF file"),
"back_url": context.get("back_url", "index"),
"progress": {
"task_id": result.task_id,
"title": _("Generating PDF file …"),
"success": _("The PDF file has been generated successfully."),
"error": _("There was a problem while generating the PDF file."),
"redirect_on_success": MEDIA_URL + pdf_path,
},
"additional_button": {
"href": MEDIA_URL + pdf_path,
"caption": _("Download PDF"),
"icon": "picture_as_pdf",
},
}
# Render progress view
return render(request, "core/pages/progress.html", context)
else:
# Render PaperCSS view if Celery isn't enabled
return render(request, template_name, context)
@celery_optional
result = generate_pdf.delay(html_template, os.path.join(MEDIA_ROOT, pdf_path))
context = {
"title": _("Progress: Generate PDF file"),
"back_url": context.get("back_url", "index"),
"progress": {
"task_id": result.task_id,
"title": _("Generating PDF file …"),
"success": _("The PDF file has been generated successfully."),
"error": _("There was a problem while generating the PDF file."),
"redirect_on_success": MEDIA_URL + pdf_path,
},
"additional_button": {
"href": MEDIA_URL + pdf_path,
"caption": _("Download PDF"),
"icon": "picture_as_pdf",
},
}
# Render progress view
return render(request, "core/pages/progress.html", context)
@app.task
def clean_up_pdf_directory() -> None:
"""Clean up directory with generated PDF files."""
files = glob.glob(os.path.join(MEDIA_ROOT, "pdfs", "*.pdf"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment