// Auteur : Simon-Pierre Boucher — contact@spboucher.ai /** * Rapport PDF de parc immobilier — couverture agrégée (tuiles, historique du parc, * répartitions, table des propriétés) puis une page condensée par propriété. * 100 % vectoriel, identité ValoPlex. */ import PDFDocument from "pdfkit"; import path from "path"; import type { PortfolioResult, UnitEstimate } from "./estimator"; const INK = "#141814"; const INK3 = "#8b928c"; const PAPER = "#f5f3ee"; const SURFACE2 = "#faf9f5"; const GREEN = "#b25f16"; const LIME = "#ff9f45"; const LIME_SOFT = "#ffedd9"; const AMBER_SOFT = "#fdf3e2"; const DANGER_SOFT = "#fbe9e7"; const LINE = "#dedcd4"; const W = 612; const H = 792; const M = 44; const F = (f: string) => path.join(process.cwd(), "assets", "fonts", f); const fmt = (v: number | null | undefined) => v == null ? "—" : new Intl.NumberFormat("fr-CA", { style: "currency", currency: "CAD", maximumFractionDigits: 0 }).format(v); const CONF_BG: Record = { A: LIME, B: LIME_SOFT, C: AMBER_SOFT, D: DANGER_SOFT }; const TYPE_FR: Record = { unifamilial: "Unifamiliale", plex: "Plex", condo_ou_multi: "Condo/multi", chalet: "Chalet", maison_mobile: "Maison mobile", terrain: "Terrain", autre: "Autre", }; type Doc = InstanceType; function logo(doc: Doc, x: number, y: number, scale = 1) { doc.save(); doc.font("SG-Bold").fontSize(24 * scale); doc.fillColor(PAPER).text("Valo", x, y, { lineBreak: false }); const w = doc.widthOfString("Valo"); const bx = x + w + 4 * scale; doc.save(); doc.rotate(-3, { origin: [bx, y + 12 * scale] }); const bw = doc.widthOfString("Plex") + 12 * scale; doc.roundedRect(bx, y - 3 * scale, bw, 30 * scale, 5 * scale).fill(LIME); doc.fillColor(INK).text("Plex", bx + 6 * scale, y, { lineBreak: false }); doc.restore(); doc.restore(); } function kicker(doc: Doc, txt: string, x: number, y: number) { doc.rect(x, y + 3.5, 20, 2).fill(GREEN); doc.font("JB-Bold").fontSize(8).fillColor(GREEN) .text(txt.toUpperCase(), x + 26, y, { characterSpacing: 1.4, lineBreak: false }); } function pageFrame(doc: Doc, page: number, total: number, generated: string) { doc.rect(0, 0, W, H).fill(PAPER); doc.rect(M, H - 46, W - 2 * M, 1.2).fill(INK); doc.font("JB-Reg").fontSize(6.5).fillColor(INK3).text( "WWW.VALOPLEX.COM — RAPPORT DE PARC IMMOBILIER · ESTIMATION STATISTIQUE À TITRE INDICATIF · SIMON-PIERRE BOUCHER · CONTACT@SPBOUCHER.AI", M, H - 38, { width: W - 2 * M - 60, characterSpacing: 0.4, lineBreak: false }); doc.font("JB-Bold").fontSize(7.5).fillColor(INK) .text(`${page} / ${total}`, W - M - 40, H - 39, { width: 40, align: "right" }); doc.font("JB-Reg").fontSize(6.5).fillColor(INK3) .text(generated, W - M - 160, H - 30, { width: 160, align: "right" }); } function card(doc: Doc, x: number, y: number, w: number, h: number, fill = "#ffffff") { doc.roundedRect(x + 3, y + 3, w, h, 8).fill("#e3e1d9"); doc.roundedRect(x, y, w, h, 8).lineWidth(1.3).fillAndStroke(fill, INK); } function tile(doc: Doc, x: number, y: number, w: number, h: number, k: string, v: string, hero = false) { card(doc, x, y, w, h, hero ? INK : "#ffffff"); doc.font("SG-Bold").fontSize(16).fillColor(hero ? LIME : INK) .text(v, x + 12, y + 12, { width: w - 24, height: 22, ellipsis: true, lineBreak: false }); doc.font("JB-Bold").fontSize(5.6).fillColor(hero ? "rgba(255,159,69,0.7)" : INK3) .text(k.toUpperCase(), x + 12, y + h - 16, { width: w - 24, characterSpacing: 0.7, lineBreak: false }); } function kvCell(doc: Doc, x: number, y: number, w: number, h: number, k: string, v: string) { doc.lineWidth(0.8); doc.roundedRect(x, y, w, h, 4).fillAndStroke(SURFACE2, LINE); doc.font("JB-Bold").fontSize(5.6).fillColor(INK3) .text(k.toUpperCase(), x + 6, y + 5.5, { width: w - 12, characterSpacing: 0.6, height: 8, ellipsis: true }); doc.font("SG-Bold").fontSize(8.8).fillColor(INK) .text(v, x + 6, y + 15.5, { width: w - 12, height: h - 19, ellipsis: true }); doc.lineWidth(1.3); } function hbars(doc: Doc, x: number, y: number, w: number, rows: [string, number, string][], accentLast = true) { const max = Math.max(...rows.map((r) => r[1]), 1); rows.forEach(([label, value, txt], i) => { const ly = y + i * 19; doc.font("JB-Bold").fontSize(7).fillColor(INK).text(label, x, ly + 2, { lineBreak: false }); const tx = x + 50; const tw = w - 50 - 86; doc.rect(tx, ly, tw, 11).fill(SURFACE2); doc.rect(tx, ly, Math.max(2, (value / max) * tw), 11) .fill(accentLast && i === rows.length - 1 ? INK : GREEN); doc.font("JB-Bold").fontSize(7).fillColor(INK) .text(txt, x + w - 82, ly + 2, { width: 82, align: "right" }); }); } /** Page condensée d'une propriété du parc. */ function propertyPage(doc: Doc, item: UnitEstimate, idx: number, total: number, pageNo: number, pageTotal: number, generated: string) { const u = item.unit!; const s = u.specs; const r = item.result; doc.addPage({ size: "LETTER", margin: 0 }); pageFrame(doc, pageNo, pageTotal, generated); doc.rect(0, 0, W, 8).fill(INK); doc.rect(0, 8, W, 2.5).fill(LIME); let y = 34; // index de propriété doc.circle(M + 10, y + 12, 12).lineWidth(1.4).fillAndStroke(GREEN, INK); doc.font("JB-Bold").fontSize(10).fillColor(PAPER) .text(String(idx + 1), M - 2, y + 7.5, { width: 24, align: "center", lineBreak: false }); doc.font("JB-Reg").fontSize(7).fillColor(INK3) .text(`PROPRIÉTÉ ${idx + 1} / ${total}`, M + 30, y + 2, { characterSpacing: 1 }); doc.font("SG-Bold").fontSize(14).fillColor(INK) .text(`${(u.adresse ?? "").toUpperCase()}${s.apt ? " APP. " + s.apt : ""} · ${(u.municipalite ?? "").toUpperCase()}`, M + 30, y + 12, { width: W - 2 * M - 30 }); y = Math.max(doc.y + 10, y + 44); doc.font("SG-Bold").fontSize(34).fillColor(INK).text(fmt(r.estimate), M, y); if (u.nbLogements && u.nbLogements >= 2) { doc.font("JB-Bold").fontSize(7.5).fillColor("#b25f16") .text(`${u.nbLogements} PORTES · ${fmt(Math.round(r.estimate / u.nbLogements))} / PORTE`, M + 250, y + 14, { characterSpacing: 0.8, lineBreak: false }); } y = doc.y + 6; doc.font("JB-Bold").fontSize(7).fillColor(INK3).text("FOURCHETTE", M, y, { lineBreak: false }); doc.font("SG-Bold").fontSize(11).fillColor(INK).text(`${fmt(r.low)} → ${fmt(r.high)}`, M, y + 10, { lineBreak: false }); const confTxt = `CONFIANCE ${r.confidenceLevel} · ${r.confidencePct} %`; doc.font("JB-Bold").fontSize(7); const cw = doc.widthOfString(confTxt) + 16; doc.roundedRect(M + 220, y + 4, cw, 16, 4).lineWidth(1).fillAndStroke(CONF_BG[r.confidenceLevel], INK); doc.fillColor(INK).text(confTxt, M + 228, y + 8.5, { lineBreak: false }); doc.font("JB-Bold").fontSize(7).fillColor(INK3).text("RÔLE 2026", M + 380, y, { lineBreak: false }); doc.font("SG-Bold").fontSize(11).fillColor(INK).text(fmt(u.valeurRole), M + 380, y + 10, { lineBreak: false }); y += 34; // fiche compacte const rows: [string, string][] = [ ["Usage", s.cubfLibelle ?? "—"], ["Année constr.", u.anneeConstruction ? String(u.anneeConstruction) : "—"], ["Aire étages", u.aireEtagesM2 ? `${u.aireEtagesM2} m²` : "—"], ["Terrain", u.superficieTerrainM2 ? `${u.superficieTerrainM2} m²` : "—"], ["Frontage", s.frontTerrainM ? `${s.frontTerrainM} m` : "—"], ["Étages", s.nbEtages != null ? String(s.nbEtages) : "—"], ["Logements", u.nbLogements != null ? String(u.nbLogements) : "—"], ["Genre", s.genreConstruction ?? "—"], ["Lien phys.", s.lienPhysique ?? "—"], ["Val. terrain", fmt(s.valeurTerrain)], ["Val. bâtiment", fmt(s.valeurBatiment)], ["Matricule", s.matricule ?? "—"], ]; kicker(doc, "Fiche du registre", M, y); y += 14; const cw2 = (W - 2 * M - 3 * 8) / 4; rows.forEach(([k, v], i) => { kvCell(doc, M + (i % 4) * (cw2 + 8), y + Math.floor(i / 4) * 36, cw2, 30, k, v); }); y += 3 * 36 + 14; // historique kicker(doc, "Valeur estimée 2021 → 2026", M, y); y += 16; card(doc, M, y, W - 2 * M, u.history.length * 19 + 20); hbars(doc, M + 14, y + 12, W - 2 * M - 28, u.history.map((h) => [String(h.year), h.value ?? 0, fmt(h.value)])); y += u.history.length * 19 + 34; // top comparables kicker(doc, `Meilleurs comparables (${Math.min(5, r.comps.length)})`, M, y); y += 14; r.comps.slice(0, 5).forEach((c, i) => { const ly = y + i * 17; if (i % 2 === 0) doc.rect(M, ly - 2, W - 2 * M, 16).fill(SURFACE2); doc.font("SG-Bold").fontSize(7.5).fillColor(INK) .text(`${i + 1}. ${c.street ?? ""}, ${c.city ?? ""}`, M + 6, ly, { width: 300, height: 9, ellipsis: true, lineBreak: false }); doc.font("JB-Reg").fontSize(6.5).fillColor(INK3) .text(`${c.date} · ${c.distanceM < 1000 ? Math.round(c.distanceM) + " m" : (c.distanceM / 1000).toFixed(1) + " km"}`, M + 316, ly + 1, { lineBreak: false }); doc.font("SG-Bold").fontSize(8).fillColor(INK) .text(fmt(Math.round(c.adjustedPrice)), W - M - 90, ly, { width: 84, align: "right", lineBreak: false }); }); } export async function buildPortfolioReport(pf: PortfolioResult): Promise { const { items, aggregates: a } = pf; const generated = new Date().toISOString().slice(0, 16).replace("T", " "); const pageTotal = 1 + items.length; const doc = new PDFDocument({ size: "LETTER", margin: 0, info: { Title: "ValoPlex — Rapport de parc immobilier" } }); doc.registerFont("SG-Bold", F("SpaceGrotesk-Bold.ttf")); doc.registerFont("SG-Med", F("SpaceGrotesk-Medium.ttf")); doc.registerFont("JB-Reg", F("JetBrainsMono-Regular.ttf")); doc.registerFont("JB-Bold", F("JetBrainsMono-Bold.ttf")); doc.registerFont("Inter", F("Inter-Regular.ttf")); const chunks: Buffer[] = []; doc.on("data", (c: Buffer) => chunks.push(c)); const done = new Promise((res) => doc.on("end", () => res(Buffer.concat(chunks)))); /* ------------------------------ COUVERTURE ------------------------------ */ pageFrame(doc, 1, pageTotal, generated); doc.rect(0, 0, W, 110).fill(INK); logo(doc, M, 32); doc.font("JB-Reg").fontSize(7).fillColor(LIME) .text("RAPPORT DE PARC IMMOBILIER · QUÉBEC", M, 70, { characterSpacing: 1.6, lineBreak: false }); doc.font("JB-Reg").fontSize(7).fillColor("#9aa39c") .text(`${a.count} PROPRIÉTÉS`, W - M - 200, 38, { width: 200, align: "right" }) .text(`GÉNÉRÉ LE ${generated}`, W - M - 200, 50, { width: 200, align: "right" }); doc.rect(0, 110, W, 3).fill(LIME); let y = 134; kicker(doc, "Valeur totale estimée du parc", M, y); y += 16; doc.font("SG-Bold").fontSize(44).fillColor(INK).text(fmt(a.totalEstimate), M, y); y = doc.y + 8; doc.font("JB-Bold").fontSize(6.5).fillColor(INK3).text("FOURCHETTE CUMULÉE", M, y, { lineBreak: false }); doc.font("SG-Bold").fontSize(12).fillColor(INK).text(`${fmt(a.totalLow)} → ${fmt(a.totalHigh)}`, M, y + 10, { lineBreak: false }); const confTxt = `CONFIANCE PONDÉRÉE ${a.confidenceLevel} · ${a.confidencePct} %`; doc.font("JB-Bold").fontSize(7.5); const cw = doc.widthOfString(confTxt) + 20; doc.roundedRect(M + 260, y + 2, cw, 18, 4).lineWidth(1).fillAndStroke(CONF_BG[a.confidenceLevel], INK); doc.fillColor(INK).text(confTxt, M + 270, y + 7.5, { lineBreak: false }); y += 38; // tuiles const tiles: [string, string, boolean][] = [ ["Propriétés", String(a.count), true], ["Évaluation municipale", fmt(a.totalRole), false], ["Écart vs rôle", a.ecartRolePct != null ? `${a.ecartRolePct >= 0 ? "+" : ""}${a.ecartRolePct.toFixed(0)} %` : "—", false], ["Croissance 2021→2026", a.growthPct != null ? `${a.growthPct >= 0 ? "+" : ""}${a.growthPct.toFixed(0)} %` : "—", true], ["Aire habitable totale", `${a.totalFloorArea.toLocaleString("fr-CA")} m²`, false], ["Terrain total", `${a.totalLandArea.toLocaleString("fr-CA")} m²`, false], ["Logements", String(a.totalDwellings), false], ["Année moyenne", a.avgYearBuilt ? String(a.avgYearBuilt) : "—", false], ]; const tw = (W - 2 * M - 3 * 10) / 4; tiles.forEach(([k, v, hero], i) => { tile(doc, M + (i % 4) * (tw + 10), y + Math.floor(i / 4) * 58, tw, 50, k, v, hero); }); y += 2 * 58 + 12; // historique du parc kicker(doc, "Valeur du parc par année (unités couvertes)", M, y); y += 14; card(doc, M, y, (W - 2 * M - 12) * 0.58, 6 * 19 + 20); hbars(doc, M + 12, y + 12, (W - 2 * M - 12) * 0.58 - 24, a.history.map((h) => [String(h.year), h.total, fmt(h.total)])); // répartitions const rx = M + (W - 2 * M - 12) * 0.58 + 12; const rw = W - M - rx; card(doc, rx, y, rw, 6 * 19 + 20); doc.font("JB-Bold").fontSize(6.2).fillColor(INK3).text("PAR MUNICIPALITÉ", rx + 12, y + 10, { characterSpacing: 0.8 }); a.municipalities.slice(0, 3).forEach((m, i) => { const ly = y + 22 + i * 15; doc.font("SG-Bold").fontSize(7.5).fillColor(INK).text(m.name, rx + 12, ly, { width: rw - 100, height: 9, ellipsis: true, lineBreak: false }); doc.font("JB-Reg").fontSize(6.8).fillColor(INK3).text(`${m.count} · ${fmt(m.total)}`, rx + rw - 96, ly, { width: 86, align: "right", lineBreak: false }); }); doc.font("JB-Bold").fontSize(6.2).fillColor(INK3).text("PAR TYPE", rx + 12, y + 72, { characterSpacing: 0.8 }); a.types.slice(0, 3).forEach((tp, i) => { const ly = y + 84 + i * 15; doc.font("SG-Bold").fontSize(7.5).fillColor(INK).text(TYPE_FR[tp.type] ?? tp.type, rx + 12, ly, { width: rw - 100, height: 9, ellipsis: true, lineBreak: false }); doc.font("JB-Reg").fontSize(6.8).fillColor(INK3).text(`${tp.count} · ${fmt(tp.total)}`, rx + rw - 96, ly, { width: 86, align: "right", lineBreak: false }); }); y += 6 * 19 + 34; // table des propriétés kicker(doc, "Sommaire des propriétés", M, y); y += 14; items.slice(0, 14).forEach((it, i) => { const u = it.unit!; const ly = y + i * 17; if (i % 2 === 0) doc.rect(M, ly - 2, W - 2 * M, 16).fill(SURFACE2); doc.circle(M + 8, ly + 5, 6).lineWidth(0.9).fillAndStroke(GREEN, INK); doc.font("JB-Bold").fontSize(6).fillColor(PAPER).text(String(i + 1), M + 2, ly + 2.5, { width: 12, align: "center", lineBreak: false }); doc.font("SG-Bold").fontSize(7.5).fillColor(INK) .text(`${u.adresse ?? ""} · ${u.municipalite ?? ""}`, M + 20, ly, { width: 280, height: 9, ellipsis: true, lineBreak: false }); doc.font("JB-Reg").fontSize(6.5).fillColor(INK3) .text(`CONF. ${it.result.confidenceLevel}`, M + 310, ly + 1, { lineBreak: false }); doc.font("JB-Reg").fontSize(6.5).fillColor(INK3) .text(`RÔLE ${fmt(u.valeurRole)}`, M + 360, ly + 1, { width: 90, lineBreak: false }); doc.font("SG-Bold").fontSize(8).fillColor(INK) .text(fmt(it.result.estimate), W - M - 80, ly, { width: 76, align: "right", lineBreak: false }); }); if (items.length > 14) { doc.font("JB-Reg").fontSize(6.5).fillColor(INK3) .text(`+ ${items.length - 14} autres propriétés — voir pages suivantes`, M, y + 14 * 17 + 2); } /* --------------------------- PAGES PROPRIÉTÉS --------------------------- */ items.forEach((it, i) => propertyPage(doc, it, i, items.length, i + 2, pageTotal, generated)); doc.end(); return done; }