Metadata-Version: 2.5
Name: isis-report-lib
Version: 0.12.0
Summary: Python library for tabular PDF reports with customizable hierarchical layouts (Jinja2 + Playwright).
Project-URL: Homepage, https://github.com/thepaneb/isis-report-lib
Project-URL: Source, https://github.com/thepaneb/isis-report-lib
Project-URL: Issues, https://github.com/thepaneb/isis-report-lib/issues
Project-URL: Changelog, https://github.com/thepaneb/isis-report-lib/blob/main/CHANGELOG.md
Author: Gil Cleber Barboza
License: MIT
License-File: LICENSE
Keywords: jinja2,pdf,playwright,report,toon
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Text Processing
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: babel>=2.14
Requires-Dist: jinja2>=3.1.6
Requires-Dist: playwright>=1.65.0
Requires-Dist: pydantic>=2.0
Provides-Extra: dev
Requires-Dist: cryptography>=42; extra == 'dev'
Requires-Dist: css-inline>=0.20; extra == 'dev'
Requires-Dist: mypy>=1.18; extra == 'dev'
Requires-Dist: openpyxl>=3.1; extra == 'dev'
Requires-Dist: pandas>=2.0; extra == 'dev'
Requires-Dist: pytest-asyncio>=1.4.1.post1; extra == 'dev'
Requires-Dist: pytest-cov>=7.1.0; extra == 'dev'
Requires-Dist: pytest-timeout>=2; extra == 'dev'
Requires-Dist: pytest>=9.2.0; extra == 'dev'
Requires-Dist: ruff>=0.17.4; extra == 'dev'
Requires-Dist: types-openpyxl>=3.1; extra == 'dev'
Provides-Extra: email
Requires-Dist: css-inline>=0.20; extra == 'email'
Provides-Extra: pandas
Requires-Dist: pandas>=2.0; extra == 'pandas'
Provides-Extra: sign
Requires-Dist: cryptography>=42; extra == 'sign'
Provides-Extra: xlsx
Requires-Dist: openpyxl>=3.1; extra == 'xlsx'
Description-Content-Type: text/markdown

# isis-report-lib

