take fonts path from path to template

This commit is contained in:
Torsten Ueberschar
2024-02-19 10:34:13 +01:00
parent b8bd3f288b
commit b669e065f5
2 changed files with 4 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ class HtmlTemplate:
if not Path(templates).exists(): if not Path(templates).exists():
raise FileNotFoundError(f'Inivalid path to template files: {templates}') raise FileNotFoundError(f'Inivalid path to template files: {templates}')
self.templates = templates self.templates = templates
self.path_to_template = Path(templates)
def prepare_template(self, invoice_data, envelope_data): def prepare_template(self, invoice_data, envelope_data):
try: try:
@@ -53,15 +54,14 @@ class HtmlTemplate:
html_content = markdown.markdown(md_content) html_content = markdown.markdown(md_content)
return html_content return html_content
@staticmethod def convert_html_to_pdf(self, source_html, output_filename):
def convert_html_to_pdf(source_html, output_filename):
# open output file for writing (truncated binary) # open output file for writing (truncated binary)
result_file = open(output_filename, "w+b") result_file = open(output_filename, "w+b")
# convert HTML to PDF # convert HTML to PDF
pisa_status = pisa.CreatePDF( pisa_status = pisa.CreatePDF(
source_html, # the HTML to convert source_html, # the HTML to convert
path='test_data/templates/fonts', path=str(self.path_to_template / 'fonts'),
dest=result_file dest=result_file
) # file handle to recieve result ) # file handle to recieve result

View File

@@ -135,7 +135,7 @@ def main():
print('Generating invoice...') print('Generating invoice...')
invoice_pdf = Path(invoice_data.Id).with_suffix('.pdf') invoice_pdf = Path(invoice_data.Id).with_suffix('.pdf')
print(f'Invoice PDF: {invoice_pdf}') print(f'Invoice PDF: {invoice_pdf}')
html_generator.HtmlTemplate.convert_html_to_pdf(template, invoice_pdf) generator.convert_html_to_pdf(template, invoice_pdf)
except Exception as e: except Exception as e:
print(f'Error: {e}') print(f'Error: {e}')
return return