TG-35 : Typo
This commit is contained in:
commit
ed0f2b1808
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'))
|
@ -5,11 +5,11 @@ from hashlib import sha512
|
|||||||
SIMPLE_CHARS = string.ascii_letters + string.digits
|
SIMPLE_CHARS = string.ascii_letters + string.digits
|
||||||
|
|
||||||
|
|
||||||
def get_random_string(length=24):
|
def get_random_string(length=32):
|
||||||
return ''.join(random.choice(SIMPLE_CHARS) for i in range(length))
|
return ''.join(random.choice(SIMPLE_CHARS) for i in range(length))
|
||||||
|
|
||||||
|
|
||||||
def get_random_hash(length=24):
|
def get_random_hash(length=64):
|
||||||
hash = sha512()
|
hash = sha512()
|
||||||
hash.update(get_random_string())
|
hash.update(get_random_string())
|
||||||
return hash.hexdigest()[:length]
|
return hash.hexdigest()[:length]
|
||||||
|
@ -76,8 +76,6 @@ class RunTests(Command):
|
|||||||
"""Seed the db """
|
"""Seed the db """
|
||||||
def run(self):
|
def run(self):
|
||||||
Config.configure_app(config="test")
|
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_loader = unittest.defaultTestLoader
|
||||||
test_runner = unittest.TextTestRunner()
|
test_runner = unittest.TextTestRunner()
|
||||||
test_suite = test_loader.discover('tests')
|
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.
53
backend/tests/tools/pdfjinja/testInsertTemplate.py
Normal file
53
backend/tests/tools/pdfjinja/testInsertTemplate.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from pdfjinja import PdfJinja
|
||||||
|
|
||||||
|
from app.tools.pdfjinja.insertTemplate import remplirTemplate
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
remplirTemplate(self.datadir,"sample.pdf",self.datadir,"output.pdf",self.data)
|
||||||
|
output = self.pdfjinja(self.data)
|
||||||
|
outfile = BytesIO()
|
||||||
|
output.write(outfile)
|
||||||
|
outfile.seek(0)
|
||||||
|
self.assertTrue(len(outfile.read()) > 0, "Output PDF is not empty.")
|
||||||
|
self.assertTrue(Path(self.datadir+"/output.pdf").is_file(),"Pdf généré inexistant")
|
||||||
|
os.remove(self.datadir+"/output.pdf")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
@ -7,7 +7,7 @@ skipsdist = True
|
|||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
commands=
|
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 report -m
|
||||||
coverage xml
|
coverage xml
|
||||||
coverage html
|
coverage html
|
||||||
|
Reference in New Issue
Block a user