Skip to content

Commit

Permalink
add: document template passwords for PDF exports
Browse files Browse the repository at this point in the history
  • Loading branch information
saemideluxe committed Jun 26, 2024
1 parent b26203d commit 930f8bd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
15 changes: 13 additions & 2 deletions basxbread/contrib/document_templates/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 930f8bd

Please sign in to comment.