44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
import pdfkit
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
class ContractCreator:
|
|
|
|
def __init__(self):
|
|
template_loader = FileSystemLoader(searchpath="./")
|
|
template_env = Environment(loader=template_loader)
|
|
self.template = template_env.get_template('template.html')
|
|
|
|
self.pdfkit_options = {
|
|
'page-size': 'A4',
|
|
'margin-top': '0.75in',
|
|
'margin-right': '0.75in',
|
|
'margin-bottom': '0.75in',
|
|
'margin-left': '0.75in',
|
|
'encoding': "UTF-8",
|
|
'custom-header' : [
|
|
('Accept-Encoding', 'gzip')
|
|
]
|
|
}
|
|
|
|
def fill_template(self, **kwargs):
|
|
return self.template.render(**kwargs)
|
|
|
|
def create_pdf(self, out_f, fields_dict):
|
|
html_str = self.fill_template(**fields_dict)
|
|
pdfkit.from_string(html_str, out_f, options=self.pdfkit_options)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
test_data = {
|
|
'ime_priimek': 'Testko Tester',
|
|
'naslov': 'Testovci 10',
|
|
'posta': '1123',
|
|
'kontakt_narocnik': 'Testica Testkovič',
|
|
'kontakt_imetnikpravic': 'Testko Tester',
|
|
'date': '16.2.2021'
|
|
}
|
|
|
|
contract_creator = ContractCreator()
|
|
contract_creator.create_pdf('out.pdf', test_data)
|