[![CI](https://github.com/thepaneb/isis-report-lib/actions/workflows/ci.yml/badge.svg)](https://github.com/thepaneb/isis-report-lib/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/isis-report-lib.svg)](https://pypi.org/project/isis-report-lib/)
[![Python](https://img.shields.io/pypi/pyversions/isis-report-lib.svg)](https://pypi.org/project/isis-report-lib/)
[![Wiki](https://img.shields.io/badge/docs-wiki-blue)](https://github.com/thepaneb/isis-report-lib/wiki)
[![Docs](https://img.shields.io/badge/docs-zensical-blue)](https://thepaneb.github.io/isis-report-lib/)
[![ASVS](https://img.shields.io/badge/ASVS-4.0.3%20mapped-blue)](docs/ASVS.md)
[![pip-audit](https://img.shields.io/badge/pip--audit-gated%20in%20CI-green)](.github/workflows/ci.yml)
[![Semgrep](https://img.shields.io/badge/semgrep-gated%20in%20CI-green)](.github/workflows/semgrep.yml)
[![Security](https://img.shields.io/badge/security-policy-blue)](SECURITY.md)
[![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/thepaneb/isis-report-lib/badge)](https://scorecard.dev/viewer/?uri=github.com/thepaneb/isis-report-lib)
[![codecov](https://codecov.io/gh/thepaneb/isis-report-lib/graph/badge.svg)](https://codecov.io/gh/thepaneb/isis-report-lib)

---

Python library for **tabular PDF reports** with customizable hierarchical layouts — extracted from the [isis_report](https://github.com/thepaneb/isis_report) PoC (Robyn microservice), without the server.

**Landing page:** <https://thepaneb.github.io/isis-report-lib/>

- Input: `dict`/`ReportPayload` (`meta` + optional `layout` + `data`) or **`.toon`** text
- Rendering: Jinja2 (templates bundled in the package)
- PDF: Playwright (Chromium)
- API: synchronous (`generate_pdf`) + asynchronous (`agenerate_pdf`)

## Installation

```bash
pip install isis-report-lib
playwright install chromium   # required to generate PDFs
```

> After upgrading `playwright`, reinstall the browser (`playwright install chromium`):
> each Playwright version pins a specific Chromium build.

Dev:

```bash
pip install -e ".[dev]"
```

## Quickstart

```python
from isis_report_lib import generate_pdf, generate_pdf_from_toon, render_html

payload = {
    "meta": {"title": "Sales Order", "sub_title": "2024", "mode": "exact"},
    "layout": {
        "company": {"label": "Company", "format_mask": "str", "alignment": "left"},
        "orders": {
            "order": {"label": "Code", "format_mask": "int", "alignment": "right"},
            "total": {"label": "Total", "format_mask": "float", "alignment": "right"},
        },
    },
    "data": [{"company": "ACME", "orders": [{"order": 750, "total": 4000}]}],
}

pdf_bytes = generate_pdf(payload)
open("report.pdf", "wb").write(pdf_bytes)

# Without Playwright: just the intermediate HTML
html = render_html(payload)

# From .toon
toon = open("examples/sales_order.toon", encoding="utf-8").read()
open("report_toon.pdf", "wb").write(generate_pdf_from_toon(toon))
```

Async:

```python
from isis_report_lib import agenerate_pdf, agenerate_pdf_from_toon

pdf = await agenerate_pdf(payload)
pdf = await agenerate_pdf_from_toon(toon_text)
```

## CLI

```bash
# PDF from .toon, .json or .csv (format guessed from extension)
isis-report generate examples/sales_order.toon -o report.pdf
isis-report generate payload.json --page-format Letter
isis-report generate data.csv --delimiter ";" -o report.pdf

# JSON Schema of the payload (editors, validation)
isis-report schema -o payload.schema.json

# Same report as XLSX, CSV or HTML (needs no browser)
isis-report generate examples/sales_order.toon -o report.xlsx
isis-report generate examples/sales_order.toon -o report.csv
isis-report generate examples/sales_order.toon -o report.html
isis-report generate input.toon --format xlsx   # or csv | html | pdf

# Intermediate HTML without a browser (prints to stdout unless -o is given)
isis-report render examples/sales_order.toon -o report.html
```

## Report signing (tamper-evident seal)

```bash
isis-report keygen -o secret.key                                  # shared secret (self-validation)
isis-report keygen --kind ed25519 -o private.pem --public-out public.pem   # third-party validation
isis-report sign report.pdf --key-file secret.key --key-id portal-1 -o report.signed.pdf
isis-report verify report.signed.pdf --key-file secret.key       # exit 0 = VALID
# rotation: repeat --key-file; the envelope key_id selects first, the rest are tried in order
isis-report verify report.signed.pdf --key-file old.key --key-file new.key
```

```python
from isis_report_lib import generate_hmac_key, sign_pdf, verify_pdf

secret = generate_hmac_key()
signed = sign_pdf(pdf_bytes, secret, key_id="portal-1")
info = verify_pdf(signed, secret)
assert info.valid  # + info.reason/alg/sha256/key_id/issued_at

# keyring rotation: {"key_id": key} mapping or key list; retired keys stay configured until old seals expire
assert verify_pdf(signed, {"2026-a": old_secret, "portal-1": secret}).valid
```

A `%ISIS-SIGNATURE` block is appended after the PDF `%%EOF` (readers ignore it; any modification invalidates the seal). `verify_pdf` never raises — check `.valid`. This is an origin/integrity seal, not Adobe PAdES.

## Data inputs

```python
from isis_report_lib import generate_pdf
from isis_report_lib.inputs import from_csv, from_dataframe, from_json

generate_pdf(from_dataframe(df, meta={"title": "Sales", "locale": "en-US"}))
generate_pdf(from_csv("sales.csv", delimiter=";"))   # light int/float/bool coercion
generate_pdf(from_json("payload.json"))              # list -> data; full payload passes through
```

`pandas` is optional: `pip install isis-report-lib[pandas]` (`NaN`/`NaT` become `None`, `Timestamp` becomes `datetime`).

## Output formats

| Format | API | Notes |
| --- | --- | --- |
| PDF | `generate_pdf` / `agenerate_pdf` | Needs Chromium (`playwright install chromium`) |
| XLSX | `generate_xlsx` / `agenerate_xlsx` | One sheet per detail table (context columns repeated), bold totals, `Summary` sheet. Needs `pip install isis-report-lib[xlsx]` |
| CSV | `generate_csv` / `agenerate_csv` | Locale-formatted values, `# table:` / `# summary` sections, `--delimiter` (`;` for Excel PT), `--bom` (UTF-8 BOM for Excel) |
| HTML | `render_html` / `arender_html` | Intermediate output, no browser needed |

```python
from isis_report_lib import generate_csv, generate_xlsx

generate_xlsx(payload)                    # -> bytes (.xlsx workbook, print titles + freeze + filters)
generate_csv(payload, delimiter=";", bom=True)  # -> str (BOM for Excel)
```

PDF options (forwarded to Playwright's `page.pdf`):

```python
generate_pdf(
    payload,
    pdf_options={
        "format": "A4",
        "margin": {"top": ".2in", "bottom": ".5in"},
        "header_template": "<div>My header</div>",
        "footer_template": "<div>Page <span class='pageNumber'></span></div>",
    },
)
```

Via CLI: `--page-format`, `--header-template FILE`, `--footer-template FILE`. The report footer shows `meta.printed_at`, auto-filled with the current timestamp when absent. Footer date/page strings follow `meta.locale` (`pt` → "Impresso em / Página X de Y").

Full example in `examples/`. Note: the sync API opens/closes the browser per call (no leaked processes); the async API reuses the shared pool.

## Composed documents (sections)

For free-form reports, pass `sections` instead of `data` + `layout`:

```python
payload = {
    "meta": {"title": "Board Pack", "locale": "en-US"},
    "sections": [
        {"type": "text", "title": "Intro", "paragraphs": ["Hello.", "World."]},
        {"type": "table", "title": "Sales", "layout": {...}, "data": [...]},
        {"type": "kpi", "title": "Totals", "items": [{"label": "Orders", "value": 42, "format_mask": "int"}]},
        {"type": "chart", "title": "Trend", "chart": {"kind": "bar", "labels": ["Jan", "Feb"], "values": [10, 20]}},
        {"type": "crosstab", "title": "Sales by region × quarter",
         "data": [...], "rows": "region", "columns": "quarter",
         "measure": "total", "function": "sum"},   # buckets × measure, totals included
    ],
}
generate_pdf(payload)   # HTML sections.jinja; XLSX/CSV flatten each table/text/kpi/chart/crosstab section
```

A running balance column is just a field with `"running_balance": "<source field>"` — cumulative sums are computed in row order (e.g. bank statements).

## Custom templates

Point `template_dir` at your own `.jinja` directory and pick a file with `template_name`:

```python
generate_pdf(payload, template_dir="my_templates", template_name="invoice.jinja")
```

```bash
isis-report generate order.toon --template-dir my_templates --template-name invoice.jinja
```

The directory is layered over the bundled one, so you can also override `tabular.jinja` or reuse `tabular_sections.jinja` macros. Templates receive `dataset`, `layout`, `meta`, plus the `fmt(value, mask)` formatter and `zip` — see `examples/custom.jinja`.

## Theming

Set `meta.accent_color` to rebrand the report header without a custom template:

```python
payload = {"meta": {"title": "Sales Order", "accent_color": "#1B7A3D"}, ...}
```

It overrides the `--brand-color` CSS variable. More masks: `str` · `int` · `float` (pt-BR) · `date` · `percent` · `currency` (R$) · `bool`.

## Payload

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `meta` | object | yes | `title`, `sub_title`, `mode` (`"exact"`/`"gray"`), `company`, `locale` (`"pt-BR"`, `"en-US"`…), `currency` (override, e.g. `"EUR"`), `accent_color`, `printed_at` (auto-filled) |
| `layout` | object | no | hierarchical field definition; omit to infer from data |
| `data` | array | yes | raw records or template format (`{"fields": ..., "groups": ...}`) |

Masks (locale-aware via Babel/CLDR): `str` · `int` · `float` · `date` (localized for `date`/`datetime` objects) · `percent` · `currency` · `bool` (`Yes`/`No`) · `sparkline` (list of numbers → inline SVG nanoplot in HTML/PDF, `▁▂▃▄▅▆▇█` block art in CSV/XLSX; field options `spark_kind: line|bar`, `spark_color`).

```python
payload = {
    "meta": {"title": "Sales Order", "locale": "en-US"},  # numbers as 1,234.50, $1,234.50
    "data": [...],
}
```

```bash
isis-report generate order.toon --locale en-US -o out.pdf   # overrides meta.locale
```

## Report parameters

Beyond `layout` and `data`, the payload supports declarative behavior — no rigid bands, the document adapts to the data:

```python
payload = {
    "meta": {
        "title": "Sales Order",
        "page": {"size": "A4", "orientation": "landscape", "margins": {"top": ".4in"}},
    },
    "layout": {
        "company": {"label": "Company", "format_mask": "str", "alignment": "left"},
        "orders": {
            "order": {"label": "Code", "format_mask": "int", "alignment": "right"},
            "status": {"label": "Status", "format_mask": "str", "alignment": "left", "visible": False},
            "total": {"label": "Total", "format_mask": "float", "alignment": "right", "aggregate": "sum"},
            "$print_when": {"field": "status", "not_equals": "cancelled"},
            "$order_by": "-order",
        },
    },
    "data": [...],
}
```

| Parameter | Where | Effect |
| --- | --- | --- |
| `meta.page` | `meta` | `{size, orientation: portrait\|landscape, margins}` → PDF page (explicit `pdf_options` win) |
| `aggregate` | field def | `sum\|count\|avg\|min\|max\|median\|distinct` → group footers, detail `tfoot`, grand summary (`distinct` counts distinct non-`None`). Flat child lists render as tables (several per item allowed); deeper nesting renders as sections |
| `$print_when` | any level | keep only rows matching `{field, equals\|not_equals\|in\|not_in\|gt\|gte\|lt\|lte\|between\|contains\|is_null\|not_null}` (several operators combine with AND) |
| `$order_by` | any level | sort rows: `[{field, direction}]` or `"field"` / `"-field"` (nulls last) |
| `visible` / `visible_when` | field def | hide a column; `visible_when` supports `{..., scope: any\|all}` |
| `cell_rules` | field def | `[{operator(s), color\|background\|bold\|italic}]` → conditional cell style in HTML and XLSX; first matching rule wins, several operators combine with AND, operator-less rule is a catch-all default (e.g. `{"gte": 1000, "lt": 5000, "color": "#C00000"}`) |
| `missing_text` / `substitutions` | field def | `missing_text` text shown for `None` (default: blank); `substitutions` `{raw: display}` map applied before formatting (`str(value)` fallback, `None` falls back to `missing_text`) — in HTML, XLSX and CSV |
| `empty_text` | flat detail list / `table` section / `meta` | message rendered instead of an empty table (`meta.empty_text` covers the whole report); exported as a note row/line in XLSX/CSV |
| `footnote` | field def | `"text"` or `{"mark", "text"}` → superscript mark on the column label, notes listed per report/section (auto marks `*†‡§‖¶` then numbers); XLSX `Notes` sheet, CSV `# [mark]` lines |
| `source_note` / `source` | `meta` / any section | provenance line under the report/section (`# [source]` in CSV, `Notes` sheet in XLSX) |
| `color_scale` / `data_bar` | field def | `color_scale: {min, max}` gradient fill by value (explicit `cell_rules` win); `data_bar: true` native Excel bars (XLSX only) |
| `meta.summary_side` | `meta` | `top\|bottom` (default) grand-summary placement |
| `meta.limits` | `meta` | opt-in DoS quotas `{max_rows, max_bytes}` — enforced in `prepare_report` for every output, fail-closed `ValueError` (unknown keys and non-positive ints raise) |

## `.toon` syntax

```toon
meta:
  title: Sales Order
  mode: exact
layout:
  product{Product,str,left}
  total{Total,float,right,sum}
data[1]:
  - product: Pen
    total: 5.50
```

`data[n]` holds the records (`dados[n]` still accepted as a legacy alias). The compact
layout form accepts an optional 4th part with the aggregate: `total{Total,float,right,sum}`.
See `examples/sales_order.toon`.

## API

| Function | Description |
| --- | --- |
| `generate_pdf(payload, pdf_options?, allowed_hosts?) -> bytes` | sync PDF from dict/`ReportPayload`; `allowed_hosts=[...]` allow-lists outbound http(s) hosts (default denies all network) |
| `agenerate_pdf(...)` | same, async |
| `generate_many(payloads, max_concurrency=4) -> list[bytes]` / `agenerate_many` | concurrent PDFs sharing the pool, results in input order (per-call `allowed_hosts` supported) |
| `generate_pdf_from_toon(text, ...)` / `agenerate_pdf_from_toon` | PDF from `.toon` (same `allowed_hosts` keyword) |
| `generate_xlsx(payload) -> bytes` / `agenerate_xlsx` / `..._from_toon` | .xlsx workbook, no browser (needs `.[xlsx]` extra) |
| `generate_csv(payload, delimiter=",") -> str` / `agenerate_csv` / `..._from_toon` | CSV text, no browser (`bom=True` for Excel) |
| `render_html(payload, inline_css=False)` / `arender_html` / `render_html_from_toon` | intermediate HTML (no browser); `inline_css=True` for email clients (needs `.[email]` extra; resolves CSS variables, sets `lang` from `meta.locale`) |
| Sections payload (`meta` + `sections: [...]`) | composed documents: `table` (full tabular engine), `text`, `kpi`, `chart` (`bar`/`line` inline SVG) |
| `ToonParser().parse(text)` / `parse_toon(text)` | `.toon` -> dict |
| `from_dataframe(df, ...)` / `from_csv(...)` / `from_json(...)` | payload builders (needs `.[pandas]` for DataFrame) |
| `payload_schema()` | JSON Schema of the payload (`isis-report schema`) |
| `collect_tables(dataset, layout)` | flat tables + summary rows (powers XLSX/CSV) |
| `TabularReportService(pdf_options?)` | pipeline `execute(payload)` (Fetch→Process→HTML→PDF) |

## Tests

```bash
pytest -q
pytest -q --cov=isis_report_lib   # coverage gate: 85% (enforced in the CI e2e job)
ruff check src tests
semgrep --test --config .semgrep/rules/ .semgrep/targets/   # custom SAST rules
semgrep scan --config p/default --config .semgrep/rules/ --exclude=.semgrep/targets --metrics=off  # (+ the 2 --exclude-rule from semgrep.yml)
```

Real-PDF tests require `playwright install chromium` and are skipped (`-m "not browser"`) when no browser is available.

## License

MIT — see `LICENSE`.

## Security

- **Dependencies**: audited with `pip-audit` in CI on every PR; Dependabot keeps SHA-pinned actions and packages updated; releases are built and published by CI via OIDC trusted publishing (no stored tokens).
- **SAST**: Semgrep scans every PR, push and weekly — registry packs plus custom `.semgrep/` rules locking the guarantees below (sandboxed autoescaped templates, pre-built SVG only, JS-off, no fetch at render time); findings fail CI and upload as SARIF to code scanning.
- **Trust boundary**: templates (`.jinja` files, `template_dir`) are code — only load them from sources you trust. Report *data* is escaped in HTML output and never executed as template code (charts/sparklines emit pre-built SVG); if you serve the HTML/email output to browsers, treat it like any user-content page.
- **Browser**: pages render with JavaScript disabled in an isolated Chromium context per sync call (shared pool in async mode); `inline_css` strips remote `<link>` stylesheets (no server-side fetches on render); outbound http(s) is denied by default at render time — allow hosts with `allowed_hosts=[...]` (`generate_pdf(...)` / CLI `--allowed-hosts`, never from the payload); the Playwright browser runs headless in its own Chromium sandbox.
- **Denial of service**: opt-in `meta.limits` (`max_rows` records, `max_bytes` payload bytes, CLI `--max-rows`/`--max-bytes`) are enforced before any shaping for every output format; deeply nested layouts/inputs are rejected with `ValueError`.
- **Exports**: text cells starting with `= + - @` (or tab/CR) are `'`-prefixed in CSV/XLSX so spreadsheets cannot evaluate data as formulas (numeric values, including negatives, are untouched).
- **Robustness**: deeply nested `.toon`/JSON input and report layouts are rejected with `ValueError` instead of crashing the interpreter; `verify_pdf` never raises — malformed signatures report `invalid`.
