Skip to main content

pypi actions codecov downloads

edc-pdf-reports

Reportlab/PDF classes for clinicedc/edc projects

Overview

This module offers functionality to link a PDF report to a model registered with ModelAdmin.

The CrfPdfReport class links a PDF report to a model registered with ModelAdmin. A link as is added to the changelist that opens an intermediate page to ask for a password. From the intermediate page a secure file is downloaded into the browser. See also PdfIntermediateView and PrintPdfReportView.

For this to work, you need to:

  • create a pdf report class;

  • declare the model with the PdfReportModelMixin and set the pdf_report_cls attr on the model;

  • declare the model’s ModelAdmin class with PdfButtonModelAdminMixin;

  • add print_to_pdf_action to Modeladmin.actions (required to print one or more pdfs using actions);

  • add “pdf_button” to the list_display (required for pdf button to appear on each row);

  • update your app’s urls;

  • add edc_pdf_reports to INSTALLED_APPS.

Your changelist will include options for printing one or many PDF reports into a password protected and secure PDF file.

If you are using this module outside of a clinicedc/edc project, you need to update two settings attributes:

# settings.py
# tells edc_pdf_reports to not import two clinicedc modules
EDC_PDF_REPORTS_INTEGRATE_EDC = False
# points
EDC_PDF_REPORTS_TEMPLATES = {"pdf_intermediate": "edc_pdf_reports/generic_pdf_intermediate.html"}

DeathReport as an example

edc_adverse_event has this configured for its DeathReport model. Let’s use this as an example.

Create the DeathReport model:

# models.py

class DeathReport(PdfReportModelMixin, BaseUuidModel):

    pdf_report_cls = DeathPdfReport

Create the DeathPdfReport class. DeathPdfReport inherits from CrfPdfReport. Link the model and changelist_url` to this PDF report class.

# death_pdf_report.py

class DeathPdfReport(CrfPdfReport):
    model = f"{get_adverse_event_app_label()}.deathreport"
    changelist_url = (
        f"{get_adverse_event_app_label()}_admin:{get_adverse_event_app_label()}_"
        "deathreport_changelist"
    )

    def get_report_story(self, **kwargs):
        ...

Declare the ModelAdmin class with PdfButtonModelAdminMixin:

# admin.py

@admin.action(permissions=["view"], description="Print Death Reports as PDF")
def print_to_pdf_action(modeladmin, request, queryset):
    return print_selected_to_pdf_action(modeladmin, request, queryset)


class DeathReportModelAdmin(PdfButtonModelAdminMixin, DeathReportModelAdminMixin):
    actions = [print_to_pdf_action]
    list_display = ["subject_identifier", "pdf_button", ...]

Update your url patterns:

# urls.py
url_patterns = [
    ...,
    *paths_for_urlpatterns("edc_pdf_reports"),
    ...]

Add to settings:

# settings.py
INSTALLED_APPS = [
    ...,
    "edc_pdf_reports.apps.AppConfig"
    ...]

Your changelist will have the new column “PDF” and the print as pdf action will be available.

ChangeList

The intermediate page, linked from the changelist, will look like this:

Intermediate page

Note the passphrase and click “Create File”. The file will be created in the view and downloaded by the browser.

Creating a PDF file outside of the view

The view PrintPdfReportView uses function write_queryset_to_secure_pdf to create a PDF. You can access this function directly.

For example:

import mempass
import tempfile
from pathlib import Path
from django.contrib.auth.models import User
from edc_pdf_reports.utils import write_queryset_to_secure_pdf, write_model_to_insecure_pdf
from effect_ae.models import DeathReport

dir = tempfile.mkdtemp()
p = Path(dir)
qs = DeathReport.objects.all()
user = User.objects.get(username="erikvw")

# create a secure PDF file for the queryset
q = p / "death_reports_secure.pdf"
password = mempass.mkpassword(2)
buffer = write_queryset_to_secure_pdf(queryset=qs, password=password, user=user)
q.write_bytes(buffer.getbuffer())
print(q)

# create an insecure PDF file for one model instance
q = p / "death_reports_insecure.pdf"
model_obj = qs[0]
buffer = write_model_to_insecure_pdf(model_obj, user=user)
q.write_bytes(buffer.getbuffer())
print(q)

Add watermark to report

When testing, you can add a watermark to the report. In your test settings set the following:

EDC_PDF_REPORTS_WATERMARK_WORD = "SAMPLE"
EDC_PDF_REPORTS_WATERMARK_FONT = ("Helvetica", 100)

The watermark prints at a 45 degree rotation across the center of each page.

Metadata

Release files for edc-pdf-reports 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for edc-pdf-reports 1.0.2
File Size Uploaded
edc_pdf_reports-1.0.2.tar.gz 124.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for edc-pdf-reports 1.0.2
File Interpreter ABI Platform
edc_pdf_reports-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 171.6 kB

Release files / edc_pdf_reports-1.0.2.tar.gz

Download URL edc_pdf_reports-1.0.2.tar.gz
Size 124.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a2bdd7e1bc1a31c25975cea72fbd04cd8ec838d96bbc922fa5fb6bd450e670ed
BLAKE2b-256 checksum
How to use checksums
1c04032e3f5a8b383944c4987321842393e0d96be0f981b4b4c9ca65dab7789d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / edc_pdf_reports-1.0.2-py3-none-any.whl

Download URL edc_pdf_reports-1.0.2-py3-none-any.whl
Size 47.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87ae97dfc0b130209a39cc0417ed370b83ce302c33db60ffa42fe9ae7f6fffbf
BLAKE2b-256 checksum
How to use checksums
149ec60c8a4396879ecd476e0527effe317468c19f4ab3ea32e343436990b361
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.25

2 release files

0.3.24

2 release files

0.3.23

2 release files

0.3.22

2 release files

0.3.21

2 release files

0.3.20

2 release files

0.3.19

2 release files

0.3.14

2 release files

0.3.13

2 release files

0.3.12

2 release files

0.3.9

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page