Metadata-Version: 2.4
Name: fajfield-reports
Version: 0.1.0
Summary: Render invoices and multi-page reports from Fajfield templates (.fjt) to PDF from Python, through a self-hosted Fajfield report server. RDLC-style reports without Windows.
Author: Fajfield
License-Expression: MIT
Project-URL: Homepage, https://fajfield.com/developers/reports/
Project-URL: Source, https://github.com/Fajfield/fajfield-reports-python
Project-URL: Issues, https://github.com/Fajfield/fajfield-reports-python/issues
Project-URL: Designer, https://fajfield.com/designer/report/
Keywords: pdf,report,invoice,rdlc,crystal-reports,erp,django,flask,fastapi
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Office/Business
Classifier: Topic :: Printing
Classifier: Intended Audience :: Developers
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# fajfield-reports (Python)

Render invoices, statements and multi-page reports to **PDF from Python**: Django, Flask, FastAPI, Odoo add-ons, scripts and scheduled jobs. Design the template in the free [Fajfield report designer](https://fajfield.com/designer/report/), export it as `.fjt`, and render it with your data.

Think *RDLC or Crystal Reports for Python*, without Windows. Tables continue across pages with the header repeated, groups get subtotals, and "Page 2 of 5" is always right.

The package talks to a **[Fajfield report server](https://github.com/Fajfield/fajfield-reports-server)** you run yourself, so your data stays on your network. No dependencies: only the Python standard library.

## Install

```sh
pip install fajfield-reports
```

Start a report server (once, on any machine your app can reach):

```sh
docker run -p 8080:8080 -e API_KEYS=change-me -v ./templates:/templates fajfield/reports-server
# or, with Node 18+:
npx @fajfield/reports-server serve --templates ./templates
```

## Render a report

```python
from fajfield_reports import ReportClient

client = ReportClient("http://localhost:8080", api_key="change-me")

client.render_to_file("invoice", {
    "company":  {"name": "Harbour Foods Co."},
    "customer": {"name": "Fresh Mart", "address": "Dubai"},
    "invoice":  {"no": "INV-0452", "date": date.today()},
    "items":    [{"name": l.product, "qty": l.qty, "rate": l.price, "taxRate": 0.05} for l in order.lines],
}, "invoice.pdf")
```

- **Template:** the name of a template stored on the server (`"invoice"`), a path to a `.fjt` file, or the template as a dict.
- **Data:** one key per dataset in the template: a list for rows, a dict for single records. `Decimal`, `date`, `datetime`, `UUID`, dataclasses and named tuples are converted for you.

### Django

```python
from django.http import HttpResponse

def invoice_pdf(request, pk):
    pdf = client.render("invoice", invoice_data(pk))
    return HttpResponse(pdf, content_type="application/pdf")
```

### FastAPI

```python
from fastapi.responses import Response

@app.get("/invoices/{pk}.pdf")
def invoice_pdf(pk: int):
    return Response(client.render("invoice", invoice_data(pk)), media_type="application/pdf")
```

## Warnings and errors

```python
r = client.render_result("invoice", data)
r.pdf, r.pages, r.warnings   # e.g. ['No data for "customer". Pass it as data: { customer: { ... } }.']
```

Problems the report can live with (missing data, bad barcode data) come back as `warnings`; the PDF is still made. Real errors raise `ReportError` with a plain message and the HTTP status: 400 bad request or template, 401 wrong API key, 404 unknown template, 0 server not reachable.

## Settings

`ReportClient()` with no arguments reads `FAJFIELD_REPORTS_URL` (default `http://localhost:8080`) and `FAJFIELD_REPORTS_KEY` from the environment.

## Also from Fajfield

[Report designer](https://fajfield.com/designer/report/) · [JavaScript](https://www.npmjs.com/package/@fajfield/reports) · [Flutter](https://pub.dev/packages/fajfield_reports_flutter) · [.NET](https://www.nuget.org/packages/Fajfield.Reports) · [Report server](https://github.com/Fajfield/fajfield-reports-server)

MIT license. Made by [Fajfield](https://fajfield.com): research and development for ERP, warehouse, POS and business data.
