PDF Generation
This page is dedicated to AIKP Developers which would like to know how and what ocli-core tools to use to generate a PDF Report.
Templates
The recommended way when trying to generate a PDF is to use a template. Currently ocli-core has built-in functionality for working with Jinja templates, but the developer is free to use any other method.
Jinja Templates
Jinja2 is a powerful templating engine for Python, that allows you to generate dynamic HTML (or any text-based format) by mixing static content with Python-like logic. Think of it as the bridge between your data and your presentation.
Here is an example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Report for ROI {{roi_name}}; task: {{task_name}}</title>
<style>
body {
background-color: beige;
}
.container {
max-width: 2048px;
margin: 0 auto;
padding: 0 35px 35px;
}
</style>
</head>
<body>
<div class="container">
<h1>Report for ROI {{roi_name}}; task: {{task_name}}</h1>
<section>
<h2>Here are some example statistics:</h2>
{% for stat in stats %}
<span>{{stat}}</span>
{% endfor %}
</section>
</div>
</body>
</html>
Currently ocli-core has a built-in helper that allows the user to load a jinja template: ocli.core.pdf_utils.load_jtemplate
Function signature
def load_jtemplate(path : str, searchpath : str = "/") -> Template | None:
Parameters
path- path of the jinja templatesearchpath- a path, or list of paths, to the directory that contains the templates.
How to use
from ocli.core.pdf_utils import load_jtemplate
template = load_jtemplate(path="/some/path", searchpath="/some/directory")
How to populate the template
After successfully importing a template, in order to generate the final html content, we would have to populate the template by passing the following variables: roi_name, task_name and stats.
html = template.render(roi_name="Some roi", task_name="Some task", stats=['First statistic'])
Exporting the content to a PDF
After successfully populating the template with the desired values, the user can export the html content as a PDF by using another helper function: ocli.core.pdf_utils.load_jtemplate
Function signature
def export_from_html(html : str, out_path : str) -> bool:
Parameters
html- the HTML content that will be exportedout_path- path of the newly created PDF
How to use
from ocli.core.pdf_utils import export_from_html
export_from_html(html=html, out_path="Report.pdf")
Congratulations, you have successfuly generated your first PDF report!