# Kundali HTML → PDF (mobile / local print)

Mobile API HTML deti hai. PDF server par mat banana — isi HTML ko device par print karo.
Source of truth: `app/web/kundali_pdf.py` (`html_to_pdf`, `chrome_print_argv`).

## 1. HTML lo

Enterprise / unlimited key (`X-API-Key`). Birth params same as pack.

```
GET /v1/vedic/kundali-book.html?date=1990-05-15&time=14:30&place=दिल्ली&name=राम
```

White-label (no watermark):

```
…&white_label=1&brand_name=तंत्रकुलज्योतिषम्
```

Sūrya: `/v1/surya/kundali-book.html`.

JSON pack (HTML string + natal + sibling URLs, **no PDF by default**):

```
GET /v1/vedic/kundali-pack?date=1990-05-15&time=14:30&place=दिल्ली&name=राम
```

`pack.html` WebView mein load karo. Server PDF chahiye to `include_pdf=1` (bada payload, Chrome on server).

HTML already includes print CSS:

```css
@page { size: A4; margin: 12mm 11mm 16mm; }
* { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
```

Do not strip `<style>` or `@page`. Save as UTF-8 `book.html`.

## 2. Chrome / Chromium (same as this API)

Write HTML to a file, then print that **file URI** (not `data:` — Chrome print is unreliable on data URLs).

```bash
# macOS
CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
# Linux
# CHROME=google-chrome   or  chromium

"$CHROME" \
  --headless=new \
  --disable-gpu \
  --disable-dev-shm-usage \
  --hide-scrollbars \
  --no-pdf-header-footer \
  --no-sandbox \
  --disable-setuid-sandbox \
  --virtual-time-budget=60000 \
  --run-all-compositor-stages-before-draw \
  --print-to-pdf=book.pdf \
  "file:///ABS/PATH/TO/book.html"
```

If that write fails, fallback (older Chrome):

```bash
"$CHROME" \
  --headless \
  --disable-gpu \
  --disable-dev-shm-usage \
  --no-sandbox \
  --print-to-pdf-no-header \
  --print-to-pdf=book.pdf \
  "file:///ABS/PATH/TO/book.html"
```

Env override on the server: `CHROME_BIN` or `GOOGLE_CHROME_BIN`.
Timeout: 180s. A valid PDF starts with `%PDF` and is more than a few bytes.

Python equivalent in this repo:

```python
from pathlib import Path
from app.web.kundali_pdf import chrome_bin, chrome_print_argv, html_to_pdf

html = Path("book.html").read_text(encoding="utf-8")
Path("book.pdf").write_bytes(html_to_pdf(html))
# or: subprocess.run(chrome_print_argv(chrome_bin(), html_path.as_uri(), "book.pdf"))
```

If Chrome is missing, server falls back to WeasyPrint (`HTML(string=html).write_pdf()`). Prefer Chrome — Devanagari + page breaks match the book CSS.

## 3. Flutter / native (no Chrome binary)

Keep the same A4 page. Do not add a browser header/footer.

**iOS (WKWebView)**

1. `loadHTMLString(html, baseURL: nil)` (or a temp `file://` URL).
2. Wait until `didFinish` navigation.
3. `webView.createPDF(configuration)` with A4 (210 × 297 mm).
4. Write the `Data` to `.pdf`.

**Android (WebView)**

1. `loadDataWithBaseURL(null, html, "text/html", "utf-8", null)`.
2. After `onPageFinished`, `createPrintDocumentAdapter` + `PrintManager`, or `WebView.PrintDocumentAdapter` → PDF.
3. Page: A4, margins ~12mm / 11mm / 16mm. Colour: exact (no grayscale).

**Flutter**

- Display: `WebViewController.loadHtmlString(pack.html)`.
- Share PDF: iOS `WKWebviewFlutter` / `printing` package with `PdfPageFormat.a4`; Android print-to-PDF from the same WebView.
- Do not rasterize screenshots into a PDF — charts and Devanagari will look wrong.

## 4. Checks

- File starts with `%PDF`.
- Cover is one page; chapters start on new pages (`.ex-cover { page-break-after: always }`).
- White-label HTML has **no** `<div class='ex-wm'>`. Default brand has the watermark.
- Do not wrap the HTML in another document before print.
