Skip to content

Commit

Permalink
add build method
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Nov 19, 2024
1 parent 10eb5b7 commit e760086
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
5 changes: 4 additions & 1 deletion edc_pdf_reports/numbered_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class NumberedCanvas(canvas.Canvas):
static_footer_text = None
footer_row_height = 25

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -30,5 +31,7 @@ def draw_page_number(self, page_count):
width, _ = A4
self.setFont("Helvetica", 6)
self.drawCentredString(
width / 2, 25, "Page %d of %d" % (self.getPageNumber(), page_count)
width / 2,
self.footer_row_height,
"Page %d of %d" % (self.getPageNumber(), page_count),
)
31 changes: 27 additions & 4 deletions edc_pdf_reports/report.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

from abc import ABC
from typing import Type
from uuid import uuid4

from django.core.handlers.wsgi import WSGIRequest
from django.utils import timezone
from django_revision.revision import Revision
from edc_protocol.research_protocol_config import ResearchProtocolConfig
from edc_utils.date import to_local
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import (
Expand All @@ -18,6 +20,8 @@
from reportlab.lib.units import cm
from reportlab.platypus import SimpleDocTemplate

from edc_pdf_reports.numbered_canvas import NumberedCanvas


class ReportError(Exception):
def __init__(self, message, code=None):
Expand All @@ -42,17 +46,30 @@ def __init__(
header_line: str | None = None,
filename: str | None = None,
request: WSGIRequest | None = None,
numbered_canvas: Type[NumberedCanvas] | None = None,
footer_row_height: int | None = None,
):
self._styles = None
self.request = request
self.page = page or self.default_page

self.filename = filename or f"{uuid4()}.pdf"
self.footer_row_height = footer_row_height or 25
self.numbered_canvas = numbered_canvas

if not header_line:
header_line = ResearchProtocolConfig().institution
self.header_line = header_line

def build(self, response):
doctemplate = self.document_template(response, **self.page)
story = self.get_report_story()
doctemplate.build(
story,
onFirstPage=self.on_first_page,
onLaterPages=self.on_later_pages,
canvasmaker=self.numbered_canvas,
)

@property
def report_filename(self) -> str:
return self.filename
Expand Down Expand Up @@ -81,16 +98,22 @@ def on_first_page(self, canvas, doc):

def on_later_pages(self, canvas, doc):
"""Callback for onLaterPages"""
self.draw_header(canvas, doc)
self.draw_footer(canvas, doc)

def draw_header(self, canvas, doc):
pass

def draw_footer(self, canvas, doc):
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name="header", fontSize=6, alignment=TA_CENTER))
width, _ = A4
canvas.setFontSize(6)
timestamp = timezone.now().strftime("%Y-%m-%d %H:%M")
canvas.drawRightString(width - len(timestamp) - 20, 25, f"printed on {timestamp}")
canvas.drawString(35, 25, f"clinicedc {Revision().tag}")
timestamp = to_local(timezone.now()).strftime("%Y-%m-%d %H:%M")
canvas.drawRightString(
width - len(timestamp) - 20, self.footer_row_height, f"printed on {timestamp}"
)
canvas.drawString(35, self.footer_row_height, f"clinicedc {Revision().tag}")

@property
def styles(self):
Expand Down

0 comments on commit e760086

Please sign in to comment.