Merge branch 'main' of git.nice.net.nz:hads/hinpdof
Some checks failed
Build and Publish Docker Image / build (push) Failing after 35s
Some checks failed
Build and Publish Docker Image / build (push) Failing after 35s
This commit is contained in:
59
test.py
Executable file
59
test.py
Executable file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Opens the `test.j2` HTML template and sends it to the hinpdof API service to convert
|
||||
it to a PDF file and saves the result to the `test.pdf` file.
|
||||
"""
|
||||
|
||||
import requests
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
# hinpdof API service openapi url: https://hinpdof.huna.me/openapi.json
|
||||
|
||||
|
||||
def render_template(template_name, context):
|
||||
"""Render the Jinja2 template with the given context."""
|
||||
env = Environment(loader=FileSystemLoader("."))
|
||||
template = env.get_template(template_name)
|
||||
return template.render(context)
|
||||
|
||||
|
||||
def convert_html_to_pdf(html_content):
|
||||
"""Send the HTML content to the hinpdof API service to convert it to a PDF file."""
|
||||
api_url = "https://hinpdof.huna.me/pdf"
|
||||
api_url = "http://localhost:8000/pdf"
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"x-secret-key": "foo",
|
||||
}
|
||||
data = {
|
||||
"html": html_content,
|
||||
}
|
||||
response = requests.post(api_url, json=data, headers=headers)
|
||||
response.raise_for_status()
|
||||
return response.content
|
||||
|
||||
|
||||
def save_pdf(pdf_content, output_path):
|
||||
"""Save the PDF content to a file."""
|
||||
with open(output_path, "wb") as pdf_file:
|
||||
pdf_file.write(pdf_content)
|
||||
|
||||
|
||||
def main():
|
||||
# Render the Jinja2 template
|
||||
template_name = "test.j2"
|
||||
context = {} # Add any context variables needed for the template
|
||||
html_content = render_template(template_name, context)
|
||||
|
||||
# Convert the HTML content to a PDF file
|
||||
pdf_content = convert_html_to_pdf(html_content)
|
||||
|
||||
# Save the PDF file
|
||||
output_path = "test.pdf"
|
||||
save_pdf(pdf_content, output_path)
|
||||
print(f"PDF file saved to {output_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user