TG-48 ajout de pdfjinja
This commit is contained in:
parent
228af15b44
commit
cbf9191138
25
backend/app/tools/pdfjinja/README.md
Normal file
25
backend/app/tools/pdfjinja/README.md
Normal file
@ -0,0 +1,25 @@
|
||||
#pdfjinja 1.0.0
|
||||
|
||||
##Dependencies :
|
||||
|
||||
python-dev
|
||||
python-pip
|
||||
libtiff5-dev
|
||||
libjpeg8-dev
|
||||
zlib1g-dev
|
||||
libfreetype6-dev
|
||||
liblcms2-dev
|
||||
libwebp-dev
|
||||
tcl8.6-dev
|
||||
tk8.6-dev
|
||||
python-tk
|
||||
pdftk
|
||||
libmagickwand-dev
|
||||
|
||||
##Installation:
|
||||
|
||||
pip install pdfjinja
|
||||
|
||||
##Test :
|
||||
|
||||
pdfjinja -h
|
24
backend/app/tools/pdfjinja/insertTemplate.py
Normal file
24
backend/app/tools/pdfjinja/insertTemplate.py
Normal file
@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" Script python qui remplie les pdf basés sur un template jinja. """
|
||||
|
||||
import os
|
||||
|
||||
from pdfjinja import PdfJinja
|
||||
|
||||
def remplirTemplate (dirname_template, pdf_template, dirname_output_file, pdf_output,dictionnaire):
|
||||
"""
|
||||
Fonction qui permet de remplir un pdf template
|
||||
:param dirname_template: chemin du fichier de template
|
||||
:param pdf_template: nom du fichier de template
|
||||
:param dirname_output_file: chemin des pdf généré
|
||||
:param pdf_output: nom du fichier pdf à générer
|
||||
:param dictionnaire: dictionnaire contenant le nom des textfields des pdf ainsi que leurs valeurs
|
||||
:return:
|
||||
"""
|
||||
template_pdf_file = os.path.join(dirname_template, pdf_template)
|
||||
template_pdf = PdfJinja(template_pdf_file)
|
||||
|
||||
rendered_pdf = template_pdf(dictionnaire)
|
||||
|
||||
output_file = os.path.join(dirname_output_file, pdf_output)
|
||||
rendered_pdf.write(open(output_file, 'wb'))
|
@ -69,8 +69,6 @@ class RunTests(Command):
|
||||
"""Seed the db """
|
||||
def run(self):
|
||||
Config.configure_app(config="test")
|
||||
os.system("python manage.py -t db downgrade base")
|
||||
os.system("python manage.py -t db upgrade")
|
||||
test_loader = unittest.defaultTestLoader
|
||||
test_runner = unittest.TextTestRunner()
|
||||
test_suite = test_loader.discover('tests')
|
||||
|
0
backend/tests/__init__.py
Normal file
0
backend/tests/__init__.py
Normal file
BIN
backend/tests/tools/pdfjinja/sample.pdf
Normal file
BIN
backend/tests/tools/pdfjinja/sample.pdf
Normal file
Binary file not shown.
BIN
backend/tests/tools/pdfjinja/sample_backup.pdf
Normal file
BIN
backend/tests/tools/pdfjinja/sample_backup.pdf
Normal file
Binary file not shown.
47
backend/tests/tools/pdfjinja/testInsertTemplate.py
Normal file
47
backend/tests/tools/pdfjinja/testInsertTemplate.py
Normal file
@ -0,0 +1,47 @@
|
||||
import json
|
||||
import os
|
||||
import unittest
|
||||
from io import BytesIO
|
||||
|
||||
from pdfjinja import PdfJinja
|
||||
|
||||
|
||||
class insertTemplateTestCase(unittest.TestCase):
|
||||
|
||||
datadir = os.path.join(os.path.dirname(__file__))
|
||||
|
||||
def setUp(self):
|
||||
pdffile = os.path.join(self.datadir, "sample.pdf")
|
||||
self.data = {
|
||||
'firstName': 'Renan',
|
||||
'lastName': 'Husson',
|
||||
'address': {
|
||||
'street': '24 rue de la pommes',
|
||||
'apt': 'C317',
|
||||
'city': 'TOULOUSE',
|
||||
'zipcode': 31000
|
||||
},
|
||||
'universite':'Jean Jaures',
|
||||
'spirit': 'Panda',
|
||||
'evil': True,
|
||||
'language': {
|
||||
'french': True,
|
||||
'esperento': True
|
||||
}
|
||||
}
|
||||
self.pdfjinja = PdfJinja(pdffile)
|
||||
|
||||
def tearDown(self):
|
||||
del self.data
|
||||
del self.pdfjinja
|
||||
|
||||
def test_render(self):
|
||||
output = self.pdfjinja(self.data)
|
||||
outfile = BytesIO()
|
||||
output.write(outfile)
|
||||
outfile.seek(0)
|
||||
self.assertTrue(len(outfile.read()) > 0, "Output PDF is not empty.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -7,7 +7,7 @@ skipsdist = True
|
||||
|
||||
[testenv]
|
||||
commands=
|
||||
coverage run --source app/api --omit app/api/*/model.py manage.py runtests
|
||||
coverage run --source app/api app/tools --omit app/api/*/model.py manage.py runtests
|
||||
coverage report -m
|
||||
coverage xml
|
||||
coverage html
|
||||
|
Reference in New Issue
Block a user