Commit 47fdc89e authored by Anton Akhmerov's avatar Anton Akhmerov

Merge branch '329-mail-exception' into 'master'

Use reportlab for generating solution pdf

Closes #329

See merge request !166
parents b0ba9dd7 d06e7f2c
Pipeline #18494 passed with stages
in 5 minutes and 23 seconds
...@@ -8,10 +8,12 @@ from email.mime.base import MIMEBase ...@@ -8,10 +8,12 @@ from email.mime.base import MIMEBase
from email import encoders from email import encoders
import jinja2 import jinja2
from wand.image import Image
from reportlab.pdfgen import canvas
from .database import Submission from .database import Submission
from . import statistics from . import statistics
from .api.exams import PAGE_FORMATS
def solution_pdf(exam_id, student_id): def solution_pdf(exam_id, student_id):
...@@ -20,17 +22,17 @@ def solution_pdf(exam_id, student_id): ...@@ -20,17 +22,17 @@ def solution_pdf(exam_id, student_id):
pages = sorted((p for s in subs for p in s.pages), key=(lambda p: p.number)) pages = sorted((p for s in subs for p in s.pages), key=(lambda p: p.number))
pages = [p.path for p in pages] pages = [p.path for p in pages]
with Image() as output_pdf: from flask import current_app
for filepath in pages: page_format = current_app.config.get('PAGE_FORMAT', 'A4') # TODO Remove default value
with Image(filename=filepath) as page: page_size = PAGE_FORMATS[page_format]
output_pdf.sequence.append(page)
output_pdf.format = 'pdf'
result = BytesIO()
output_pdf.save(file=result) result = BytesIO()
pdf = canvas.Canvas(result, pagesize=page_size)
for page in pages:
pdf.drawImage(page, 0, 0, width=page_size[0], height=page_size[1])
pdf.showPage()
pdf.save()
result.seek(0) result.seek(0)
return result return result
......
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