Skip to content

Render

Pub Analyzer provides the option to generate reports in PDF format, that summarizes all the available information. This process utilizes typst, the new markup-based typesetting system for the sciences. Let's see how to render a scientific production report for an author.

import asyncio

from pub_analyzer.internal.render import render_report
from pub_analyzer.models.report import AuthorReport

report = AuthorReport(**kwargs) # (1)!
pdf_bytes = asyncio.run(render_report(report=report, file_path='demo.typ'))

with open('demo.pdf', mode="wb") as file:
    file.write(pdf_bytes)
  1. Use real information instead of **kwargs placeholder.

Warning

Currently, I have not discovered an efficient approach to generate a complete PDF summary for institutions, covering the huge volume of data in the reports. I am open to new ideas and suggestions about it.

Render reports.

render_report async

def render_report(
    report: AuthorReport | InstitutionReport,
    file_path: Path,
) -> bytes

Render report to PDF.

The specified path is not where the PDF file will be saved. The path is where the typst file will be created (You can create a temporary path using the tempfile package). This is done in this way because at the moment the typst package can only read the document to be compiled from a file.

Parameters:

Name Type Description Default
report AuthorReport | InstitutionReport

Report Model.

required
file_path Path

Temporary directory for the typst file.

required

Returns:

Type Description
bytes

PDF bytes.

Raises:

Type Description
SyntaxError

If typst compiler syntax error.

render_template_report async

def render_template_report(
    report: AuthorReport | InstitutionReport,
) -> str

Render report template.

Render the report to typst format using the templates.

Parameters:

Name Type Description Default
report AuthorReport | InstitutionReport

Report Model.

required

Returns:

Type Description
str

Report in Typst language.

Raises:

Type Description
NotImplementedError

If report is InstitutionReport type.