devind-team / devind-django-helpers

Django helpers.
MIT License
0 stars 1 forks source link

Document generator improvement #7

Open SquakR opened 2 years ago

SquakR commented 2 years ago

The current implementation of the document generator which generates a Word or Pdf document depending on the template requires manual unzipping a Word file and extracting a xml document. Generating a Word or Pdf file from a template is a common task that can be useful to many users. Therefore, this feature can be extracted in another independent library with the following API.

document_generator = DocumentGenerator(
    template='document_template.docx',
    name='my_document',
    output_dir='~/documents',
    variables={'variable1': 'value1', 'variable2': 'value2'}
)
word_path1 = document_generator.generate_docx() 
# word_path1 = '/home/user/documents/my_document.docx'
word_path2 = document_generator.generate_docx(
    name='another_document',
    output_dir='~/another_documents/',
    variables={'variable1': 'value1', 'variable2': 'value2'}
)
# word_path2 = '/home/user/documents/another_document.docx'
pdf_path = document_generator.generate_pdf()
# pdf_path = '/home/user/documents/my_document.pdf'
SquakR commented 2 years ago

Also, instead of our own solution, we should consider the python-docx-template library. If we use this library, we only need to implement the conversion from docx to pdf because there is no ready solution for Linux.

word_to_pdf(input_path='~/documents/document.docx', output_path='~/documents/document.pdf')

The only problem is that the library uses Jinja2 instead of Django templates.