PDF templates with the template kit

A Numsolar PDF template is a small web app. Numsolar fills it with the offer's data and prints it to a PDF for every offer.

How it works

  1. You upload a zip with a built web app (dist/index.html and its files).
  2. For each offer, Numsolar opens dist/index.html in a headless Chromium and passes the offer's data (the template data, JSON).
  3. The app draws the pages. When they are ready it sets the class offer-ready on <html>.
  4. Numsolar waits for html.offer-ready and prints the PDF.

The app is TypeScript with Preact and htm, built with Vite, laid out in pages with Paged.js. Two Numsolar packages do the work:

  • @numsolar/offer-templating (public on npm, 0.2.0): loads the offer data and the settings, gives them to your components (useOfferData(), useSettings()) and sets the PDF metadata. Its Vite plugin, @numsolar/vite-plugin-offer-templating, makes the build work from inside a zip.
  • @numsolar/template-kit (the kit, in the kit/ folder of every template zip, not on npm): print styles, fonts, number formats, data selectors and ready components (cover, charts, price tables, signature line, …).
The old template language (HTML pages with numsolar-* tags) is retired and no longer documented. Make new templates with the template kit, as below.

Start from a gallery template

  1. Open the template gallery and pick a template.
  2. Download its zip (for example numsolar-mall-offer-enkel.zip, the one-page offer). The zip holds the source, the kit, example data and the built dist/.
  3. Unzip it and install the packages. You need Node.js 18 or later.
shell
npm ci             # install the packages (once)
npm run dev        # preview with the example houses: http://localhost:5135/?house=1 … 3
npm run check      # type check
npm run build      # write dist/

What is in the zip

zip layout
package.json        the packages and the "numsolar" metadata
index.html          the page shell (<div id="app">)
src/main.ts         the entry: <App>, the page size, the PDF metadata
src/pages/          your pages (Preact components)
src/texts.ts        every text of the template
src/assets/style.css  your colours and layout
kit/                @numsolar/template-kit (do not change: copy a part into src/ instead)
sample-data/        invented houses in the real data shape
dist/               the built template: Numsolar renders dist/index.html
AI-GUIDE.md         the rules and the data, for an AI chat or a developer
template.json       written by the build from package.json "numsolar"

package.json

The dependencies, and a numsolar block that describes the template in the gallery:

package.json (part)
{
  "dependencies": {
    "@numsolar/offer-templating": "0.2.0",
    "@numsolar/template-kit": "file:./kit",
    "@preact/signals": "2.11.3",
    "htm": "3.1.1",
    "pagedjs": "0.4.3",
    "preact": "10.29.8"
  },
  "devDependencies": {
    "@numsolar/vite-plugin-offer-templating": "0.2.0",
    "typescript": "5.9.3",
    "vite": "5.4.21"
  },
  "numsolar": {
    "kind": "offer",
    "contractMin": 1,
    "title": {"sv": "Enkel", "en": "Simple"},
    "tagline": {"sv": "Hela offerten på en sida", "en": "The whole offer on one page"},
    "pages": [1, 1],
    "length": "1",
    "scheme": "light",
    "palette": "mono",
    "density": "balanced",
    "audience": ["budget"],
    "display_font": null
  }
}
  • kind: offer, wiring, mounting, bom, agreement or other.
  • contractMin: the lowest data contract version the template needs (1 or 2).
  • title (required) and tagline: in Swedish and English.
  • The gallery filters: pages ([min, max]), length (1, 2-3, 4-6, 8-12, 13-16), scheme (light, dark), palette (colourful, mono, duotone), density (rich, balanced, vibe), audience (family, eco, numbers, premium, budget, farm, battery, ev, holiday, thorough, brf).
  • display_font: one of the kit's display faces (archivo, baloo-2, fraunces, instrument-serif, jetbrains-mono, lora, space-grotesk, source-serif-4) or null.

The entry point

A short src/main.ts: the kit's styles, the page size for Paged.js, your pages, and the ready signal.

src/main.ts
import { App, useOfferData, useSettings } from "@numsolar/offer-templating";
import { html } from "htm/preact";
import { render } from "preact";

import { onRenderedPages, type TemplateData } from "@numsolar/template-kit";
import type { Settings } from "@numsolar/template-kit/offer-data";
import "@numsolar/template-kit/tokens.css";
import "@numsolar/template-kit/components.css";
import "@numsolar/template-kit/screen.css";
import "./assets/style.css";
import Pages from "./pages/Pages";

function Document() {
  const data = useOfferData() as TemplateData;
  const settings = useSettings(false) as Settings | undefined;
  return html`
    <style type="pagedjs+text/css">
      @page { size: A4; margin: 10mm; }
    </style>
    <${Pages} data=${data} settings=${settings} />
  `;
}

render(
  html`<${App} prodMode=${import.meta.env.PROD} onRenderedPages=${() => onRenderedPages()}>
    <${Document} />
  </${App}>`,
  document.getElementById("app") as HTMLElement,
);

onRenderedPages() sets html.offer-ready when Paged.js has laid out the pages.

A page

Read the data through the kit's selectors (@numsolar/template-kit/offer-data). They return null for a missing value, so a page never throws on old or partial data.

src/pages/Pages.ts
import { kwh, PriceSummary, SignatureLine } from "@numsolar/template-kit";
import * as D from "@numsolar/template-kit/offer-data";
import { html } from "htm/preact";

export default function Pages(props: { data: D.Data; settings?: D.Settings }) {
  const { data } = props;
  const who = D.customer(data);
  const e = D.energy(data);
  const us = D.contact(data);

  return html`<section class="page">
    <h1>Offert till ${who?.name ?? ""}</h1>
    ${e ? html`<p>Produktion: ${kwh(e.production)} per år</p>` : null}
    <${PriceSummary} price=${D.price(data)} />
    <${SignatureLine} parties=${[who?.name, us?.org]} />
  </section>`;
}

Some selectors of offer-data:

  • customer, contact, offerMeta, currency: who, from whom, which offer.
  • price, cards, sortedLines: the price and the equipment.
  • energy, months, shares, economy, bills, balances: the simulation's energy and money.
  • pictures, roofs, faces: the house images and the roof faces.
  • strings, protection, structural, bomBrief, warranties, payment, schedule: wiring, loads, parts and agreement data.
  • schemeAttrs(settings): the organisation's brand colours and fonts on the root element.

The kit's components include CoverFrame, Hero, FactsLine, MonthlyBars, ShareRing, FlowStack, PriceTable, PriceSummary, MonthTable, RoofTable, WarrantyTable, KpiRow, StepsList, Timeline, ContactCard and SignatureLine; the formats money, kwh, num, percent and date print Swedish numbers (decimal comma, a thin space before units). kit/README.md lists them all.

Rules

  • Print what Numsolar sends. Never compute physics or money in the template.
  • A missing value hides its section, or prints "Inte tillgängligt". Never throw.
  • Read data.contract?.version ?? 1 and hide what needs a newer contract.
  • Load only your own files, data: URLs and Google Fonts: other requests are blocked when the PDF is made.

Upload it

  1. Run npm run build, then zip the folder with dist/ at the top level (not inside another folder): zip -r ../my-template.zip . -x "node_modules/*".
  2. In Numsolar, open PDF Templates → Upload new PDF template and upload the zip.
  3. Under the advanced settings: "Path to index.html in archive" is ./dist/index.html; set "Wait for DOM to load" to html.offer-ready.
  4. Save. Numsolar shows a preview and a sample PDF.

Settings that the installer sets per template reach the app as useSettings(), for example accent, logoDataUrl, showCost and compareOffers.