@aglyn/shared-util-tools 1.0.0-beta.289 → 1.0.0-beta.290
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/package.json +2 -2
- package/src/lib/pdf/pdf-file.d.ts +65 -0
- package/src/lib/pdf/pdf-file.js +170 -0
- package/src/lib/pdf/pdf-file.js.map +1 -0
- package/src/lib/pdf/pdf-text.d.ts +42 -0
- package/src/lib/pdf/pdf-text.js +743 -0
- package/src/lib/pdf/pdf-text.js.map +1 -0
- package/src/lib/pdf/pdf-writer.d.ts +173 -0
- package/src/lib/pdf/pdf-writer.js +348 -0
- package/src/lib/pdf/pdf-writer.js.map +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../libs/shared/util/tools/src/lib/pdf/pdf-text.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*==========================================\n * TEXT IN A HAND-WRITTEN PDF: the standard Helvetica faces in WinAnsi.\n *\n * The standard 14 fonts need no embedding, and every reader draws them, but\n * they hold one byte per character: WinAnsi, which is Latin-1 plus a row of\n * typographic marks (curly quotes, dashes, the bullet, the euro). Everything\n * here turns arbitrary text into that byte set FIRST, so a string that\n * reaches a content stream can never carry a byte the font has no glyph for:\n *\n * - a character WinAnsi holds is kept as it is;\n * - a known look-alike is swapped in (`−` → `-`, `→` → `->`, `ł` → `l`,\n * odd spaces → a space, zero-width marks → nothing);\n * - an accented letter outside Latin-1 loses its accent (`ă` → `a`) and a\n * compatibility form its styling (`fi` → `fi`, `A` → `A`);\n * - emoji, pictographs and stray combining marks are dropped;\n * - anything else (another script) prints `?`.\n *\n * Widths are Adobe's published metrics for Helvetica and Helvetica-Bold\n * (Helvetica-Oblique shares Helvetica's), in thousandths of the font size,\n * so text measured here right-aligns where a reader draws it.\n *\n * Reached as `@aglyn/shared-util-tools/pdf/pdf-text`; not re-exported from\n * the package root.\n *=========================================*/\n\n/** The three faces a document may set text in. */\nexport type PdfFont = 'regular' | 'bold' | 'italic'\n\n/** Unicode code points WinAnsi holds at 0x80–0x9F, and their bytes. */\nconst WIN_ANSI_HIGH: Readonly<Record<number, number>> = {\n 0x20ac: 0x80,\n 0x201a: 0x82,\n 0x0192: 0x83,\n 0x201e: 0x84,\n 0x2026: 0x85,\n 0x2020: 0x86,\n 0x2021: 0x87,\n 0x02c6: 0x88,\n 0x2030: 0x89,\n 0x0160: 0x8a,\n 0x2039: 0x8b,\n 0x0152: 0x8c,\n 0x017d: 0x8e,\n 0x2018: 0x91,\n 0x2019: 0x92,\n 0x201c: 0x93,\n 0x201d: 0x94,\n 0x2022: 0x95,\n 0x2013: 0x96,\n 0x2014: 0x97,\n 0x02dc: 0x98,\n 0x2122: 0x99,\n 0x0161: 0x9a,\n 0x203a: 0x9b,\n 0x0153: 0x9c,\n 0x017e: 0x9e,\n 0x0178: 0x9f,\n}\n\n/** Characters WinAnsi lacks, read as the nearest text it has. */\nconst STAND_INS: Readonly<Record<string, string>> = {\n '\\t': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n ' ': ' ',\n '': '',\n '': '',\n '': '',\n '': '',\n '': '',\n '‐': '-',\n '‑': '-',\n '‒': '–',\n '―': '—',\n '−': '-',\n '⁃': '-',\n '′': \"'\",\n '″': '\"',\n '‵': \"'\",\n '‛': \"'\",\n '‟': '\"',\n 'ʼ': '’',\n 'ʹ': \"'\",\n '‣': '•',\n '●': '•',\n '▪': '•',\n '◦': 'o',\n '○': 'o',\n '∙': '·',\n '⁄': '/',\n '←': '<-',\n '→': '->',\n '↔': '<->',\n '⇒': '=>',\n '≤': '<=',\n '≥': '>=',\n '≠': '!=',\n '≈': '~',\n '№': 'No.',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n '₹': 'Rs',\n '₩': 'W',\n '₪': 'NIS',\n '₽': 'RUB',\n '₱': 'PHP',\n '₺': 'TL',\n '₫': 'd',\n '₦': 'N',\n '₴': 'UAH',\n 'Ł': 'L',\n 'ł': 'l',\n 'Đ': 'D',\n 'đ': 'd',\n 'Ħ': 'H',\n 'ħ': 'h',\n 'ı': 'i',\n 'Ŀ': 'L',\n 'ŀ': 'l',\n 'Ŋ': 'N',\n 'ŋ': 'n',\n 'Ŧ': 'T',\n 'ŧ': 't',\n 'ſ': 's',\n 'IJ': 'IJ',\n 'ij': 'ij',\n 'ȷ': 'j',\n 'Ə': 'E',\n 'ə': 'e',\n}\n\n/** Whether WinAnsi holds this one character. */\nfunction encodable(code: number): boolean {\n return (code >= 32 && code <= 126) || (code >= 0xa0 && code <= 0xff) || WIN_ANSI_HIGH[code] !== undefined\n}\n\n/** Marks with no glyph of their own in a text font: combining accents, variation selectors, emoji, private use. */\nfunction dropped(code: number): boolean {\n return (\n (code >= 0x0300 && code <= 0x036f) ||\n (code >= 0x1ab0 && code <= 0x1aff) ||\n (code >= 0x20d0 && code <= 0x20ff) ||\n (code >= 0xfe00 && code <= 0xfe0f) ||\n (code >= 0xfe20 && code <= 0xfe2f) ||\n (code >= 0x2600 && code <= 0x27bf) ||\n (code >= 0x2b00 && code <= 0x2bff) ||\n (code >= 0xe000 && code <= 0xf8ff) ||\n code >= 0x1f000 ||\n code < 32 ||\n (code >= 0x7f && code < 0xa0)\n )\n}\n\n/** One character as text WinAnsi holds, without the decomposition fallback. */\nfunction direct(char: string): string | null {\n const code = char.codePointAt(0) ?? 0\n if (encodable(code)) return char\n const standIn = STAND_INS[char]\n if (standIn !== undefined) return standIn\n if (dropped(code)) return ''\n return null\n}\n\n/**\n * `text` as characters WinAnsi holds, line breaks kept (`\\r\\n` and `\\r`\n * become `\\n`). Text that is already WinAnsi comes back unchanged.\n */\nexport function toWinAnsiText(text: string): string {\n const source = String(text ?? '')\n .replace(/\\r\\n?/g, '\\n')\n .normalize('NFC')\n let out = ''\n for (const char of source) {\n if (char === '\\n') {\n out += char\n continue\n }\n const kept = direct(char)\n if (kept !== null) {\n out += kept\n continue\n }\n // An accented letter or a compatibility form: its parts, accents dropped.\n let parts = ''\n let readable = true\n for (const part of char.normalize('NFKD')) {\n const mapped = direct(part)\n if (mapped === null) {\n readable = false\n break\n }\n parts += mapped\n }\n out += readable && parts ? parts : '?'\n }\n return out\n}\n\n/** One character's WinAnsi byte, `?` (63) for a character WinAnsi does not hold. */\nexport function winAnsiByte(char: string): number {\n const code = char.codePointAt(0) ?? 63\n if ((code >= 32 && code <= 126) || (code >= 0xa0 && code <= 0xff)) return code\n return WIN_ANSI_HIGH[code] ?? 63\n}\n\n/**\n * A PDF string literal's body for `text`: WinAnsi bytes, `\\`, `(` and `)`\n * escaped, and every byte outside printable ASCII as an octal escape, so the\n * literal itself is plain ASCII. A line break prints as a space.\n */\nexport function pdfString(text: string): string {\n let out = ''\n for (const char of toWinAnsiText(text)) {\n const byte = char === '\\n' ? 32 : winAnsiByte(char)\n if (byte === 0x5c || byte === 0x28 || byte === 0x29) out += `\\\\${String.fromCharCode(byte)}`\n else if (byte < 32 || byte > 126) out += `\\\\${byte.toString(8).padStart(3, '0')}`\n else out += String.fromCharCode(byte)\n }\n return out\n}\n\n/*==========================================\n * WIDTHS\n *=========================================*/\n\n/** Helvetica advance widths for WinAnsi bytes 32–255, 1/1000 em; 0 where WinAnsi has no character. */\nconst HELVETICA: readonly number[] = [\n 278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278,\n 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556,\n 1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778,\n 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556,\n 333, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556,\n 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584, 0,\n 556, 0, 222, 556, 333, 1000, 556, 556, 333, 1000, 667, 333, 1000, 0, 611, 0,\n 0, 222, 222, 333, 333, 350, 556, 1000, 333, 1000, 500, 333, 944, 0, 500, 667,\n 278, 333, 556, 556, 556, 556, 260, 556, 333, 737, 370, 556, 584, 333, 737, 333,\n 400, 584, 333, 333, 333, 556, 537, 278, 333, 333, 365, 556, 834, 834, 834, 611,\n 667, 667, 667, 667, 667, 667, 1000, 722, 667, 667, 667, 667, 278, 278, 278, 278,\n 722, 722, 778, 778, 778, 778, 778, 584, 778, 722, 722, 722, 722, 667, 667, 611,\n 556, 556, 556, 556, 556, 556, 889, 500, 556, 556, 556, 556, 278, 278, 278, 278,\n 556, 556, 556, 556, 556, 556, 556, 584, 611, 556, 556, 556, 556, 500, 556, 500,\n]\n\n/** Helvetica-Bold advance widths for WinAnsi bytes 32–255, 1/1000 em. */\nconst HELVETICA_BOLD: readonly number[] = [\n 278, 333, 474, 556, 556, 889, 722, 238, 333, 333, 389, 584, 278, 333, 278, 278,\n 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 333, 333, 584, 584, 584, 611,\n 975, 722, 722, 722, 722, 667, 611, 778, 722, 278, 556, 722, 611, 833, 722, 778,\n 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 333, 278, 333, 584, 556,\n 333, 556, 611, 556, 611, 556, 333, 611, 611, 278, 278, 556, 278, 889, 611, 611,\n 611, 611, 389, 556, 333, 611, 556, 778, 556, 556, 500, 389, 280, 389, 584, 0,\n 556, 0, 278, 556, 500, 1000, 556, 556, 333, 1000, 667, 333, 1000, 0, 611, 0,\n 0, 278, 278, 500, 500, 350, 556, 1000, 333, 1000, 556, 333, 944, 0, 500, 667,\n 278, 333, 556, 556, 556, 556, 280, 556, 333, 737, 370, 556, 584, 333, 737, 333,\n 400, 584, 333, 333, 333, 611, 556, 278, 333, 333, 365, 556, 834, 834, 834, 611,\n 722, 722, 722, 722, 722, 722, 1000, 722, 667, 667, 667, 667, 278, 278, 278, 278,\n 722, 722, 778, 778, 778, 778, 778, 584, 778, 722, 722, 722, 722, 667, 667, 611,\n 556, 556, 556, 556, 556, 556, 889, 556, 556, 556, 556, 556, 278, 278, 278, 278,\n 611, 611, 611, 611, 611, 611, 611, 584, 611, 611, 611, 611, 611, 556, 611, 556,\n]\n\n/** `text`'s width in points at `size`, as a reader draws it in `font`. */\nexport function measureText(text: string, size: number, font: PdfFont = 'regular'): number {\n const table = font === 'bold' ? HELVETICA_BOLD : HELVETICA\n let total = 0\n for (const char of toWinAnsiText(text)) {\n if (char === '\\n') continue\n total += table[winAnsiByte(char) - 32] ?? 556\n }\n return (total / 1000) * size\n}\n\n/** Words wrapped into lines no wider than `width` by `measure`; an overlong word is broken. */\nfunction wrapWith(text: string, width: number, measure: (line: string) => number): string[] {\n const lines: string[] = []\n for (const paragraph of String(text ?? '').split('\\n')) {\n const words = paragraph.split(/\\s+/).filter(Boolean)\n if (!words.length) {\n lines.push('')\n continue\n }\n let line = ''\n for (let word of words) {\n while (measure(word) > width) {\n let cut = word.length - 1\n while (cut > 1 && measure(word.slice(0, cut)) > width) cut -= 1\n if (line) {\n lines.push(line)\n line = ''\n }\n lines.push(word.slice(0, cut))\n word = word.slice(cut)\n }\n const next = line ? `${line} ${word}` : word\n if (measure(next) > width && line) {\n lines.push(line)\n line = word\n } else {\n line = next\n }\n }\n if (line) lines.push(line)\n }\n return lines\n}\n\n/**\n * `text` wrapped to `width` points at `size` in `font`, by the exact widths.\n * Each `\\n` starts a new line; a blank line comes back as `''`.\n */\nexport function wrapLines(text: string, size: number, width: number, font: PdfFont = 'regular'): string[] {\n return wrapWith(toWinAnsiText(text), width, (line) => measureText(line, size, font))\n}\n\n/*==========================================\n * THE QUICK ESTIMATE\n *\n * The first hand-written document (the waivers plugin's signed copy) wraps by\n * this cheaper estimate: Helvetica's widths for ASCII, an em-ish 556 for the\n * rest, and bold as 7% wider. It stays as it was so that document's bytes\n * never move; new layouts use `measureText` and `wrapLines`.\n *=========================================*/\n\nfunction estimatedWidth(byte: number): number {\n if (byte >= 32 && byte <= 126) return HELVETICA[byte - 32]\n if (byte === 0x95) return 350\n if (byte === 0x97 || byte === 0x85) return 1000\n return 556\n}\n\n/** A string's estimated width in points at `size`; bold runs 7% wider. */\nexport function textWidth(text: string, size: number, bold = false): number {\n let total = 0\n for (const char of toWinAnsiText(text)) total += estimatedWidth(winAnsiByte(char))\n return (total / 1000) * size * (bold ? 1.07 : 1)\n}\n\n/** Words wrapped by {@link textWidth} into lines no wider than `width`; an overlong word is broken. */\nexport function wrapText(text: string, size: number, width: number, bold = false): string[] {\n return wrapWith(text, width, (line) => textWidth(line, size, bold))\n}\n"],"names":["WIN_ANSI_HIGH","STAND_INS","encodable","code","undefined","dropped","direct","char","codePointAt","standIn","toWinAnsiText","text","source","String","replace","normalize","out","kept","parts","readable","part","mapped","winAnsiByte","pdfString","byte","fromCharCode","toString","padStart","HELVETICA","HELVETICA_BOLD","measureText","size","font","table","total","wrapWith","width","measure","lines","paragraph","split","words","filter","Boolean","length","push","line","word","cut","slice","next","wrapLines","estimatedWidth","textWidth","bold","wrapText"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;2CAuB2C,GAE3C,gDAAgD,GAGhD,qEAAqE,GACrE,MAAMA,gBAAkD;IACtD,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,QA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,IAAY,EAAEoB,IAAY,EAAEK,KAAa,EAAEJ,OAAgB,SAAS;IAC5F,OAAOG,SAASzB,cAAcC,OAAOyB,OAAO,CAACU,OAAShB,YAAYgB,MAAMf,MAAMC;AAChF;AAEA;;;;;;;2CAO2C,GAE3C,SAASoB,eAAe5B,IAAY;IAClC,IAAIA,QAAQ,MAAMA,QAAQ,KAAK,OAAOI,SAAS,CAACJ,OAAO,GAAG;IAC1D,IAAIA,SAAS,MAAM,OAAO;IAC1B,IAAIA,SAAS,QAAQA,SAAS,MAAM,OAAO;IAC3C,OAAO;AACT;AAEA,wEAAwE,GACxE,OAAO,SAAS6B,UAAU1C,IAAY,EAAEoB,IAAY,EAAEuB,OAAO,KAAK;IAChE,IAAIpB,QAAQ;IACZ,KAAK,MAAM3B,QAAQG,cAAcC,MAAOuB,SAASkB,eAAe9B,YAAYf;IAC5E,OAAO,AAAC2B,QAAQ,OAAQH,OAAQuB,CAAAA,OAAO,OAAO,CAAA;AAChD;AAEA,qGAAqG,GACrG,OAAO,SAASC,SAAS5C,IAAY,EAAEoB,IAAY,EAAEK,KAAa,EAAEkB,OAAO,KAAK;IAC9E,OAAOnB,SAASxB,MAAMyB,OAAO,CAACU,OAASO,UAAUP,MAAMf,MAAMuB;AAC/D"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { type PdfJpegImage } from './pdf-file';
|
|
18
|
+
import { type PdfFont } from './pdf-text';
|
|
19
|
+
/** A color as red, green and blue from 0 to 1. */
|
|
20
|
+
export type PdfRgb = readonly [number, number, number];
|
|
21
|
+
/** `#rgb` / `#rrggbb` (alpha ignored) as a {@link PdfRgb}, or null for anything else. */
|
|
22
|
+
export declare function pdfRgbFromHex(hex: string | null | undefined): PdfRgb | null;
|
|
23
|
+
export type PdfAlign = 'left' | 'right' | 'center';
|
|
24
|
+
export interface PdfTextStyle {
|
|
25
|
+
font?: PdfFont;
|
|
26
|
+
size?: number;
|
|
27
|
+
color?: PdfRgb;
|
|
28
|
+
}
|
|
29
|
+
export interface PdfMargins {
|
|
30
|
+
top: number;
|
|
31
|
+
right: number;
|
|
32
|
+
bottom: number;
|
|
33
|
+
left: number;
|
|
34
|
+
}
|
|
35
|
+
export interface PdfWriterOptions {
|
|
36
|
+
title: string;
|
|
37
|
+
producer?: string;
|
|
38
|
+
/** Points; US letter by default. */
|
|
39
|
+
pageWidth?: number;
|
|
40
|
+
pageHeight?: number;
|
|
41
|
+
/** One number for all four sides, or each; 54 (three quarters of an inch) by default. */
|
|
42
|
+
margin?: number | PdfMargins;
|
|
43
|
+
/** Runs at the top of every page after the first, before anything else is drawn on it. */
|
|
44
|
+
onNewPage?: (writer: PdfWriter) => void;
|
|
45
|
+
}
|
|
46
|
+
export interface PdfParagraphOptions {
|
|
47
|
+
x?: number;
|
|
48
|
+
width?: number;
|
|
49
|
+
style?: PdfTextStyle;
|
|
50
|
+
/** Line height as a multiple of the font size; 1.35 by default. */
|
|
51
|
+
leading?: number;
|
|
52
|
+
align?: PdfAlign;
|
|
53
|
+
/** Space left below the paragraph. */
|
|
54
|
+
after?: number;
|
|
55
|
+
/** Each line drawn is also a link to this address. */
|
|
56
|
+
link?: string;
|
|
57
|
+
}
|
|
58
|
+
/** One paragraph inside a table cell, in its own style. */
|
|
59
|
+
export interface PdfCellText {
|
|
60
|
+
text: string;
|
|
61
|
+
style?: PdfTextStyle;
|
|
62
|
+
}
|
|
63
|
+
/** A table cell: one paragraph in the table's body style, or several, each styled. */
|
|
64
|
+
export type PdfTableCell = string | readonly PdfCellText[];
|
|
65
|
+
export interface PdfTableColumn {
|
|
66
|
+
header: string;
|
|
67
|
+
/** Points; 0 shares out what the fixed columns leave. */
|
|
68
|
+
width: number;
|
|
69
|
+
align?: PdfAlign;
|
|
70
|
+
}
|
|
71
|
+
export interface PdfTableOptions {
|
|
72
|
+
columns: readonly PdfTableColumn[];
|
|
73
|
+
rows: ReadonlyArray<readonly PdfTableCell[]>;
|
|
74
|
+
x?: number;
|
|
75
|
+
width?: number;
|
|
76
|
+
headerStyle?: PdfTextStyle;
|
|
77
|
+
/** The header row's ground; none by default. */
|
|
78
|
+
headerFill?: PdfRgb | null;
|
|
79
|
+
bodyStyle?: PdfTextStyle;
|
|
80
|
+
/** Space inside each cell. */
|
|
81
|
+
padding?: {
|
|
82
|
+
x: number;
|
|
83
|
+
y: number;
|
|
84
|
+
};
|
|
85
|
+
leading?: number;
|
|
86
|
+
/** A hairline under each row; none by default. */
|
|
87
|
+
ruleColor?: PdfRgb | null;
|
|
88
|
+
}
|
|
89
|
+
/** What the footer prints on one page, at its left, center and right. */
|
|
90
|
+
export interface PdfFooterText {
|
|
91
|
+
left?: string;
|
|
92
|
+
center?: string;
|
|
93
|
+
right?: string;
|
|
94
|
+
}
|
|
95
|
+
export interface PdfFinishOptions {
|
|
96
|
+
footer?: (page: number, pageCount: number) => PdfFooterText | null;
|
|
97
|
+
footerStyle?: PdfTextStyle;
|
|
98
|
+
/** A hairline above the footer, in this color. */
|
|
99
|
+
footerRule?: PdfRgb | null;
|
|
100
|
+
}
|
|
101
|
+
export declare class PdfWriter {
|
|
102
|
+
readonly pageWidth: number;
|
|
103
|
+
readonly pageHeight: number;
|
|
104
|
+
readonly margin: PdfMargins;
|
|
105
|
+
/** The top of whatever is drawn next, in points from the bottom of the page. */
|
|
106
|
+
y: number;
|
|
107
|
+
private readonly options;
|
|
108
|
+
private readonly pages;
|
|
109
|
+
private readonly images;
|
|
110
|
+
/** The page drawing goes to when it is not the last one (the footers, drawn at the end). */
|
|
111
|
+
private target;
|
|
112
|
+
constructor(options: PdfWriterOptions);
|
|
113
|
+
/** Where content starts on a page. */
|
|
114
|
+
get top(): number;
|
|
115
|
+
/** Where content must stop on a page. */
|
|
116
|
+
get bottom(): number;
|
|
117
|
+
get left(): number;
|
|
118
|
+
get right(): number;
|
|
119
|
+
get contentWidth(): number;
|
|
120
|
+
get pageCount(): number;
|
|
121
|
+
/** What is left of this page below the cursor. */
|
|
122
|
+
get room(): number;
|
|
123
|
+
private get page();
|
|
124
|
+
/** Starts a new page and puts the cursor at its top. */
|
|
125
|
+
addPage(): void;
|
|
126
|
+
/** Starts a new page when `height` would not fit below the cursor; true when it did. */
|
|
127
|
+
ensure(height: number): boolean;
|
|
128
|
+
/** Moves the cursor down. */
|
|
129
|
+
down(points: number): void;
|
|
130
|
+
measure(text: string, style?: PdfTextStyle): number;
|
|
131
|
+
wrap(text: string, width: number, style?: PdfTextStyle): string[];
|
|
132
|
+
/** A raw operator string on the current page. */
|
|
133
|
+
raw(operators: string): void;
|
|
134
|
+
/** One line of text with its baseline at `y`. */
|
|
135
|
+
text(x: number, y: number, text: string, style?: PdfTextStyle): void;
|
|
136
|
+
/** One line of text placed in the span from `x` across `width` by `align`. */
|
|
137
|
+
textIn(x: number, width: number, y: number, text: string, style?: PdfTextStyle, align?: PdfAlign): void;
|
|
138
|
+
fillRect(x: number, y: number, width: number, height: number, color: PdfRgb): void;
|
|
139
|
+
strokeRect(x: number, y: number, width: number, height: number, color: PdfRgb, lineWidth?: number): void;
|
|
140
|
+
line(x1: number, y1: number, x2: number, y2: number, color: PdfRgb, lineWidth?: number): void;
|
|
141
|
+
/** A clickable area opening `url` (web and mail addresses only). */
|
|
142
|
+
link(x: number, y: number, width: number, height: number, url: string): void;
|
|
143
|
+
/**
|
|
144
|
+
* A JPEG fitted into the box whose bottom-left is (`x`, `y`): `contain`
|
|
145
|
+
* keeps its shape inside the box (placed by `align`, at the box's top),
|
|
146
|
+
* `fill` stretches it to the box. Returns where it was drawn.
|
|
147
|
+
*/
|
|
148
|
+
image(image: PdfJpegImage, box: {
|
|
149
|
+
x: number;
|
|
150
|
+
y: number;
|
|
151
|
+
width: number;
|
|
152
|
+
height: number;
|
|
153
|
+
}, options?: {
|
|
154
|
+
fit?: 'contain' | 'fill';
|
|
155
|
+
align?: PdfAlign;
|
|
156
|
+
}): {
|
|
157
|
+
x: number;
|
|
158
|
+
y: number;
|
|
159
|
+
width: number;
|
|
160
|
+
height: number;
|
|
161
|
+
};
|
|
162
|
+
/** Wrapped text from the cursor down, onto further pages as needed. */
|
|
163
|
+
paragraph(text: string, options?: PdfParagraphOptions): void;
|
|
164
|
+
/**
|
|
165
|
+
* A table from the cursor down. Cells wrap to their column; a row moves to
|
|
166
|
+
* the next page whole when it fits on one, and splits between its lines
|
|
167
|
+
* when it is taller than a page. The header row is drawn again at the top
|
|
168
|
+
* of every page the table reaches.
|
|
169
|
+
*/
|
|
170
|
+
table(options: PdfTableOptions): void;
|
|
171
|
+
/** The finished file, with each page's footer drawn now that the page count is known. */
|
|
172
|
+
finish(options?: PdfFinishOptions): Uint8Array;
|
|
173
|
+
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2026 Aglyn LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/ import { rgbChannelsOfHex } from "../contrast.js";
|
|
18
|
+
import { buildPdfFile, pdfNumber } from "./pdf-file.js";
|
|
19
|
+
import { measureText, pdfString, wrapLines } from "./pdf-text.js";
|
|
20
|
+
/** `#rgb` / `#rrggbb` (alpha ignored) as a {@link PdfRgb}, or null for anything else. */ export function pdfRgbFromHex(hex) {
|
|
21
|
+
const channels = typeof hex === 'string' ? rgbChannelsOfHex(hex) : null;
|
|
22
|
+
return channels ? [
|
|
23
|
+
channels[0] / 255,
|
|
24
|
+
channels[1] / 255,
|
|
25
|
+
channels[2] / 255
|
|
26
|
+
] : null;
|
|
27
|
+
}
|
|
28
|
+
/** Black ink: what text is drawn in when a style names no color. */ const INK = [
|
|
29
|
+
0,
|
|
30
|
+
0,
|
|
31
|
+
0
|
|
32
|
+
];
|
|
33
|
+
const rgb = (color)=>color.map((channel)=>pdfNumber(Math.min(1, Math.max(0, channel)))).join(' ');
|
|
34
|
+
const FONT_RESOURCE = {
|
|
35
|
+
regular: 'F1',
|
|
36
|
+
bold: 'F2',
|
|
37
|
+
italic: 'F3'
|
|
38
|
+
};
|
|
39
|
+
export class PdfWriter {
|
|
40
|
+
/** Where content starts on a page. */ get top() {
|
|
41
|
+
return this.pageHeight - this.margin.top;
|
|
42
|
+
}
|
|
43
|
+
/** Where content must stop on a page. */ get bottom() {
|
|
44
|
+
return this.margin.bottom;
|
|
45
|
+
}
|
|
46
|
+
get left() {
|
|
47
|
+
return this.margin.left;
|
|
48
|
+
}
|
|
49
|
+
get right() {
|
|
50
|
+
return this.pageWidth - this.margin.right;
|
|
51
|
+
}
|
|
52
|
+
get contentWidth() {
|
|
53
|
+
return this.right - this.left;
|
|
54
|
+
}
|
|
55
|
+
get pageCount() {
|
|
56
|
+
return this.pages.length;
|
|
57
|
+
}
|
|
58
|
+
/** What is left of this page below the cursor. */ get room() {
|
|
59
|
+
return this.y - this.bottom;
|
|
60
|
+
}
|
|
61
|
+
get page() {
|
|
62
|
+
var _this_target;
|
|
63
|
+
return (_this_target = this.target) != null ? _this_target : this.pages[this.pages.length - 1];
|
|
64
|
+
}
|
|
65
|
+
/** Starts a new page and puts the cursor at its top. */ addPage() {
|
|
66
|
+
this.pages.push({
|
|
67
|
+
ops: [],
|
|
68
|
+
links: []
|
|
69
|
+
});
|
|
70
|
+
this.y = this.top;
|
|
71
|
+
this.options.onNewPage == null ? void 0 : this.options.onNewPage.call(this.options, this);
|
|
72
|
+
}
|
|
73
|
+
/** Starts a new page when `height` would not fit below the cursor; true when it did. */ ensure(height) {
|
|
74
|
+
if (this.y - height >= this.bottom) return false;
|
|
75
|
+
this.addPage();
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
/** Moves the cursor down. */ down(points) {
|
|
79
|
+
this.y -= points;
|
|
80
|
+
}
|
|
81
|
+
measure(text, style = {}) {
|
|
82
|
+
var _style_size, _style_font;
|
|
83
|
+
return measureText(text, (_style_size = style.size) != null ? _style_size : 10, (_style_font = style.font) != null ? _style_font : 'regular');
|
|
84
|
+
}
|
|
85
|
+
wrap(text, width, style = {}) {
|
|
86
|
+
var _style_size, _style_font;
|
|
87
|
+
return wrapLines(text, (_style_size = style.size) != null ? _style_size : 10, width, (_style_font = style.font) != null ? _style_font : 'regular');
|
|
88
|
+
}
|
|
89
|
+
/** A raw operator string on the current page. */ raw(operators) {
|
|
90
|
+
this.page.ops.push(operators);
|
|
91
|
+
}
|
|
92
|
+
/** One line of text with its baseline at `y`. */ text(x, y, text, style = {}) {
|
|
93
|
+
var _style_size, _style_color, _style_font;
|
|
94
|
+
const body = pdfString(text);
|
|
95
|
+
if (!body) return;
|
|
96
|
+
const size = (_style_size = style.size) != null ? _style_size : 10;
|
|
97
|
+
this.page.ops.push(`BT ${rgb((_style_color = style.color) != null ? _style_color : INK)} rg /${FONT_RESOURCE[(_style_font = style.font) != null ? _style_font : 'regular']} ${pdfNumber(size)} Tf ` + `${pdfNumber(x)} ${pdfNumber(y)} Td (${body}) Tj ET`);
|
|
98
|
+
}
|
|
99
|
+
/** One line of text placed in the span from `x` across `width` by `align`. */ textIn(x, width, y, text, style = {}, align = 'left') {
|
|
100
|
+
const measured = this.measure(text, style);
|
|
101
|
+
const at = align === 'right' ? x + width - measured : align === 'center' ? x + (width - measured) / 2 : x;
|
|
102
|
+
this.text(at, y, text, style);
|
|
103
|
+
}
|
|
104
|
+
fillRect(x, y, width, height, color) {
|
|
105
|
+
this.page.ops.push(`q ${rgb(color)} rg ${pdfNumber(x)} ${pdfNumber(y)} ${pdfNumber(width)} ${pdfNumber(height)} re f Q`);
|
|
106
|
+
}
|
|
107
|
+
strokeRect(x, y, width, height, color, lineWidth = 0.75) {
|
|
108
|
+
this.page.ops.push(`q ${rgb(color)} RG ${pdfNumber(lineWidth)} w ${pdfNumber(x)} ${pdfNumber(y)} ${pdfNumber(width)} ${pdfNumber(height)} re S Q`);
|
|
109
|
+
}
|
|
110
|
+
line(x1, y1, x2, y2, color, lineWidth = 0.75) {
|
|
111
|
+
this.page.ops.push(`q ${rgb(color)} RG ${pdfNumber(lineWidth)} w ${pdfNumber(x1)} ${pdfNumber(y1)} m ${pdfNumber(x2)} ${pdfNumber(y2)} l S Q`);
|
|
112
|
+
}
|
|
113
|
+
/** A clickable area opening `url` (web and mail addresses only). */ link(x, y, width, height, url) {
|
|
114
|
+
this.page.links.push({
|
|
115
|
+
x,
|
|
116
|
+
y,
|
|
117
|
+
width,
|
|
118
|
+
height,
|
|
119
|
+
url
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* A JPEG fitted into the box whose bottom-left is (`x`, `y`): `contain`
|
|
124
|
+
* keeps its shape inside the box (placed by `align`, at the box's top),
|
|
125
|
+
* `fill` stretches it to the box. Returns where it was drawn.
|
|
126
|
+
*/ image(image, box, options = {}) {
|
|
127
|
+
var _options_fit;
|
|
128
|
+
let index = this.images.indexOf(image);
|
|
129
|
+
if (index < 0) {
|
|
130
|
+
this.images.push(image);
|
|
131
|
+
index = this.images.length - 1;
|
|
132
|
+
}
|
|
133
|
+
let { x, y, width, height } = box;
|
|
134
|
+
if (((_options_fit = options.fit) != null ? _options_fit : 'contain') === 'contain') {
|
|
135
|
+
var _options_align;
|
|
136
|
+
const scale = Math.min(box.width / image.width, box.height / image.height);
|
|
137
|
+
width = image.width * scale;
|
|
138
|
+
height = image.height * scale;
|
|
139
|
+
const align = (_options_align = options.align) != null ? _options_align : 'left';
|
|
140
|
+
x = align === 'right' ? box.x + box.width - width : align === 'center' ? box.x + (box.width - width) / 2 : box.x;
|
|
141
|
+
y = box.y + box.height - height;
|
|
142
|
+
}
|
|
143
|
+
this.page.ops.push(`q ${pdfNumber(width)} 0 0 ${pdfNumber(height)} ${pdfNumber(x)} ${pdfNumber(y)} cm /Im${index + 1} Do Q`);
|
|
144
|
+
return {
|
|
145
|
+
x,
|
|
146
|
+
y,
|
|
147
|
+
width,
|
|
148
|
+
height
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** Wrapped text from the cursor down, onto further pages as needed. */ paragraph(text, options = {}) {
|
|
152
|
+
var _options_style, _style_size, _options_leading, _options_x, _options_width, _options_after;
|
|
153
|
+
const style = (_options_style = options.style) != null ? _options_style : {};
|
|
154
|
+
const size = (_style_size = style.size) != null ? _style_size : 10;
|
|
155
|
+
const leading = size * ((_options_leading = options.leading) != null ? _options_leading : 1.35);
|
|
156
|
+
const x = (_options_x = options.x) != null ? _options_x : this.left;
|
|
157
|
+
const width = (_options_width = options.width) != null ? _options_width : this.right - x;
|
|
158
|
+
for (const line of this.wrap(text, width, style)){
|
|
159
|
+
this.ensure(leading);
|
|
160
|
+
this.y -= leading;
|
|
161
|
+
if (!line) continue;
|
|
162
|
+
// The baseline sits a fifth of the line above its bottom, so descenders clear the next line.
|
|
163
|
+
const baseline = this.y + (leading - size) / 2 + size * 0.2;
|
|
164
|
+
this.textIn(x, width, baseline, line, style, options.align);
|
|
165
|
+
if (options.link) {
|
|
166
|
+
const measured = this.measure(line, style);
|
|
167
|
+
const at = options.align === 'right' ? x + width - measured : options.align === 'center' ? x + (width - measured) / 2 : x;
|
|
168
|
+
this.link(at, this.y, measured, leading, options.link);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
this.y -= (_options_after = options.after) != null ? _options_after : 0;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* A table from the cursor down. Cells wrap to their column; a row moves to
|
|
175
|
+
* the next page whole when it fits on one, and splits between its lines
|
|
176
|
+
* when it is taller than a page. The header row is drawn again at the top
|
|
177
|
+
* of every page the table reaches.
|
|
178
|
+
*/ table(options) {
|
|
179
|
+
var _options_x, _options_width, _options_padding, _options_leading, _options_bodyStyle, _options_headerStyle, _bodyStyle_size, _headerStyle_size, _bodyStyle_size1;
|
|
180
|
+
const x = (_options_x = options.x) != null ? _options_x : this.left;
|
|
181
|
+
const width = (_options_width = options.width) != null ? _options_width : this.right - x;
|
|
182
|
+
const padding = (_options_padding = options.padding) != null ? _options_padding : {
|
|
183
|
+
x: 6,
|
|
184
|
+
y: 5
|
|
185
|
+
};
|
|
186
|
+
const leadingRatio = (_options_leading = options.leading) != null ? _options_leading : 1.3;
|
|
187
|
+
const bodyStyle = (_options_bodyStyle = options.bodyStyle) != null ? _options_bodyStyle : {};
|
|
188
|
+
const headerStyle = (_options_headerStyle = options.headerStyle) != null ? _options_headerStyle : {
|
|
189
|
+
font: 'bold',
|
|
190
|
+
size: (_bodyStyle_size = bodyStyle.size) != null ? _bodyStyle_size : 10
|
|
191
|
+
};
|
|
192
|
+
const fixed = options.columns.reduce((sum, column)=>sum + Math.max(0, column.width), 0);
|
|
193
|
+
const flexible = options.columns.filter((column)=>!(column.width > 0)).length;
|
|
194
|
+
const share = flexible ? Math.max(0, width - fixed) / flexible : 0;
|
|
195
|
+
const widths = options.columns.map((column)=>column.width > 0 ? column.width : share);
|
|
196
|
+
const lefts = widths.map((_, index)=>x + widths.slice(0, index).reduce((sum, value)=>sum + value, 0));
|
|
197
|
+
const cellLines = (cell, column)=>{
|
|
198
|
+
const parts = typeof cell === 'string' ? [
|
|
199
|
+
{
|
|
200
|
+
text: cell,
|
|
201
|
+
style: bodyStyle
|
|
202
|
+
}
|
|
203
|
+
] : cell;
|
|
204
|
+
const lines = [];
|
|
205
|
+
for (const part of parts){
|
|
206
|
+
var _part_style, _style_size;
|
|
207
|
+
const style = _extends({}, bodyStyle, (_part_style = part.style) != null ? _part_style : {});
|
|
208
|
+
const height = ((_style_size = style.size) != null ? _style_size : 10) * leadingRatio;
|
|
209
|
+
for (const text of this.wrap(part.text, widths[column] - padding.x * 2, style))lines.push({
|
|
210
|
+
text,
|
|
211
|
+
style,
|
|
212
|
+
height
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return lines;
|
|
216
|
+
};
|
|
217
|
+
const headerSize = (_headerStyle_size = headerStyle.size) != null ? _headerStyle_size : 10;
|
|
218
|
+
const headerHeight = headerSize * leadingRatio + padding.y * 2;
|
|
219
|
+
const drawHeader = ()=>{
|
|
220
|
+
if (options.headerFill) this.fillRect(x, this.y - headerHeight, width, headerHeight, options.headerFill);
|
|
221
|
+
const baseline = this.y - padding.y - headerSize * leadingRatio + (headerSize * leadingRatio - headerSize) / 2 + headerSize * 0.2;
|
|
222
|
+
options.columns.forEach((column, index)=>{
|
|
223
|
+
this.textIn(lefts[index] + padding.x, widths[index] - padding.x * 2, baseline, column.header, headerStyle, column.align);
|
|
224
|
+
});
|
|
225
|
+
this.y -= headerHeight;
|
|
226
|
+
};
|
|
227
|
+
const startPage = ()=>{
|
|
228
|
+
this.addPage();
|
|
229
|
+
drawHeader();
|
|
230
|
+
};
|
|
231
|
+
const drawLines = (cells, height)=>{
|
|
232
|
+
const top = this.y;
|
|
233
|
+
cells.forEach((lines, index)=>{
|
|
234
|
+
let y = top - padding.y;
|
|
235
|
+
for (const line of lines){
|
|
236
|
+
var _line_style_size;
|
|
237
|
+
y -= line.height;
|
|
238
|
+
const size = (_line_style_size = line.style.size) != null ? _line_style_size : 10;
|
|
239
|
+
const baseline = y + (line.height - size) / 2 + size * 0.2;
|
|
240
|
+
if (line.text) {
|
|
241
|
+
this.textIn(lefts[index] + padding.x, widths[index] - padding.x * 2, baseline, line.text, line.style, options.columns[index].align);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
this.y = top - height;
|
|
246
|
+
if (options.ruleColor) this.line(x, this.y, x + width, this.y, options.ruleColor, 0.5);
|
|
247
|
+
};
|
|
248
|
+
const heightOf = (lines)=>lines.reduce((sum, line)=>sum + line.height, 0);
|
|
249
|
+
// The header and one line of the first row go together.
|
|
250
|
+
const firstLine = ((_bodyStyle_size1 = bodyStyle.size) != null ? _bodyStyle_size1 : 10) * leadingRatio;
|
|
251
|
+
if (this.room < headerHeight + firstLine + padding.y * 2) this.addPage();
|
|
252
|
+
drawHeader();
|
|
253
|
+
const freshRoom = this.top - headerHeight - this.bottom;
|
|
254
|
+
for (const row of options.rows){
|
|
255
|
+
let remaining = options.columns.map((_, index)=>{
|
|
256
|
+
var _row_index;
|
|
257
|
+
return cellLines((_row_index = row[index]) != null ? _row_index : '', index);
|
|
258
|
+
});
|
|
259
|
+
const whole = Math.max(...remaining.map(heightOf)) + padding.y * 2;
|
|
260
|
+
if (whole > this.room && whole <= freshRoom) startPage();
|
|
261
|
+
let fresh = false;
|
|
262
|
+
while(remaining.some((lines)=>lines.length)){
|
|
263
|
+
const fits = this.room - padding.y * 2;
|
|
264
|
+
const tallest = Math.max(...remaining.map(heightOf));
|
|
265
|
+
if (tallest <= fits) {
|
|
266
|
+
drawLines(remaining, tallest + padding.y * 2);
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
let taken = remaining.map((lines)=>{
|
|
270
|
+
let used = 0;
|
|
271
|
+
let count = 0;
|
|
272
|
+
while(count < lines.length && used + lines[count].height <= fits)used += lines[count++].height;
|
|
273
|
+
return count;
|
|
274
|
+
});
|
|
275
|
+
if (taken.every((count)=>count === 0)) {
|
|
276
|
+
if (!fresh) {
|
|
277
|
+
startPage();
|
|
278
|
+
fresh = true;
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
// Not one line fits even a fresh page: draw a line of each cell anyway, so the table always ends.
|
|
282
|
+
taken = remaining.map((lines)=>Math.min(1, lines.length));
|
|
283
|
+
}
|
|
284
|
+
const chunk = remaining.map((lines, index)=>lines.slice(0, taken[index]));
|
|
285
|
+
drawLines(chunk, Math.max(...chunk.map(heightOf)) + padding.y * 2);
|
|
286
|
+
remaining = remaining.map((lines, index)=>lines.slice(taken[index]));
|
|
287
|
+
startPage();
|
|
288
|
+
fresh = true;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/** The finished file, with each page's footer drawn now that the page count is known. */ finish(options = {}) {
|
|
293
|
+
const total = this.pages.length;
|
|
294
|
+
if (options.footer) {
|
|
295
|
+
var _options_footerStyle;
|
|
296
|
+
const style = _extends({
|
|
297
|
+
size: 8
|
|
298
|
+
}, (_options_footerStyle = options.footerStyle) != null ? _options_footerStyle : {});
|
|
299
|
+
const baseline = this.margin.bottom / 2;
|
|
300
|
+
this.pages.forEach((page, index)=>{
|
|
301
|
+
const footer = options.footer == null ? void 0 : options.footer.call(options, index + 1, total);
|
|
302
|
+
if (!footer) return;
|
|
303
|
+
this.target = page;
|
|
304
|
+
if (options.footerRule) this.line(this.left, baseline + 12, this.right, baseline + 12, options.footerRule, 0.5);
|
|
305
|
+
if (footer.left) this.textIn(this.left, this.contentWidth, baseline, footer.left, style, 'left');
|
|
306
|
+
if (footer.center) this.textIn(this.left, this.contentWidth, baseline, footer.center, style, 'center');
|
|
307
|
+
if (footer.right) this.textIn(this.left, this.contentWidth, baseline, footer.right, style, 'right');
|
|
308
|
+
});
|
|
309
|
+
this.target = null;
|
|
310
|
+
}
|
|
311
|
+
return buildPdfFile(_extends({
|
|
312
|
+
title: this.options.title
|
|
313
|
+
}, this.options.producer !== undefined ? {
|
|
314
|
+
producer: this.options.producer
|
|
315
|
+
} : {}, {
|
|
316
|
+
images: this.images,
|
|
317
|
+
pages: this.pages.map((page)=>({
|
|
318
|
+
width: this.pageWidth,
|
|
319
|
+
height: this.pageHeight,
|
|
320
|
+
content: page.ops.join('\n'),
|
|
321
|
+
links: page.links
|
|
322
|
+
}))
|
|
323
|
+
}));
|
|
324
|
+
}
|
|
325
|
+
constructor(options){
|
|
326
|
+
var _options_pageWidth, _options_pageHeight, _options_margin;
|
|
327
|
+
this.pages = [];
|
|
328
|
+
this.images = [];
|
|
329
|
+
/** The page drawing goes to when it is not the last one (the footers, drawn at the end). */ this.target = null;
|
|
330
|
+
this.options = options;
|
|
331
|
+
this.pageWidth = (_options_pageWidth = options.pageWidth) != null ? _options_pageWidth : 612;
|
|
332
|
+
this.pageHeight = (_options_pageHeight = options.pageHeight) != null ? _options_pageHeight : 792;
|
|
333
|
+
const margin = (_options_margin = options.margin) != null ? _options_margin : 54;
|
|
334
|
+
this.margin = typeof margin === 'number' ? {
|
|
335
|
+
top: margin,
|
|
336
|
+
right: margin,
|
|
337
|
+
bottom: margin,
|
|
338
|
+
left: margin
|
|
339
|
+
} : margin;
|
|
340
|
+
this.pages.push({
|
|
341
|
+
ops: [],
|
|
342
|
+
links: []
|
|
343
|
+
});
|
|
344
|
+
this.y = this.top;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
//# sourceMappingURL=pdf-writer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../libs/shared/util/tools/src/lib/pdf/pdf-writer.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { rgbChannelsOfHex } from '../contrast'\nimport { buildPdfFile, pdfNumber, type PdfJpegImage, type PdfLinkArea } from './pdf-file'\nimport { measureText, pdfString, wrapLines, type PdfFont } from './pdf-text'\n\n/*==========================================\n * A FLOWING PAGE LAYOUT over `buildPdfFile`: a cursor that moves down the\n * page and breaks onto a new one when the next thing would not fit.\n *\n * Coordinates are PDF points from the BOTTOM-left corner (72 to the inch).\n * `y` is the top of whatever is drawn next; the content area runs from the\n * top margin down to the bottom margin, and the footer sits in the bottom\n * margin, drawn when the document is finished and the page count is known.\n *\n * What it draws: text (left, right or centered, in a color), paragraphs that\n * wrap and run onto the next page, filled and stroked rectangles, rules, JPEG\n * images fitted to a box, clickable links, and tables whose cells wrap, whose\n * rows split across a page break when they must, and whose header row repeats\n * on every page the table reaches. Every string passes through the WinAnsi\n * mapping in `pdf-text.ts`, so no input can put a byte in a content stream\n * that the font cannot draw.\n *\n * Reached as `@aglyn/shared-util-tools/pdf/pdf-writer`.\n *=========================================*/\n\n/** A color as red, green and blue from 0 to 1. */\nexport type PdfRgb = readonly [number, number, number]\n\n/** `#rgb` / `#rrggbb` (alpha ignored) as a {@link PdfRgb}, or null for anything else. */\nexport function pdfRgbFromHex(hex: string | null | undefined): PdfRgb | null {\n const channels = typeof hex === 'string' ? rgbChannelsOfHex(hex) : null\n return channels ? [channels[0] / 255, channels[1] / 255, channels[2] / 255] : null\n}\n\n/** Black ink: what text is drawn in when a style names no color. */\nconst INK: PdfRgb = [0, 0, 0]\n\nexport type PdfAlign = 'left' | 'right' | 'center'\n\nexport interface PdfTextStyle {\n font?: PdfFont\n size?: number\n color?: PdfRgb\n}\n\nexport interface PdfMargins {\n top: number\n right: number\n bottom: number\n left: number\n}\n\nexport interface PdfWriterOptions {\n title: string\n producer?: string\n /** Points; US letter by default. */\n pageWidth?: number\n pageHeight?: number\n /** One number for all four sides, or each; 54 (three quarters of an inch) by default. */\n margin?: number | PdfMargins\n /** Runs at the top of every page after the first, before anything else is drawn on it. */\n onNewPage?: (writer: PdfWriter) => void\n}\n\nexport interface PdfParagraphOptions {\n x?: number\n width?: number\n style?: PdfTextStyle\n /** Line height as a multiple of the font size; 1.35 by default. */\n leading?: number\n align?: PdfAlign\n /** Space left below the paragraph. */\n after?: number\n /** Each line drawn is also a link to this address. */\n link?: string\n}\n\n/** One paragraph inside a table cell, in its own style. */\nexport interface PdfCellText {\n text: string\n style?: PdfTextStyle\n}\n\n/** A table cell: one paragraph in the table's body style, or several, each styled. */\nexport type PdfTableCell = string | readonly PdfCellText[]\n\nexport interface PdfTableColumn {\n header: string\n /** Points; 0 shares out what the fixed columns leave. */\n width: number\n align?: PdfAlign\n}\n\nexport interface PdfTableOptions {\n columns: readonly PdfTableColumn[]\n rows: ReadonlyArray<readonly PdfTableCell[]>\n x?: number\n width?: number\n headerStyle?: PdfTextStyle\n /** The header row's ground; none by default. */\n headerFill?: PdfRgb | null\n bodyStyle?: PdfTextStyle\n /** Space inside each cell. */\n padding?: { x: number; y: number }\n leading?: number\n /** A hairline under each row; none by default. */\n ruleColor?: PdfRgb | null\n}\n\n/** What the footer prints on one page, at its left, center and right. */\nexport interface PdfFooterText {\n left?: string\n center?: string\n right?: string\n}\n\nexport interface PdfFinishOptions {\n footer?: (page: number, pageCount: number) => PdfFooterText | null\n footerStyle?: PdfTextStyle\n /** A hairline above the footer, in this color. */\n footerRule?: PdfRgb | null\n}\n\ninterface Page {\n ops: string[]\n links: PdfLinkArea[]\n}\n\nconst rgb = (color: PdfRgb) => color.map((channel) => pdfNumber(Math.min(1, Math.max(0, channel)))).join(' ')\n\nconst FONT_RESOURCE: Record<PdfFont, string> = { regular: 'F1', bold: 'F2', italic: 'F3' }\n\nexport class PdfWriter {\n readonly pageWidth: number\n readonly pageHeight: number\n readonly margin: PdfMargins\n /** The top of whatever is drawn next, in points from the bottom of the page. */\n y: number\n private readonly options: PdfWriterOptions\n private readonly pages: Page[] = []\n private readonly images: PdfJpegImage[] = []\n /** The page drawing goes to when it is not the last one (the footers, drawn at the end). */\n private target: Page | null = null\n\n constructor(options: PdfWriterOptions) {\n this.options = options\n this.pageWidth = options.pageWidth ?? 612\n this.pageHeight = options.pageHeight ?? 792\n const margin = options.margin ?? 54\n this.margin = typeof margin === 'number' ? { top: margin, right: margin, bottom: margin, left: margin } : margin\n this.pages.push({ ops: [], links: [] })\n this.y = this.top\n }\n\n /** Where content starts on a page. */\n get top(): number {\n return this.pageHeight - this.margin.top\n }\n\n /** Where content must stop on a page. */\n get bottom(): number {\n return this.margin.bottom\n }\n\n get left(): number {\n return this.margin.left\n }\n\n get right(): number {\n return this.pageWidth - this.margin.right\n }\n\n get contentWidth(): number {\n return this.right - this.left\n }\n\n get pageCount(): number {\n return this.pages.length\n }\n\n /** What is left of this page below the cursor. */\n get room(): number {\n return this.y - this.bottom\n }\n\n private get page(): Page {\n return this.target ?? this.pages[this.pages.length - 1]\n }\n\n /** Starts a new page and puts the cursor at its top. */\n addPage(): void {\n this.pages.push({ ops: [], links: [] })\n this.y = this.top\n this.options.onNewPage?.(this)\n }\n\n /** Starts a new page when `height` would not fit below the cursor; true when it did. */\n ensure(height: number): boolean {\n if (this.y - height >= this.bottom) return false\n this.addPage()\n return true\n }\n\n /** Moves the cursor down. */\n down(points: number): void {\n this.y -= points\n }\n\n measure(text: string, style: PdfTextStyle = {}): number {\n return measureText(text, style.size ?? 10, style.font ?? 'regular')\n }\n\n wrap(text: string, width: number, style: PdfTextStyle = {}): string[] {\n return wrapLines(text, style.size ?? 10, width, style.font ?? 'regular')\n }\n\n /** A raw operator string on the current page. */\n raw(operators: string): void {\n this.page.ops.push(operators)\n }\n\n /** One line of text with its baseline at `y`. */\n text(x: number, y: number, text: string, style: PdfTextStyle = {}): void {\n const body = pdfString(text)\n if (!body) return\n const size = style.size ?? 10\n this.page.ops.push(\n `BT ${rgb(style.color ?? INK)} rg /${FONT_RESOURCE[style.font ?? 'regular']} ${pdfNumber(size)} Tf ` +\n `${pdfNumber(x)} ${pdfNumber(y)} Td (${body}) Tj ET`,\n )\n }\n\n /** One line of text placed in the span from `x` across `width` by `align`. */\n textIn(x: number, width: number, y: number, text: string, style: PdfTextStyle = {}, align: PdfAlign = 'left'): void {\n const measured = this.measure(text, style)\n const at = align === 'right' ? x + width - measured : align === 'center' ? x + (width - measured) / 2 : x\n this.text(at, y, text, style)\n }\n\n fillRect(x: number, y: number, width: number, height: number, color: PdfRgb): void {\n this.page.ops.push(`q ${rgb(color)} rg ${pdfNumber(x)} ${pdfNumber(y)} ${pdfNumber(width)} ${pdfNumber(height)} re f Q`)\n }\n\n strokeRect(x: number, y: number, width: number, height: number, color: PdfRgb, lineWidth = 0.75): void {\n this.page.ops.push(\n `q ${rgb(color)} RG ${pdfNumber(lineWidth)} w ${pdfNumber(x)} ${pdfNumber(y)} ${pdfNumber(width)} ${pdfNumber(height)} re S Q`,\n )\n }\n\n line(x1: number, y1: number, x2: number, y2: number, color: PdfRgb, lineWidth = 0.75): void {\n this.page.ops.push(\n `q ${rgb(color)} RG ${pdfNumber(lineWidth)} w ${pdfNumber(x1)} ${pdfNumber(y1)} m ${pdfNumber(x2)} ${pdfNumber(y2)} l S Q`,\n )\n }\n\n /** A clickable area opening `url` (web and mail addresses only). */\n link(x: number, y: number, width: number, height: number, url: string): void {\n this.page.links.push({ x, y, width, height, url })\n }\n\n /**\n * A JPEG fitted into the box whose bottom-left is (`x`, `y`): `contain`\n * keeps its shape inside the box (placed by `align`, at the box's top),\n * `fill` stretches it to the box. Returns where it was drawn.\n */\n image(\n image: PdfJpegImage,\n box: { x: number; y: number; width: number; height: number },\n options: { fit?: 'contain' | 'fill'; align?: PdfAlign } = {},\n ): { x: number; y: number; width: number; height: number } {\n let index = this.images.indexOf(image)\n if (index < 0) {\n this.images.push(image)\n index = this.images.length - 1\n }\n let { x, y, width, height } = box\n if ((options.fit ?? 'contain') === 'contain') {\n const scale = Math.min(box.width / image.width, box.height / image.height)\n width = image.width * scale\n height = image.height * scale\n const align = options.align ?? 'left'\n x = align === 'right' ? box.x + box.width - width : align === 'center' ? box.x + (box.width - width) / 2 : box.x\n y = box.y + box.height - height\n }\n this.page.ops.push(\n `q ${pdfNumber(width)} 0 0 ${pdfNumber(height)} ${pdfNumber(x)} ${pdfNumber(y)} cm /Im${index + 1} Do Q`,\n )\n return { x, y, width, height }\n }\n\n /** Wrapped text from the cursor down, onto further pages as needed. */\n paragraph(text: string, options: PdfParagraphOptions = {}): void {\n const style = options.style ?? {}\n const size = style.size ?? 10\n const leading = size * (options.leading ?? 1.35)\n const x = options.x ?? this.left\n const width = options.width ?? this.right - x\n for (const line of this.wrap(text, width, style)) {\n this.ensure(leading)\n this.y -= leading\n if (!line) continue\n // The baseline sits a fifth of the line above its bottom, so descenders clear the next line.\n const baseline = this.y + (leading - size) / 2 + size * 0.2\n this.textIn(x, width, baseline, line, style, options.align)\n if (options.link) {\n const measured = this.measure(line, style)\n const at = options.align === 'right' ? x + width - measured : options.align === 'center' ? x + (width - measured) / 2 : x\n this.link(at, this.y, measured, leading, options.link)\n }\n }\n this.y -= options.after ?? 0\n }\n\n /**\n * A table from the cursor down. Cells wrap to their column; a row moves to\n * the next page whole when it fits on one, and splits between its lines\n * when it is taller than a page. The header row is drawn again at the top\n * of every page the table reaches.\n */\n table(options: PdfTableOptions): void {\n const x = options.x ?? this.left\n const width = options.width ?? this.right - x\n const padding = options.padding ?? { x: 6, y: 5 }\n const leadingRatio = options.leading ?? 1.3\n const bodyStyle = options.bodyStyle ?? {}\n const headerStyle = options.headerStyle ?? { font: 'bold', size: bodyStyle.size ?? 10 }\n const fixed = options.columns.reduce((sum, column) => sum + Math.max(0, column.width), 0)\n const flexible = options.columns.filter((column) => !(column.width > 0)).length\n const share = flexible ? Math.max(0, width - fixed) / flexible : 0\n const widths = options.columns.map((column) => (column.width > 0 ? column.width : share))\n const lefts = widths.map((_, index) => x + widths.slice(0, index).reduce((sum, value) => sum + value, 0))\n\n type Line = { text: string; style: PdfTextStyle; height: number }\n const cellLines = (cell: PdfTableCell, column: number): Line[] => {\n const parts = typeof cell === 'string' ? [{ text: cell, style: bodyStyle }] : cell\n const lines: Line[] = []\n for (const part of parts) {\n const style = { ...bodyStyle, ...(part.style ?? {}) }\n const height = (style.size ?? 10) * leadingRatio\n for (const text of this.wrap(part.text, widths[column] - padding.x * 2, style)) lines.push({ text, style, height })\n }\n return lines\n }\n\n const headerSize = headerStyle.size ?? 10\n const headerHeight = headerSize * leadingRatio + padding.y * 2\n const drawHeader = () => {\n if (options.headerFill) this.fillRect(x, this.y - headerHeight, width, headerHeight, options.headerFill)\n const baseline = this.y - padding.y - headerSize * leadingRatio + (headerSize * leadingRatio - headerSize) / 2 + headerSize * 0.2\n options.columns.forEach((column, index) => {\n this.textIn(lefts[index] + padding.x, widths[index] - padding.x * 2, baseline, column.header, headerStyle, column.align)\n })\n this.y -= headerHeight\n }\n const startPage = () => {\n this.addPage()\n drawHeader()\n }\n const drawLines = (cells: Line[][], height: number) => {\n const top = this.y\n cells.forEach((lines, index) => {\n let y = top - padding.y\n for (const line of lines) {\n y -= line.height\n const size = line.style.size ?? 10\n const baseline = y + (line.height - size) / 2 + size * 0.2\n if (line.text) {\n this.textIn(lefts[index] + padding.x, widths[index] - padding.x * 2, baseline, line.text, line.style, options.columns[index].align)\n }\n }\n })\n this.y = top - height\n if (options.ruleColor) this.line(x, this.y, x + width, this.y, options.ruleColor, 0.5)\n }\n const heightOf = (lines: Line[]) => lines.reduce((sum, line) => sum + line.height, 0)\n\n // The header and one line of the first row go together.\n const firstLine = (bodyStyle.size ?? 10) * leadingRatio\n if (this.room < headerHeight + firstLine + padding.y * 2) this.addPage()\n drawHeader()\n const freshRoom = this.top - headerHeight - this.bottom\n for (const row of options.rows) {\n let remaining = options.columns.map((_, index) => cellLines(row[index] ?? '', index))\n const whole = Math.max(...remaining.map(heightOf)) + padding.y * 2\n if (whole > this.room && whole <= freshRoom) startPage()\n let fresh = false\n while (remaining.some((lines) => lines.length)) {\n const fits = this.room - padding.y * 2\n const tallest = Math.max(...remaining.map(heightOf))\n if (tallest <= fits) {\n drawLines(remaining, tallest + padding.y * 2)\n break\n }\n let taken = remaining.map((lines) => {\n let used = 0\n let count = 0\n while (count < lines.length && used + lines[count].height <= fits) used += lines[count++].height\n return count\n })\n if (taken.every((count) => count === 0)) {\n if (!fresh) {\n startPage()\n fresh = true\n continue\n }\n // Not one line fits even a fresh page: draw a line of each cell anyway, so the table always ends.\n taken = remaining.map((lines) => Math.min(1, lines.length))\n }\n const chunk = remaining.map((lines, index) => lines.slice(0, taken[index]))\n drawLines(chunk, Math.max(...chunk.map(heightOf)) + padding.y * 2)\n remaining = remaining.map((lines, index) => lines.slice(taken[index]))\n startPage()\n fresh = true\n }\n }\n }\n\n /** The finished file, with each page's footer drawn now that the page count is known. */\n finish(options: PdfFinishOptions = {}): Uint8Array {\n const total = this.pages.length\n if (options.footer) {\n const style: PdfTextStyle = { size: 8, ...(options.footerStyle ?? {}) }\n const baseline = this.margin.bottom / 2\n this.pages.forEach((page, index) => {\n const footer = options.footer?.(index + 1, total)\n if (!footer) return\n this.target = page\n if (options.footerRule) this.line(this.left, baseline + 12, this.right, baseline + 12, options.footerRule, 0.5)\n if (footer.left) this.textIn(this.left, this.contentWidth, baseline, footer.left, style, 'left')\n if (footer.center) this.textIn(this.left, this.contentWidth, baseline, footer.center, style, 'center')\n if (footer.right) this.textIn(this.left, this.contentWidth, baseline, footer.right, style, 'right')\n })\n this.target = null\n }\n return buildPdfFile({\n title: this.options.title,\n ...(this.options.producer !== undefined ? { producer: this.options.producer } : {}),\n images: this.images,\n pages: this.pages.map((page) => ({\n width: this.pageWidth,\n height: this.pageHeight,\n content: page.ops.join('\\n'),\n links: page.links,\n })),\n })\n }\n}\n"],"names":["rgbChannelsOfHex","buildPdfFile","pdfNumber","measureText","pdfString","wrapLines","pdfRgbFromHex","hex","channels","INK","rgb","color","map","channel","Math","min","max","join","FONT_RESOURCE","regular","bold","italic","PdfWriter","top","pageHeight","margin","bottom","left","right","pageWidth","contentWidth","pageCount","pages","length","room","y","page","target","addPage","push","ops","links","options","onNewPage","ensure","height","down","points","measure","text","style","size","font","wrap","width","raw","operators","x","body","textIn","align","measured","at","fillRect","strokeRect","lineWidth","line","x1","y1","x2","y2","link","url","image","box","index","images","indexOf","fit","scale","paragraph","leading","baseline","after","table","bodyStyle","headerStyle","padding","leadingRatio","fixed","columns","reduce","sum","column","flexible","filter","share","widths","lefts","_","slice","value","cellLines","cell","parts","lines","part","headerSize","headerHeight","drawHeader","headerFill","forEach","header","startPage","drawLines","cells","ruleColor","heightOf","firstLine","freshRoom","row","rows","remaining","whole","fresh","some","fits","tallest","taken","used","count","every","chunk","finish","total","footer","footerStyle","footerRule","center","title","producer","undefined","content"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,gBAAgB,QAAQ,iBAAa;AAC9C,SAASC,YAAY,EAAEC,SAAS,QAA6C,gBAAY;AACzF,SAASC,WAAW,EAAEC,SAAS,EAAEC,SAAS,QAAsB,gBAAY;AAyB5E,uFAAuF,GACvF,OAAO,SAASC,cAAcC,GAA8B;IAC1D,MAAMC,WAAW,OAAOD,QAAQ,WAAWP,iBAAiBO,OAAO;IACnE,OAAOC,WAAW;QAACA,QAAQ,CAAC,EAAE,GAAG;QAAKA,QAAQ,CAAC,EAAE,GAAG;QAAKA,QAAQ,CAAC,EAAE,GAAG;KAAI,GAAG;AAChF;AAEA,kEAAkE,GAClE,MAAMC,MAAc;IAAC;IAAG;IAAG;CAAE;AA6F7B,MAAMC,MAAM,CAACC,QAAkBA,MAAMC,GAAG,CAAC,CAACC,UAAYX,UAAUY,KAAKC,GAAG,CAAC,GAAGD,KAAKE,GAAG,CAAC,GAAGH,YAAYI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