From 930f8bd5e59b8579c6087fc1a05c67acb6a935fa Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 26 Jun 2024 12:30:51 +0700 Subject: [PATCH] add: document template passwords for PDF exports --- .../0008_documenttemplate_pdf_password.py | 23 +++++++++++++++++++ .../contrib/document_templates/models.py | 15 ++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 basxbread/contrib/document_templates/migrations/0008_documenttemplate_pdf_password.py diff --git a/basxbread/contrib/document_templates/migrations/0008_documenttemplate_pdf_password.py b/basxbread/contrib/document_templates/migrations/0008_documenttemplate_pdf_password.py new file mode 100644 index 00000000..88c91483 --- /dev/null +++ b/basxbread/contrib/document_templates/migrations/0008_documenttemplate_pdf_password.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.7 on 2024-06-26 05:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("document_templates", "0007_alter_documenttemplatevariable_value"), + ] + + operations = [ + migrations.AddField( + model_name="documenttemplate", + name="pdf_password", + field=models.CharField( + blank=True, + help_text="An optional password that will be set on generated PDFs", + max_length=2048, + verbose_name="PDF-Password", + ), + ), + ] diff --git a/basxbread/contrib/document_templates/models.py b/basxbread/contrib/document_templates/models.py index 2afc03ce..f637ca1b 100644 --- a/basxbread/contrib/document_templates/models.py +++ b/basxbread/contrib/document_templates/models.py @@ -40,6 +40,13 @@ class DocumentTemplate(models.Model): filename_template = models.TextField(_("Filename template"), blank=True) filename_template.formfield_kwargs = {"widget": forms.Textarea(attrs={"rows": 1})} + pdf_password = models.CharField( + _("PDF-Password"), + blank=True, + max_length=2048, + help_text=_("An optional password that will be set on generated PDFs"), + ) + def default_context(self): return {"now": DateFormat(now())} @@ -106,10 +113,14 @@ def generate_document_pdf(self, object): file.write(content.read()) subprocess.run( # nosec [ - shutil.which("libreoffice") + shutil.which("soffice") or "false", # statisfies mypy, since which may return None "--convert-to", - "pdf", + ( + f'pdf:draw_pdf_Export:{{"EncryptFile":{{"type":"boolean","value":"true"}},"DocumentOpenPassword":{{"type":"string","value":"{self.pdf_password}"}}}}' + if self.pdf_password + else "pdf" + ), file.name, "--outdir", tmpdir,