Package: edc-pdf-reports
edc-pdf-reportsReportlab/PDF classes for clinicedc/edc projectsOverviewThis module offers functionality to link a PDF report to a model registered with ModelAdmin.TheCrfPdfReportclass 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 alsoPdfIntermediateViewandPrintPdfReportView.For this to work, you need to:create a pdf report class;declare the model with thePdfReportModelMixinand set thepdf_report_clsattr on the model;declare the model’s ModelAdmin class withPdfButtonModelAdminMixin;addprint_to_pdf_actionto 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.DeathReport as an exampleedc_adverse_eventhas this configured for itsDeathReportmodel. Let’s use this as an example.Create theDeathReportmodel:# models.pyclassDeathReport(PdfReportModelMixin,BaseUuidModel):pdf_report_cls=DeathPdfReportCreate theDeathPdfReportclass.DeathPdfReportinherits fromCrfPdfReport. Link themodelandchangelist_url`to this PDF report class.# death_pdf_report.pyclassDeathPdfReport(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")defget_report_story(self,**kwargs):...Declare the ModelAdmin class withPdfButtonModelAdminMixin:# admin.pyclassDeathReportModelAdmin(PdfButtonModelAdminMixin,DeathReportModelAdminMixin):actions=[print_to_pdf_action]list_display=["subject_identifier","pdf_button",...]Update your url patterns:# urls.pyurl_patterns=[...,*paths_for_urlpatterns("edc_pdf_reports"),...]Add tosettings:# settings.pyINSTALLED_APPS=[...,"edc_pdf_reports.apps.AppConfig"...]Your changelist will have the new column “PDF” and the print as pdf action will be available.The intermediate page, linked from the changelist, will look like this: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 viewThe viewPrintPdfReportViewuses functionwrite_queryset_to_secure_pdfto create a PDF.
You can access this function directly.For example:importmempassimporttempfilefrompathlibimportPathfromdjango.contrib.auth.modelsimportUserfromedc_pdf_reports.utilsimportwrite_queryset_to_secure_pdf,write_model_to_insecure_pdffromeffect_ae.modelsimportDeathReportdir=tempfile.mkdtemp()p=Path(dir)qs=DeathReport.objects.all()user=User.objects.get(username="erikvw")# create a secure PDF file for the querysetq=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 instanceq=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)