-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from multiflexi/update_presenters
Update presenters
- Loading branch information
Showing
8 changed files
with
370 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,97 @@ | ||
"""PDF Presenter. | ||
Returns: | ||
dict: mime type and base64 encoded data of the generated PDF document | ||
""" | ||
import datetime | ||
import os | ||
import tempfile | ||
from base64 import b64encode | ||
import jinja2 | ||
import pdfkit | ||
from weasyprint import HTML | ||
|
||
from .base_presenter import BasePresenter | ||
from shared.schema.parameter import Parameter, ParameterType | ||
|
||
|
||
class PDFPresenter(BasePresenter): | ||
"""PDF Presenter class. | ||
Args: | ||
BasePresenter (class): Base presenter class | ||
""" | ||
|
||
type = "PDF_PRESENTER" | ||
name = "PDF Presenter" | ||
description = "Presenter for generating PDF documents" | ||
|
||
parameters = [ | ||
Parameter(0, "HEADER_TEMPLATE_PATH", "Header template path", "Path of header template file", | ||
ParameterType.STRING), | ||
Parameter(0, "BODY_TEMPLATE_PATH", "Body template path", "Path of body template file", | ||
ParameterType.STRING), | ||
Parameter(0, "FOOTER_TEMPLATE_PATH", "Footer template path", "Path of footer template file", | ||
ParameterType.STRING) | ||
] | ||
parameters = [Parameter(0, "PDF_TEMPLATE_PATH", "Template path", "Path of header template file", ParameterType.STRING)] | ||
|
||
parameters.extend(BasePresenter.parameters) | ||
|
||
def generate(self, presenter_input): | ||
"""Generate PDF document. | ||
Args: | ||
presenter_input (_type_): Parameters from settings | ||
Returns: | ||
dict: mime type and base64 encoded data of the generated PDF document | ||
""" | ||
try: | ||
temporary_directory = tempfile.gettempdir() + "/" | ||
output_body_html = temporary_directory + 'pdf_body.html' | ||
output_pdf = temporary_directory + 'pdf_report__' + datetime.datetime.now().strftime( | ||
"%d-%m-%Y_%H:%M") + '.pdf' | ||
output_html = temporary_directory + "pdf_body.html" | ||
output_pdf = temporary_directory + "pdf_report__" + datetime.datetime.now().strftime("%d-%m-%Y_%H:%M") + ".pdf" | ||
|
||
pdf_header_template = presenter_input.parameter_values_map['HEADER_TEMPLATE_PATH'] | ||
pdf_footer_template = presenter_input.parameter_values_map['FOOTER_TEMPLATE_PATH'] | ||
head, tail = os.path.split(presenter_input.parameter_values_map['BODY_TEMPLATE_PATH']) | ||
head, tail = os.path.split(presenter_input.parameter_values_map["PDF_TEMPLATE_PATH"]) | ||
|
||
input_data = BasePresenter.generate_input_data(presenter_input) | ||
|
||
env = jinja2.Environment(loader=jinja2.FileSystemLoader(head)) | ||
env.filters["strfdate"] = BasePresenter._filter_datetime | ||
body = env.get_template(tail) | ||
output_text = body.render(data=input_data) | ||
with open(output_body_html, 'w') as output_file: | ||
pdf = env.get_template(tail) | ||
output_text = pdf.render(data=input_data) | ||
with open(output_html, "w") as output_file: | ||
output_file.write(output_text) | ||
|
||
if not os.path.exists(temporary_directory): | ||
os.mkdir(temporary_directory) | ||
|
||
options = { | ||
'dpi': 500, | ||
'page-size': 'A4', | ||
'margin-top': '1.55in', | ||
'margin-right': '0.75in', | ||
'margin-bottom': '1.55in', | ||
'margin-left': '0.75in', | ||
'encoding': "UTF-8", | ||
'header-html': pdf_header_template, | ||
'footer-html': pdf_footer_template, | ||
'custom-header': [ | ||
('Accept-Encoding', 'gzip') | ||
], | ||
'no-outline': None, | ||
'enable-local-file-access': None | ||
} | ||
|
||
pdfkit.from_file(input=output_body_html, output_path=output_pdf, options=options) | ||
|
||
encoding = 'UTF-8' | ||
# options = { | ||
# 'dpi': 500, | ||
# 'page-size': 'A4', | ||
# 'margin-top': '1.55in', | ||
# 'margin-right': '0.75in', | ||
# 'margin-bottom': '1.55in', | ||
# 'margin-left': '0.75in', | ||
# 'encoding': "UTF-8", | ||
# 'header-html': pdf_header_template, | ||
# 'footer-html': pdf_footer_template, | ||
# 'custom-header': [ | ||
# ('Accept-Encoding', 'gzip') | ||
# ], | ||
# 'no-outline': None, | ||
# 'enable-local-file-access': None | ||
# } | ||
HTML(output_html).write_pdf(output_pdf) | ||
|
||
encoding = "UTF-8" | ||
file = output_pdf | ||
|
||
with open(file, 'rb') as open_file: | ||
with open(file, "rb") as open_file: | ||
byte_content = open_file.read() | ||
|
||
base64_bytes = b64encode(byte_content) | ||
|
||
data = base64_bytes.decode(encoding) | ||
|
||
presenter_output = { | ||
'mime_type': 'application/pdf', | ||
'data': data | ||
} | ||
presenter_output = {"mime_type": "application/pdf", "data": data} | ||
|
||
os.remove(output_body_html) | ||
os.remove(output_html) | ||
os.remove(file) | ||
|
||
return presenter_output | ||
except Exception as error: | ||
BasePresenter.print_exception(self, error) | ||
presenter_output = { | ||
'mime_type': 'text/plain', | ||
'data': b64encode(("TEMPLATING ERROR\n"+str(error)).encode()).decode('UTF-8') | ||
} | ||
presenter_output = {"mime_type": "text/plain", "data": b64encode(("TEMPLATING ERROR\n" + str(error)).encode()).decode("UTF-8")} | ||
return presenter_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
Flask==1.1.4 | ||
Flask-Cors==3.0.10 | ||
Flask-RESTful==0.3.7 | ||
gevent==21.8.0 | ||
greenlet==1.1.1 | ||
gunicorn==20.0.4 | ||
Jinja2==2.11.3 | ||
MarkupSafe==1.1.0 | ||
marshmallow==3.18.0 | ||
Flask==3.0.0 | ||
Flask-Cors==4.0.0 | ||
Flask-RESTful==0.3.10 | ||
gevent==23.9.1 | ||
gunicorn==21.2.0 | ||
Jinja2==3.1.2 | ||
marshmallow==3.20.1 | ||
marshmallow-enum==1.5.1 | ||
pdfkit==0.6.1 | ||
PyJWT==1.7.1 | ||
python-dotenv==0.10.5 | ||
pytz==2019.3 | ||
PyYAML==6.0.1 | ||
six==1.14.0 | ||
Werkzeug==0.16.0 | ||
python-dotenv==1.0.0 | ||
weasyprint==60.1 |
Oops, something went wrong.