@docengine/pdf-renderer 0.0.0-stage → 0.0.106
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 +10 -3
- package/dist/.tsbuildinfo +1 -0
- package/dist/browser.d.ts +16 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +20 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.sf-stub.d.ts +9 -0
- package/dist/browser.sf-stub.d.ts.map +1 -0
- package/dist/browser.sf-stub.js +19 -0
- package/dist/browser.sf-stub.js.map +1 -0
- package/dist/document-pdf-definition-browser.d.ts +10 -0
- package/dist/document-pdf-definition-browser.d.ts.map +1 -0
- package/dist/document-pdf-definition-browser.js +9 -0
- package/dist/document-pdf-definition-browser.js.map +1 -0
- package/dist/document-pdf-definition-factory.d.ts +29 -0
- package/dist/document-pdf-definition-factory.d.ts.map +1 -0
- package/dist/document-pdf-definition-factory.js +170 -0
- package/dist/document-pdf-definition-factory.js.map +1 -0
- package/dist/document-pdf-definition-node.d.ts +10 -0
- package/dist/document-pdf-definition-node.d.ts.map +1 -0
- package/dist/document-pdf-definition-node.js +9 -0
- package/dist/document-pdf-definition-node.js.map +1 -0
- package/dist/document-pdf-definition-preview-browser.d.ts +9 -0
- package/dist/document-pdf-definition-preview-browser.d.ts.map +1 -0
- package/dist/document-pdf-definition-preview-browser.js +4 -0
- package/dist/document-pdf-definition-preview-browser.js.map +1 -0
- package/dist/document-pdf-definition-preview-node.d.ts +9 -0
- package/dist/document-pdf-definition-preview-node.d.ts.map +1 -0
- package/dist/document-pdf-definition-preview-node.js +4 -0
- package/dist/document-pdf-definition-preview-node.js.map +1 -0
- package/dist/document-pdf-definition-preview.d.ts +13 -0
- package/dist/document-pdf-definition-preview.d.ts.map +1 -0
- package/dist/document-pdf-definition-preview.js +42 -0
- package/dist/document-pdf-definition-preview.js.map +1 -0
- package/dist/fonts-browser-registry.d.ts +13 -0
- package/dist/fonts-browser-registry.d.ts.map +1 -0
- package/dist/fonts-browser-registry.js +7 -0
- package/dist/fonts-browser-registry.js.map +1 -0
- package/dist/fonts-browser-vfs.d.ts +12 -0
- package/dist/fonts-browser-vfs.d.ts.map +1 -0
- package/dist/fonts-browser-vfs.js +4 -0
- package/dist/fonts-browser-vfs.js.map +1 -0
- package/dist/fonts-browser-vfs.sf-stub.d.ts +5 -0
- package/dist/fonts-browser-vfs.sf-stub.d.ts.map +1 -0
- package/dist/fonts-browser-vfs.sf-stub.js +5 -0
- package/dist/fonts-browser-vfs.sf-stub.js.map +1 -0
- package/dist/fonts-browser.d.ts +22 -0
- package/dist/fonts-browser.d.ts.map +1 -0
- package/dist/fonts-browser.js +22 -0
- package/dist/fonts-browser.js.map +1 -0
- package/dist/fonts-registry.d.ts +17 -0
- package/dist/fonts-registry.d.ts.map +1 -0
- package/dist/fonts-registry.js +83 -0
- package/dist/fonts-registry.js.map +1 -0
- package/dist/fonts-registry.test.d.ts +2 -0
- package/dist/fonts-registry.test.d.ts.map +1 -0
- package/dist/fonts-registry.test.js +22 -0
- package/dist/fonts-registry.test.js.map +1 -0
- package/dist/fonts.d.ts +16 -0
- package/dist/fonts.d.ts.map +1 -0
- package/dist/fonts.js +17 -0
- package/dist/fonts.js.map +1 -0
- package/dist/generate-html.d.ts +33 -0
- package/dist/generate-html.d.ts.map +1 -0
- package/dist/generate-html.js +90 -0
- package/dist/generate-html.js.map +1 -0
- package/dist/generate-html.test.d.ts +2 -0
- package/dist/generate-html.test.d.ts.map +1 -0
- package/dist/generate-html.test.js +270 -0
- package/dist/generate-html.test.js.map +1 -0
- package/dist/generate-pdf-browser.d.ts +14 -0
- package/dist/generate-pdf-browser.d.ts.map +1 -0
- package/dist/generate-pdf-browser.js +55 -0
- package/dist/generate-pdf-browser.js.map +1 -0
- package/dist/generate-pdf.d.ts +10 -0
- package/dist/generate-pdf.d.ts.map +1 -0
- package/dist/generate-pdf.js +36 -0
- package/dist/generate-pdf.js.map +1 -0
- package/dist/html-environment.d.ts +10 -0
- package/dist/html-environment.d.ts.map +1 -0
- package/dist/html-environment.js +30 -0
- package/dist/html-environment.js.map +1 -0
- package/dist/html-text.d.ts +46 -0
- package/dist/html-text.d.ts.map +1 -0
- package/dist/html-text.js +451 -0
- package/dist/html-text.js.map +1 -0
- package/dist/html-text.test.d.ts +2 -0
- package/dist/html-text.test.d.ts.map +1 -0
- package/dist/html-text.test.js +79 -0
- package/dist/html-text.test.js.map +1 -0
- package/dist/image-prefetch.d.ts +7 -0
- package/dist/image-prefetch.d.ts.map +1 -0
- package/dist/image-prefetch.js +50 -0
- package/dist/image-prefetch.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/map-editor-pdf-options.d.ts +13 -0
- package/dist/map-editor-pdf-options.d.ts.map +1 -0
- package/dist/map-editor-pdf-options.js +28 -0
- package/dist/map-editor-pdf-options.js.map +1 -0
- package/dist/map-editor-pdf-options.test.d.ts +2 -0
- package/dist/map-editor-pdf-options.test.d.ts.map +1 -0
- package/dist/map-editor-pdf-options.test.js +16 -0
- package/dist/map-editor-pdf-options.test.js.map +1 -0
- package/dist/merge-document.d.ts +3 -0
- package/dist/merge-document.d.ts.map +1 -0
- package/dist/merge-document.js +29 -0
- package/dist/merge-document.js.map +1 -0
- package/dist/multipage-renderer.d.ts +31 -0
- package/dist/multipage-renderer.d.ts.map +1 -0
- package/dist/multipage-renderer.js +131 -0
- package/dist/multipage-renderer.js.map +1 -0
- package/dist/pdf-horizontal-rule.d.ts +18 -0
- package/dist/pdf-horizontal-rule.d.ts.map +1 -0
- package/dist/pdf-horizontal-rule.js +46 -0
- package/dist/pdf-horizontal-rule.js.map +1 -0
- package/dist/pdf-inline-field-background.d.ts +19 -0
- package/dist/pdf-inline-field-background.d.ts.map +1 -0
- package/dist/pdf-inline-field-background.js +106 -0
- package/dist/pdf-inline-field-background.js.map +1 -0
- package/dist/pdf-inline-field-background.test.d.ts +2 -0
- package/dist/pdf-inline-field-background.test.d.ts.map +1 -0
- package/dist/pdf-inline-field-background.test.js +22 -0
- package/dist/pdf-inline-field-background.test.js.map +1 -0
- package/dist/pdf-renderer.test.d.ts +2 -0
- package/dist/pdf-renderer.test.d.ts.map +1 -0
- package/dist/pdf-renderer.test.js +2614 -0
- package/dist/pdf-renderer.test.js.map +1 -0
- package/dist/pdfmake.sf-stub.d.ts +11 -0
- package/dist/pdfmake.sf-stub.d.ts.map +1 -0
- package/dist/pdfmake.sf-stub.js +16 -0
- package/dist/pdfmake.sf-stub.js.map +1 -0
- package/dist/preview-dom-to-pdf.d.ts +25 -0
- package/dist/preview-dom-to-pdf.d.ts.map +1 -0
- package/dist/preview-dom-to-pdf.js +694 -0
- package/dist/preview-dom-to-pdf.js.map +1 -0
- package/dist/preview-dom-to-pdf.test.d.ts +2 -0
- package/dist/preview-dom-to-pdf.test.d.ts.map +1 -0
- package/dist/preview-dom-to-pdf.test.js +734 -0
- package/dist/preview-dom-to-pdf.test.js.map +1 -0
- package/dist/preview-field-plugins.d.ts +6 -0
- package/dist/preview-field-plugins.d.ts.map +1 -0
- package/dist/preview-field-plugins.js +37 -0
- package/dist/preview-field-plugins.js.map +1 -0
- package/dist/repeatable-section-header.d.ts +39 -0
- package/dist/repeatable-section-header.d.ts.map +1 -0
- package/dist/repeatable-section-header.js +145 -0
- package/dist/repeatable-section-header.js.map +1 -0
- package/dist/repeatable-table-pagination.d.ts +17 -0
- package/dist/repeatable-table-pagination.d.ts.map +1 -0
- package/dist/repeatable-table-pagination.js +255 -0
- package/dist/repeatable-table-pagination.js.map +1 -0
- package/dist/repeatable-table-pagination.test.d.ts +2 -0
- package/dist/repeatable-table-pagination.test.d.ts.map +1 -0
- package/dist/repeatable-table-pagination.test.js +110 -0
- package/dist/repeatable-table-pagination.test.js.map +1 -0
- package/dist/rich-text-pdf-styles.d.ts +15 -0
- package/dist/rich-text-pdf-styles.d.ts.map +1 -0
- package/dist/rich-text-pdf-styles.js +30 -0
- package/dist/rich-text-pdf-styles.js.map +1 -0
- package/dist/segment-renderer.d.ts +26 -0
- package/dist/segment-renderer.d.ts.map +1 -0
- package/dist/segment-renderer.js +1252 -0
- package/dist/segment-renderer.js.map +1 -0
- package/dist/style-mapper.d.ts +32 -0
- package/dist/style-mapper.d.ts.map +1 -0
- package/dist/style-mapper.js +124 -0
- package/dist/style-mapper.js.map +1 -0
- package/dist/style-mapper.test.d.ts +2 -0
- package/dist/style-mapper.test.d.ts.map +1 -0
- package/dist/style-mapper.test.js +77 -0
- package/dist/style-mapper.test.js.map +1 -0
- package/dist/table-column-width.d.ts +20 -0
- package/dist/table-column-width.d.ts.map +1 -0
- package/dist/table-column-width.js +56 -0
- package/dist/table-column-width.js.map +1 -0
- package/dist/table-column-width.test.d.ts +2 -0
- package/dist/table-column-width.test.d.ts.map +1 -0
- package/dist/table-column-width.test.js +40 -0
- package/dist/table-column-width.test.js.map +1 -0
- package/dist/table-layout.d.ts +44 -0
- package/dist/table-layout.d.ts.map +1 -0
- package/dist/table-layout.js +70 -0
- package/dist/table-layout.js.map +1 -0
- package/dist/types.d.ts +73 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/units.d.ts +29 -0
- package/dist/units.d.ts.map +1 -0
- package/dist/units.js +63 -0
- package/dist/units.js.map +1 -0
- package/package.json +36 -6
|
@@ -0,0 +1,1252 @@
|
|
|
1
|
+
import { buildRepeaterFillDocument, cellFieldId, collectAllValues, DOCUMENT_TABLE_TEXT_STYLE, enrichComputedValues, evaluateComputedField, collapseBlankSpacingBeforeTables, filterSegmentsForPreview, formatFieldDisplay, getFieldDisplayLabel, getFieldHandler, getTableSchema, isFieldEmpty, isTableCellDisplayPlaceholder, normalizeRepeaterValue, repeaterHasContent, resolveTableInstanceRows, tableSegmentHasContent, tableSegmentHasRequiredEmpty, findAdjacentTableBlock, resolvePageSetupFieldValueStyle, resolvePageSetupFieldHighlightStyle, evaluateSectionVisibility, barcodeValueToPdfBlock, } from '@docengine/editor/node';
|
|
2
|
+
import { isPivotTableValue, createEmptyPivotValue, formatNumericDisplay } from '@docengine/engine';
|
|
3
|
+
import { htmlToPdfBlocks, htmlToPdfText, isBlockLevelHtml, pdfBlocksHaveContent, pdfTextContent, plainTextToPdfText, finalizePdfInlineParts, withPdfStyle } from './html-text.js';
|
|
4
|
+
import { resolvePdfFieldStyleForExport, resolvePdfSectionHeaderStyle, resolvePdfTextStyle, resolveTableColumnPdfStyle, DEFAULT_BODY_FONT_PT, DEFAULT_TABLE_FONT_PT, } from './style-mapper.js';
|
|
5
|
+
import { resolveVisionTablePdfLayout, TABLE_PDF_LINE_HEIGHT } from './table-layout.js';
|
|
6
|
+
import { withPdfSectionBorderRules } from './pdf-horizontal-rule.js';
|
|
7
|
+
import { estimateContentWidthPt } from './repeatable-table-pagination.js';
|
|
8
|
+
import { chunkTableBodyRowsVariable, estimateContinuationChunkMaxBodyHeight, estimateFirstChunkMaxBodyHeight, estimatePdfContentHeightPt, repeatableChunkFitsPage, } from './repeatable-table-pagination.js';
|
|
9
|
+
import { usesRepeatablePageHeader, buildRepeatableSectionPageHeader, shouldAttachCompanionLeadingToPageHeader, } from './repeatable-section-header.js';
|
|
10
|
+
import { parseTableColumnWidth, tableColumnWidthsFromSchema } from './table-column-width.js';
|
|
11
|
+
function tableHasRowLabels(rows) {
|
|
12
|
+
return (rows ?? []).some((row) => String(row?.label ?? '').trim() !== '');
|
|
13
|
+
}
|
|
14
|
+
function shouldShowRowLabels(tableSchema, rows) {
|
|
15
|
+
return tableSchema?.showRowLabels === true && tableHasRowLabels(rows);
|
|
16
|
+
}
|
|
17
|
+
function parseColumnWidth(width) {
|
|
18
|
+
return parseTableColumnWidth(width);
|
|
19
|
+
}
|
|
20
|
+
function resolveFieldValue(fieldId, fieldValues, fieldSchemas, blocks) {
|
|
21
|
+
const schema = fieldSchemas?.[fieldId];
|
|
22
|
+
if (schema?.type === 'computed') {
|
|
23
|
+
if (Object.prototype.hasOwnProperty.call(fieldValues, fieldId)) {
|
|
24
|
+
return fieldValues[fieldId];
|
|
25
|
+
}
|
|
26
|
+
return evaluateComputedField(fieldId, fieldValues, fieldSchemas, { blocks }).value;
|
|
27
|
+
}
|
|
28
|
+
return fieldValues[fieldId];
|
|
29
|
+
}
|
|
30
|
+
export function createPdfRenderContext(doc, options) {
|
|
31
|
+
const fieldSchemas = doc.fieldSchemas ?? {};
|
|
32
|
+
const blocks = doc.blocks ?? [];
|
|
33
|
+
const fieldValues = collectAllValues(blocks);
|
|
34
|
+
enrichComputedValues(fieldValues, fieldSchemas, blocks);
|
|
35
|
+
const pageSetup = { ...(doc.pageSetup ?? {}), ...(options.pageSetup ?? {}) };
|
|
36
|
+
const fieldValueStyle = resolvePageSetupFieldValueStyle(pageSetup, options.fieldValueStyle);
|
|
37
|
+
const fieldHighlightStyle = resolvePageSetupFieldHighlightStyle(pageSetup, options.fieldHighlight);
|
|
38
|
+
const textPdfStyle = resolvePdfTextStyle(pageSetup, options.fieldValueStyle, options.resolveFontName);
|
|
39
|
+
const bodyPdfStyle = {
|
|
40
|
+
...textPdfStyle,
|
|
41
|
+
font: textPdfStyle.font ?? options.defaultFont,
|
|
42
|
+
};
|
|
43
|
+
const rawSectionHeaderStyle = resolvePdfSectionHeaderStyle(options.resolveFontName);
|
|
44
|
+
const sectionHeaderPdfStyle = {
|
|
45
|
+
...rawSectionHeaderStyle,
|
|
46
|
+
font: rawSectionHeaderStyle.font ?? options.defaultFont,
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
fieldSchemas,
|
|
50
|
+
blocks,
|
|
51
|
+
fieldValues,
|
|
52
|
+
pageSetup,
|
|
53
|
+
fieldHighlightStyle,
|
|
54
|
+
hideEmptyValues: options.hideEmptyValues === true,
|
|
55
|
+
previewContext: {
|
|
56
|
+
previewMode: true,
|
|
57
|
+
fieldSchemas,
|
|
58
|
+
fieldValueStyle,
|
|
59
|
+
blocks,
|
|
60
|
+
hideEmptyValues: options.hideEmptyValues === true,
|
|
61
|
+
},
|
|
62
|
+
resolveFontName: options.resolveFontName,
|
|
63
|
+
defaultFont: options.defaultFont,
|
|
64
|
+
fieldValueStyle,
|
|
65
|
+
textPdfStyle: bodyPdfStyle,
|
|
66
|
+
bodyPdfStyle,
|
|
67
|
+
sectionHeaderPdfStyle,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export function isHideTitleInPreview(data) {
|
|
71
|
+
const value = data?.hideTitleInPreview;
|
|
72
|
+
return value === true || value === 1 || value === '1' || String(value).toLowerCase() === 'true';
|
|
73
|
+
}
|
|
74
|
+
export function resolveVisibleSectionLabel(data) {
|
|
75
|
+
if (!data || isHideTitleInPreview(data))
|
|
76
|
+
return '';
|
|
77
|
+
return String(data.label ?? '').trim();
|
|
78
|
+
}
|
|
79
|
+
export function buildPdfSectionTitleNode(sectionLabel, sectionHeaderStyle, margin = [0, 0, 0, 6]) {
|
|
80
|
+
const label = String(sectionLabel ?? '').trim();
|
|
81
|
+
if (!label)
|
|
82
|
+
return null;
|
|
83
|
+
return {
|
|
84
|
+
text: label,
|
|
85
|
+
...sectionHeaderStyle,
|
|
86
|
+
style: 'sectionHeader',
|
|
87
|
+
margin,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function stripBlockAlignment(style) {
|
|
91
|
+
const inlineStyle = { ...style };
|
|
92
|
+
delete inlineStyle.alignment;
|
|
93
|
+
// Cell fill belongs on the table cell node, not inline text parts.
|
|
94
|
+
delete inlineStyle.fillColor;
|
|
95
|
+
return inlineStyle;
|
|
96
|
+
}
|
|
97
|
+
/** Table body text: drop both fillColor and background so only td fillColor paints. */
|
|
98
|
+
function stripTableCellInlineStyle(style) {
|
|
99
|
+
const inlineStyle = stripBlockAlignment(style);
|
|
100
|
+
delete inlineStyle.background;
|
|
101
|
+
return inlineStyle;
|
|
102
|
+
}
|
|
103
|
+
function withTableCellFillColor(cell, style) {
|
|
104
|
+
const fillColor = style?.fillColor;
|
|
105
|
+
if (!fillColor)
|
|
106
|
+
return cell;
|
|
107
|
+
if (cell === '' || cell == null) {
|
|
108
|
+
return { text: '', fillColor, lineHeight: TABLE_PDF_LINE_HEIGHT };
|
|
109
|
+
}
|
|
110
|
+
if (typeof cell !== 'object') {
|
|
111
|
+
return { text: cell, fillColor, lineHeight: TABLE_PDF_LINE_HEIGHT };
|
|
112
|
+
}
|
|
113
|
+
return { ...cell, fillColor };
|
|
114
|
+
}
|
|
115
|
+
function pushPdfBlocks(content, blocks, style = {}, blockStyle = {}, defaultAlignment, defaultFont) {
|
|
116
|
+
const inlineStyle = stripBlockAlignment(style);
|
|
117
|
+
const fallbackAlignment = defaultAlignment ?? style.alignment;
|
|
118
|
+
const fontName = String(inlineStyle.font ?? defaultFont ?? '');
|
|
119
|
+
for (const block of blocks) {
|
|
120
|
+
const parts = finalizePdfInlineParts(block.parts, { font: fontName, inlineStyle });
|
|
121
|
+
if (!pdfTextContent(parts))
|
|
122
|
+
continue;
|
|
123
|
+
const node = {
|
|
124
|
+
text: parts,
|
|
125
|
+
margin: block.margin ?? [0, 0, 0, 4],
|
|
126
|
+
...blockStyle,
|
|
127
|
+
};
|
|
128
|
+
const alignment = block.alignment ?? fallbackAlignment;
|
|
129
|
+
if (alignment)
|
|
130
|
+
node.alignment = alignment;
|
|
131
|
+
content.push(node);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function withTableCellLineHeight(cell) {
|
|
135
|
+
if (cell === '' || cell == null)
|
|
136
|
+
return cell;
|
|
137
|
+
if (typeof cell !== 'object')
|
|
138
|
+
return { text: cell, lineHeight: TABLE_PDF_LINE_HEIGHT };
|
|
139
|
+
return { ...cell, lineHeight: TABLE_PDF_LINE_HEIGHT };
|
|
140
|
+
}
|
|
141
|
+
function pdfBlocksToTableCell(blocks, style) {
|
|
142
|
+
const cellAlignment = style.alignment;
|
|
143
|
+
const inlineStyle = { lineHeight: TABLE_PDF_LINE_HEIGHT, ...stripTableCellInlineStyle(style) };
|
|
144
|
+
const fontName = String(inlineStyle.font ?? '');
|
|
145
|
+
const nodes = [];
|
|
146
|
+
for (const block of blocks) {
|
|
147
|
+
const parts = finalizePdfInlineParts(block.parts, { font: fontName, inlineStyle });
|
|
148
|
+
if (!pdfTextContent(parts))
|
|
149
|
+
continue;
|
|
150
|
+
const node = { text: parts };
|
|
151
|
+
if (block.alignment)
|
|
152
|
+
node.alignment = block.alignment;
|
|
153
|
+
else if (cellAlignment)
|
|
154
|
+
node.alignment = cellAlignment;
|
|
155
|
+
nodes.push(node);
|
|
156
|
+
}
|
|
157
|
+
let cell;
|
|
158
|
+
if (!nodes.length)
|
|
159
|
+
cell = '';
|
|
160
|
+
else if (nodes.length === 1)
|
|
161
|
+
cell = withTableCellLineHeight(nodes[0]);
|
|
162
|
+
else
|
|
163
|
+
cell = withTableCellLineHeight({ stack: nodes });
|
|
164
|
+
return withTableCellFillColor(cell, style);
|
|
165
|
+
}
|
|
166
|
+
function buildEmptyPdfTableCell(cellId, ctx) {
|
|
167
|
+
const style = resolvePdfFieldStyleForExport(cellId, ctx, DOCUMENT_TABLE_TEXT_STYLE);
|
|
168
|
+
const cell = {
|
|
169
|
+
text: '',
|
|
170
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
171
|
+
fontSize: style.fontSize ?? DEFAULT_TABLE_FONT_PT,
|
|
172
|
+
};
|
|
173
|
+
if (style.font)
|
|
174
|
+
cell.font = style.font;
|
|
175
|
+
if (style.alignment)
|
|
176
|
+
cell.alignment = style.alignment;
|
|
177
|
+
return withTableCellFillColor(cell, style);
|
|
178
|
+
}
|
|
179
|
+
export function renderSegmentsToPdfContent(segments, ctx) {
|
|
180
|
+
return renderSegmentsToPdfProseBlocks(segments, ctx);
|
|
181
|
+
}
|
|
182
|
+
function createProseParagraphBuilder(content, defaultFont) {
|
|
183
|
+
let paragraphParts = [];
|
|
184
|
+
let blockStyle = {};
|
|
185
|
+
let currentAlign;
|
|
186
|
+
/** After omitting an empty field, collapse a doubled newline once:
|
|
187
|
+
* `main\n` + [empty] + `\namount` → `main\namount` (not a blank line). */
|
|
188
|
+
let collapseDoubledNewline = false;
|
|
189
|
+
return {
|
|
190
|
+
ensureAlignment(align) {
|
|
191
|
+
const nextAlign = align ?? null;
|
|
192
|
+
if (currentAlign !== undefined && nextAlign !== currentAlign) {
|
|
193
|
+
this.flush();
|
|
194
|
+
}
|
|
195
|
+
currentAlign = nextAlign;
|
|
196
|
+
},
|
|
197
|
+
appendParts(parts) {
|
|
198
|
+
let next = parts;
|
|
199
|
+
if (collapseDoubledNewline && Array.isArray(next) && next.length) {
|
|
200
|
+
if (partsEndWithNewline(paragraphParts) && partsStartWithNewline(next)) {
|
|
201
|
+
next = stripLeadingNewlineFromParts(next);
|
|
202
|
+
}
|
|
203
|
+
collapseDoubledNewline = false;
|
|
204
|
+
}
|
|
205
|
+
paragraphParts.push(...next);
|
|
206
|
+
},
|
|
207
|
+
setBlockStyle(style) {
|
|
208
|
+
blockStyle = style;
|
|
209
|
+
},
|
|
210
|
+
/** Call when an optional empty field is skipped between text segments. */
|
|
211
|
+
noteOmittedField() {
|
|
212
|
+
collapseDoubledNewline = true;
|
|
213
|
+
},
|
|
214
|
+
flush(options = {}) {
|
|
215
|
+
let sourceParts = paragraphParts;
|
|
216
|
+
if (options.trimTrailingBreaks) {
|
|
217
|
+
sourceParts = stripTrailingNewlinesFromParts(sourceParts);
|
|
218
|
+
}
|
|
219
|
+
const text = pdfTextContent(sourceParts);
|
|
220
|
+
if (!text) {
|
|
221
|
+
paragraphParts = [];
|
|
222
|
+
blockStyle = {};
|
|
223
|
+
currentAlign = undefined;
|
|
224
|
+
collapseDoubledNewline = false;
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const parts = finalizePdfInlineParts(sourceParts, { font: defaultFont });
|
|
228
|
+
content.push({ text: parts, margin: [0, 0, 0, 0], ...blockStyle });
|
|
229
|
+
paragraphParts = [];
|
|
230
|
+
blockStyle = {};
|
|
231
|
+
currentAlign = undefined;
|
|
232
|
+
collapseDoubledNewline = false;
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function partTextValue(part) {
|
|
237
|
+
if (typeof part === 'string')
|
|
238
|
+
return part;
|
|
239
|
+
return String(part?.text ?? '');
|
|
240
|
+
}
|
|
241
|
+
function partsEndWithNewline(parts) {
|
|
242
|
+
for (let i = parts.length - 1; i >= 0; i -= 1) {
|
|
243
|
+
const text = partTextValue(parts[i]);
|
|
244
|
+
if (!text)
|
|
245
|
+
continue;
|
|
246
|
+
return /\n[^\S\n]*$/.test(text);
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
function partsStartWithNewline(parts) {
|
|
251
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
252
|
+
const text = partTextValue(parts[i]);
|
|
253
|
+
if (!text)
|
|
254
|
+
continue;
|
|
255
|
+
return /^[^\S\n]*\n/.test(text);
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
function stripLeadingNewlineFromParts(parts) {
|
|
260
|
+
if (!parts.length)
|
|
261
|
+
return parts;
|
|
262
|
+
const first = parts[0];
|
|
263
|
+
const text = partTextValue(first);
|
|
264
|
+
if (!/^[^\S\n]*\n/.test(text))
|
|
265
|
+
return parts;
|
|
266
|
+
const trimmed = text.replace(/^[^\S\n]*\n/, '');
|
|
267
|
+
if (!trimmed)
|
|
268
|
+
return parts.slice(1);
|
|
269
|
+
if (typeof first === 'string')
|
|
270
|
+
return [trimmed, ...parts.slice(1)];
|
|
271
|
+
return [{ ...first, text: trimmed }, ...parts.slice(1)];
|
|
272
|
+
}
|
|
273
|
+
/** Remove all trailing newlines before a block-level node (table / columns). */
|
|
274
|
+
function stripTrailingNewlinesFromParts(parts) {
|
|
275
|
+
const next = [...parts];
|
|
276
|
+
while (next.length > 0) {
|
|
277
|
+
const last = next[next.length - 1];
|
|
278
|
+
const text = partTextValue(last);
|
|
279
|
+
if (!text) {
|
|
280
|
+
next.pop();
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (!/\n/.test(text))
|
|
284
|
+
break;
|
|
285
|
+
const trimmed = text.replace(/[^\S\n]*\n+[^\S\n]*$/g, '').replace(/[ \t]+$/g, '');
|
|
286
|
+
if (!trimmed) {
|
|
287
|
+
next.pop();
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
if (trimmed === text)
|
|
291
|
+
break;
|
|
292
|
+
if (typeof last === 'string')
|
|
293
|
+
next[next.length - 1] = trimmed;
|
|
294
|
+
else
|
|
295
|
+
next[next.length - 1] = { ...last, text: trimmed };
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
return next;
|
|
299
|
+
}
|
|
300
|
+
function appendPlainTextToProseParagraph(raw, inlineStyle, builder) {
|
|
301
|
+
const value = String(raw ?? '');
|
|
302
|
+
if (!value)
|
|
303
|
+
return;
|
|
304
|
+
builder.appendParts(withPdfStyle(plainTextToPdfText(value), stripBlockAlignment(inlineStyle)));
|
|
305
|
+
}
|
|
306
|
+
export function renderSegmentsToPdfProseBlocks(segments, ctx) {
|
|
307
|
+
const content = [];
|
|
308
|
+
const documentFont = String(ctx.bodyPdfStyle.font ?? ctx.defaultFont ?? '');
|
|
309
|
+
const builder = createProseParagraphBuilder(content, documentFont || undefined);
|
|
310
|
+
const htmlFontOptions = {
|
|
311
|
+
baseFontSize: Number(ctx.bodyPdfStyle.fontSize) || DEFAULT_BODY_FONT_PT,
|
|
312
|
+
baseFont: documentFont || undefined,
|
|
313
|
+
};
|
|
314
|
+
const normalizedSegments = collapseBlankSpacingBeforeTables(segments ?? []);
|
|
315
|
+
for (const seg of normalizedSegments) {
|
|
316
|
+
const segAlign = seg.align;
|
|
317
|
+
const alignmentStyle = segAlign ? { alignment: segAlign } : {};
|
|
318
|
+
if (seg.type === 'text') {
|
|
319
|
+
if (seg.html) {
|
|
320
|
+
const html = String(seg.html ?? '');
|
|
321
|
+
if (isBlockLevelHtml(html)) {
|
|
322
|
+
builder.flush();
|
|
323
|
+
pushPdfBlocks(content, htmlToPdfBlocks(html, htmlFontOptions), ctx.bodyPdfStyle, alignmentStyle, segAlign, documentFont || undefined);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
builder.ensureAlignment(segAlign ?? null);
|
|
327
|
+
builder.setBlockStyle(alignmentStyle);
|
|
328
|
+
builder.appendParts(withPdfStyle(htmlToPdfText(html, htmlFontOptions), stripBlockAlignment(ctx.bodyPdfStyle)));
|
|
329
|
+
}
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
builder.ensureAlignment(segAlign ?? null);
|
|
333
|
+
builder.setBlockStyle(alignmentStyle);
|
|
334
|
+
appendPlainTextToProseParagraph(String(seg.content ?? ''), ctx.bodyPdfStyle, builder);
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (seg.type === 'field' || seg.type === 'repeater' || seg.type === 'child') {
|
|
338
|
+
const fieldId = String(seg.id ?? '');
|
|
339
|
+
const schema = ctx.fieldSchemas[fieldId];
|
|
340
|
+
if (schema?.type === 'child' || schema?.type === 'repeater') {
|
|
341
|
+
builder.flush();
|
|
342
|
+
const repeaterBlock = renderRepeaterFieldPdf(fieldId, ctx);
|
|
343
|
+
if (repeaterBlock)
|
|
344
|
+
content.push(repeaterBlock);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
const value = resolveFieldValue(fieldId, ctx.fieldValues, ctx.fieldSchemas, ctx.blocks);
|
|
348
|
+
const empty = isPdfFieldEmpty(value, schema);
|
|
349
|
+
// Optional empties are omitted; required empties keep their placeholder.
|
|
350
|
+
// Collapse the newline that usually wraps the field so gaps stay even across rows.
|
|
351
|
+
if (empty && !schema?.required) {
|
|
352
|
+
builder.noteOmittedField();
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
const label = getFieldDisplayLabel(fieldId, seg.placeholder, ctx.previewContext);
|
|
356
|
+
const style = resolvePdfFieldStyleForExport(fieldId, ctx);
|
|
357
|
+
const baseFontSize = Number(style.fontSize ?? ctx.bodyPdfStyle.fontSize) || DEFAULT_BODY_FONT_PT;
|
|
358
|
+
const fieldAlignment = segAlign ?? style.alignment;
|
|
359
|
+
const fieldBlockStyle = fieldAlignment ? { alignment: fieldAlignment } : {};
|
|
360
|
+
if (empty && schema?.required) {
|
|
361
|
+
const requiredStyle = { ...stripBlockAlignment(style), italics: true, color: '#888888' };
|
|
362
|
+
const requiredParts = plainTextToPdfText(String(label ?? ''));
|
|
363
|
+
builder.ensureAlignment(fieldAlignment ?? null);
|
|
364
|
+
builder.setBlockStyle(fieldBlockStyle);
|
|
365
|
+
builder.appendParts(withPdfStyle(requiredParts, requiredStyle));
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
// Barcode: emit svg image block (JSON→PDF path has no preview DOM images).
|
|
369
|
+
if (schema?.type === 'barcode') {
|
|
370
|
+
builder.flush();
|
|
371
|
+
const barcodeBlock = barcodeValueToPdfBlock(value, schema);
|
|
372
|
+
if (barcodeBlock) {
|
|
373
|
+
if (fieldAlignment) {
|
|
374
|
+
content.push({ ...barcodeBlock, alignment: fieldAlignment });
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
content.push(barcodeBlock);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
// Invalid code — fall back to plain text so the value is not lost.
|
|
382
|
+
builder.ensureAlignment(fieldAlignment ?? null);
|
|
383
|
+
builder.setBlockStyle(fieldBlockStyle);
|
|
384
|
+
builder.appendParts(withPdfStyle(fieldValueToPdfParts(fieldId, value, schema, label, ctx), stripBlockAlignment(style)));
|
|
385
|
+
builder.flush();
|
|
386
|
+
}
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const pdfMode = resolvePdfRenderMode(schema);
|
|
390
|
+
if (pdfMode === 'html') {
|
|
391
|
+
builder.flush();
|
|
392
|
+
const htmlBlocks = htmlToPdfBlocks(String(value ?? ''), {
|
|
393
|
+
baseFontSize,
|
|
394
|
+
baseFont: String(style.font ?? ctx.defaultFont ?? ''),
|
|
395
|
+
});
|
|
396
|
+
pushPdfBlocks(content, htmlBlocks, style, fieldBlockStyle, fieldAlignment, String(style.font ?? ctx.defaultFont ?? ''));
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
builder.ensureAlignment(fieldAlignment ?? null);
|
|
400
|
+
builder.setBlockStyle(fieldBlockStyle);
|
|
401
|
+
builder.appendParts(withPdfStyle(fieldValueToPdfParts(fieldId, value, schema, label, ctx), stripBlockAlignment(style)));
|
|
402
|
+
}
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
if (seg.type === 'columns') {
|
|
406
|
+
builder.flush({ trimTrailingBreaks: true });
|
|
407
|
+
const columnBlocks = renderColumnsSegmentPdf(seg, ctx);
|
|
408
|
+
if (columnBlocks)
|
|
409
|
+
content.push(columnBlocks);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
if (seg.type === 'table') {
|
|
413
|
+
builder.flush({ trimTrailingBreaks: true });
|
|
414
|
+
const tableBlock = renderTableSegmentPdf(seg, ctx);
|
|
415
|
+
if (tableBlock)
|
|
416
|
+
content.push(tableBlock);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
builder.flush({ trimTrailingBreaks: true });
|
|
420
|
+
return content;
|
|
421
|
+
}
|
|
422
|
+
function renderColumnsSegmentPdf(seg, ctx) {
|
|
423
|
+
const cols = seg.columns ?? [[], []];
|
|
424
|
+
const widths = seg.widths ?? [];
|
|
425
|
+
const left = renderSegmentsToPdfContent(cols[0] ?? [], ctx);
|
|
426
|
+
const right = renderSegmentsToPdfContent(cols[1] ?? [], ctx);
|
|
427
|
+
if (!left.length && !right.length)
|
|
428
|
+
return null;
|
|
429
|
+
const w0 = parseColumnWidth(widths[0]);
|
|
430
|
+
const w1 = parseColumnWidth(widths[1]);
|
|
431
|
+
return {
|
|
432
|
+
columns: [
|
|
433
|
+
{ width: w0, stack: left.length ? left : [{ text: '' }] },
|
|
434
|
+
{ width: w1, stack: right.length ? right : [{ text: '' }] },
|
|
435
|
+
],
|
|
436
|
+
columnGap: 12,
|
|
437
|
+
margin: [0, 0, 0, 8],
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
function resolvePdfRenderMode(schema) {
|
|
441
|
+
return (getFieldHandler(schema?.type)?.pdfRenderMode?.(schema) ??
|
|
442
|
+
(schema?.type === 'text' && schema?.htmlEditor ? 'html' : 'plain'));
|
|
443
|
+
}
|
|
444
|
+
function isPdfFieldEmpty(value, schema) {
|
|
445
|
+
if (resolvePdfRenderMode(schema) === 'html') {
|
|
446
|
+
return !pdfBlocksHaveContent(htmlToPdfBlocks(String(value ?? '')));
|
|
447
|
+
}
|
|
448
|
+
return isFieldEmpty(value, {
|
|
449
|
+
schema,
|
|
450
|
+
htmlEditor: !!schema?.htmlEditor,
|
|
451
|
+
repeaterSchema: schema?.type === 'child' ? schema : undefined,
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
function fieldValueToPdfParts(fieldId, value, _schema, placeholder, ctx) {
|
|
455
|
+
const display = formatFieldDisplay(fieldId, value, placeholder, ctx.previewContext);
|
|
456
|
+
return plainTextToPdfText(String(display ?? '').replace(/\u200B/g, ''));
|
|
457
|
+
}
|
|
458
|
+
function tableHasPreviewContent(tableId, ctx, segmentRows) {
|
|
459
|
+
return (tableSegmentHasContent(tableId, ctx.fieldValues, ctx.fieldSchemas, segmentRows) ||
|
|
460
|
+
tableSegmentHasRequiredEmpty(tableId, ctx.fieldValues, ctx.fieldSchemas, segmentRows));
|
|
461
|
+
}
|
|
462
|
+
function formatPivotPdfCell(value, emptyCell = '', schema = {}) {
|
|
463
|
+
if (value == null || value === '')
|
|
464
|
+
return emptyCell;
|
|
465
|
+
const formatted = formatNumericDisplay(value, {
|
|
466
|
+
displayFormat: schema.displayFormat,
|
|
467
|
+
currencyCode: schema.currencyCode,
|
|
468
|
+
fractionDigits: schema.fractionDigits,
|
|
469
|
+
suffix: schema.suffix,
|
|
470
|
+
});
|
|
471
|
+
if (formatted !== '')
|
|
472
|
+
return formatted;
|
|
473
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
474
|
+
return Number.isInteger(value) ? String(value) : String(Math.round(value * 100) / 100);
|
|
475
|
+
}
|
|
476
|
+
return String(value);
|
|
477
|
+
}
|
|
478
|
+
function pivotValueAlign(schema = {}) {
|
|
479
|
+
const align = schema.valueAlign;
|
|
480
|
+
if (align === 'left' || align === 'center' || align === 'right')
|
|
481
|
+
return align;
|
|
482
|
+
return 'right';
|
|
483
|
+
}
|
|
484
|
+
function buildPivotPdfTable(tableId, ctx) {
|
|
485
|
+
const schema = ctx.fieldSchemas?.[tableId] ?? {};
|
|
486
|
+
const raw = ctx.fieldValues?.[tableId];
|
|
487
|
+
const pivot = isPivotTableValue(raw) ? raw : createEmptyPivotValue();
|
|
488
|
+
const columns = pivot.columns ?? [];
|
|
489
|
+
const rows = pivot.rows ?? [];
|
|
490
|
+
if (!rows.length && !columns.length)
|
|
491
|
+
return null;
|
|
492
|
+
const emptyCell = schema.emptyCell != null ? String(schema.emptyCell) : '';
|
|
493
|
+
const showRowTotals = !!schema.showRowTotals;
|
|
494
|
+
const showColumnTotals = !!schema.showColumnTotals;
|
|
495
|
+
const showGrandTotal = !!schema.showGrandTotal;
|
|
496
|
+
const valueAlign = pivotValueAlign(schema);
|
|
497
|
+
const bodyFont = ctx.bodyPdfStyle?.font ?? ctx.defaultFont;
|
|
498
|
+
const fontSize = Number(ctx.bodyPdfStyle?.fontSize) || DEFAULT_TABLE_FONT_PT;
|
|
499
|
+
const headerRow = [
|
|
500
|
+
{
|
|
501
|
+
// Corner stays empty (same as canvas) — long labels crush the first column.
|
|
502
|
+
text: '',
|
|
503
|
+
bold: true,
|
|
504
|
+
fillColor: '#f0f0f0',
|
|
505
|
+
fontSize,
|
|
506
|
+
font: bodyFont,
|
|
507
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
508
|
+
},
|
|
509
|
+
...columns.map((col) => ({
|
|
510
|
+
text: String(col.label ?? col.key ?? ''),
|
|
511
|
+
bold: true,
|
|
512
|
+
fillColor: '#f0f0f0',
|
|
513
|
+
fontSize,
|
|
514
|
+
font: bodyFont,
|
|
515
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
516
|
+
})),
|
|
517
|
+
];
|
|
518
|
+
if (showRowTotals) {
|
|
519
|
+
headerRow.push({
|
|
520
|
+
text: 'Total',
|
|
521
|
+
bold: true,
|
|
522
|
+
fillColor: '#f0f0f0',
|
|
523
|
+
fontSize,
|
|
524
|
+
font: bodyFont,
|
|
525
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
const bodyRows = rows.map((row) => {
|
|
529
|
+
const cells = [
|
|
530
|
+
{
|
|
531
|
+
text: String(row.label ?? ''),
|
|
532
|
+
bold: true,
|
|
533
|
+
fontSize,
|
|
534
|
+
font: bodyFont,
|
|
535
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
536
|
+
},
|
|
537
|
+
...columns.map((col) => ({
|
|
538
|
+
text: formatPivotPdfCell(row.cells?.[col.key], emptyCell, schema),
|
|
539
|
+
fontSize,
|
|
540
|
+
font: bodyFont,
|
|
541
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
542
|
+
alignment: valueAlign,
|
|
543
|
+
})),
|
|
544
|
+
];
|
|
545
|
+
if (showRowTotals) {
|
|
546
|
+
cells.push({
|
|
547
|
+
text: formatPivotPdfCell(row.rowTotal, emptyCell, schema),
|
|
548
|
+
bold: true,
|
|
549
|
+
fontSize,
|
|
550
|
+
font: bodyFont,
|
|
551
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
552
|
+
alignment: valueAlign,
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
return cells;
|
|
556
|
+
});
|
|
557
|
+
if (showColumnTotals || showGrandTotal) {
|
|
558
|
+
const totalsRow = [
|
|
559
|
+
{
|
|
560
|
+
text: 'Total',
|
|
561
|
+
bold: true,
|
|
562
|
+
fontSize,
|
|
563
|
+
font: bodyFont,
|
|
564
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
565
|
+
},
|
|
566
|
+
...columns.map((col) => ({
|
|
567
|
+
text: showColumnTotals
|
|
568
|
+
? formatPivotPdfCell(pivot.columnTotals?.[col.key], emptyCell, schema)
|
|
569
|
+
: emptyCell,
|
|
570
|
+
bold: true,
|
|
571
|
+
fontSize,
|
|
572
|
+
font: bodyFont,
|
|
573
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
574
|
+
alignment: valueAlign,
|
|
575
|
+
})),
|
|
576
|
+
];
|
|
577
|
+
if (showRowTotals) {
|
|
578
|
+
totalsRow.push({
|
|
579
|
+
text: showGrandTotal
|
|
580
|
+
? formatPivotPdfCell(pivot.grandTotal, emptyCell, schema)
|
|
581
|
+
: emptyCell,
|
|
582
|
+
bold: true,
|
|
583
|
+
fontSize,
|
|
584
|
+
font: bodyFont,
|
|
585
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
586
|
+
alignment: valueAlign,
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
bodyRows.push(totalsRow);
|
|
590
|
+
}
|
|
591
|
+
if (!bodyRows.length)
|
|
592
|
+
return null;
|
|
593
|
+
const colCount = headerRow.length;
|
|
594
|
+
const rowLabelWidth = parseTableColumnWidth(schema.rowLabelWidth);
|
|
595
|
+
const firstWidth = rowLabelWidth !== '*'
|
|
596
|
+
? rowLabelWidth
|
|
597
|
+
: (() => {
|
|
598
|
+
const em = String(schema.rowLabelWidth ?? '8em').match(/^(\d+(?:\.\d+)?)em$/i);
|
|
599
|
+
if (em)
|
|
600
|
+
return Math.round(Number.parseFloat(em[1]) * fontSize);
|
|
601
|
+
return 'auto';
|
|
602
|
+
})();
|
|
603
|
+
const widths = [firstWidth, ...Array.from({ length: colCount - 1 }, () => '*')];
|
|
604
|
+
return {
|
|
605
|
+
table: {
|
|
606
|
+
headerRows: 1,
|
|
607
|
+
keepWithHeaderRows: 1,
|
|
608
|
+
widths,
|
|
609
|
+
body: [headerRow, ...bodyRows],
|
|
610
|
+
dontBreakRows: true,
|
|
611
|
+
},
|
|
612
|
+
layout: resolveVisionTablePdfLayout({ hideBorders: !!schema.hideBorders }),
|
|
613
|
+
margin: [0, 0, 0, 8],
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
function renderTableSegmentPdf(seg, ctx) {
|
|
617
|
+
const tableId = String(seg.id ?? '');
|
|
618
|
+
const schema = ctx.fieldSchemas?.[tableId];
|
|
619
|
+
if (schema?.type === 'pivotTable') {
|
|
620
|
+
if (ctx.hideEmptyValues && !tableHasPreviewContent(tableId, ctx, seg.rows)) {
|
|
621
|
+
return null;
|
|
622
|
+
}
|
|
623
|
+
return buildPivotPdfTable(tableId, ctx);
|
|
624
|
+
}
|
|
625
|
+
if (ctx.hideEmptyValues && !tableHasPreviewContent(tableId, ctx, seg.rows)) {
|
|
626
|
+
return null;
|
|
627
|
+
}
|
|
628
|
+
return buildPdfTable(tableId, ctx, seg.rows);
|
|
629
|
+
}
|
|
630
|
+
export function buildPdfTableBodyRows(tableId, ctx, segmentRows) {
|
|
631
|
+
const tableSchema = getTableSchema(tableId, { fieldSchemas: ctx.fieldSchemas });
|
|
632
|
+
const tableRows = resolveTableInstanceRows(segmentRows, tableSchema);
|
|
633
|
+
const columns = tableSchema.columns ?? [];
|
|
634
|
+
const columnHeaderRow = [
|
|
635
|
+
...(shouldShowRowLabels(tableSchema, tableRows)
|
|
636
|
+
? [{ text: '', fillColor: '#f0f0f0', lineHeight: TABLE_PDF_LINE_HEIGHT }]
|
|
637
|
+
: []),
|
|
638
|
+
...columns.map((col) => ({
|
|
639
|
+
text: col.label ?? col.key,
|
|
640
|
+
...resolveTableColumnPdfStyle(tableId, col.key, ctx.fieldSchemas, ctx.fieldValueStyle, ctx.resolveFontName, ctx.pageSetup),
|
|
641
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
642
|
+
fillColor: '#f0f0f0',
|
|
643
|
+
})),
|
|
644
|
+
];
|
|
645
|
+
const bodyRows = [];
|
|
646
|
+
const hideEmpty = ctx.hideEmptyValues === true;
|
|
647
|
+
const includeRowLabels = shouldShowRowLabels(tableSchema, tableRows);
|
|
648
|
+
for (const row of tableRows) {
|
|
649
|
+
const cells = [];
|
|
650
|
+
let hasValue = false;
|
|
651
|
+
if (includeRowLabels) {
|
|
652
|
+
cells.push({
|
|
653
|
+
text: String(row.label ?? ''),
|
|
654
|
+
bold: true,
|
|
655
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
for (const col of columns) {
|
|
659
|
+
const cellId = cellFieldId(tableId, row.key, col.key);
|
|
660
|
+
const cellSchema = ctx.fieldSchemas[cellId];
|
|
661
|
+
const value = resolveFieldValue(cellId, ctx.fieldValues, ctx.fieldSchemas, ctx.blocks);
|
|
662
|
+
const label = cellSchema?.label ?? col.label ?? col.key;
|
|
663
|
+
if (isTableCellDisplayPlaceholder(value, label) || isPdfFieldEmpty(value, cellSchema)) {
|
|
664
|
+
if (cellSchema?.required) {
|
|
665
|
+
hasValue = true;
|
|
666
|
+
const requiredStyle = resolvePdfFieldStyleForExport(cellId, ctx, DOCUMENT_TABLE_TEXT_STYLE);
|
|
667
|
+
cells.push(withTableCellFillColor({
|
|
668
|
+
text: String(label ?? ''),
|
|
669
|
+
italics: true,
|
|
670
|
+
color: '#888888',
|
|
671
|
+
lineHeight: TABLE_PDF_LINE_HEIGHT,
|
|
672
|
+
fontSize: requiredStyle.fontSize ?? DEFAULT_TABLE_FONT_PT,
|
|
673
|
+
}, requiredStyle));
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
cells.push(buildEmptyPdfTableCell(cellId, ctx));
|
|
677
|
+
}
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
hasValue = true;
|
|
681
|
+
const style = resolvePdfFieldStyleForExport(cellId, ctx, DOCUMENT_TABLE_TEXT_STYLE);
|
|
682
|
+
const baseFontSize = Number(style.fontSize) || DEFAULT_TABLE_FONT_PT;
|
|
683
|
+
const cellFont = String(style.font ?? ctx.defaultFont ?? '');
|
|
684
|
+
const blocks = resolvePdfRenderMode(cellSchema) === 'html'
|
|
685
|
+
? htmlToPdfBlocks(String(value ?? ''), { baseFontSize, baseFont: cellFont })
|
|
686
|
+
: [{ parts: fieldValueToPdfParts(cellId, value, cellSchema, col.label ?? col.key, ctx) }];
|
|
687
|
+
const cell = pdfBlocksToTableCell(blocks, style);
|
|
688
|
+
cells.push(cell || '');
|
|
689
|
+
}
|
|
690
|
+
if (hideEmpty && !hasValue)
|
|
691
|
+
continue;
|
|
692
|
+
bodyRows.push(cells);
|
|
693
|
+
}
|
|
694
|
+
if (!bodyRows.length)
|
|
695
|
+
return null;
|
|
696
|
+
return {
|
|
697
|
+
columnHeaderRow,
|
|
698
|
+
bodyRows,
|
|
699
|
+
columns,
|
|
700
|
+
hideHeader: !!tableSchema.hideHeader,
|
|
701
|
+
hideBorders: !!tableSchema.hideBorders,
|
|
702
|
+
includeRowLabels,
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
export function buildPdfTableFromBodyRows(columnHeaderRow, bodyRows, columns, presentation = {}) {
|
|
706
|
+
if (!bodyRows.length)
|
|
707
|
+
return null;
|
|
708
|
+
const hideHeader = !!presentation.hideHeader;
|
|
709
|
+
const body = hideHeader ? [...bodyRows] : [columnHeaderRow, ...bodyRows];
|
|
710
|
+
const dataWidths = tableColumnWidthsFromSchema(columns);
|
|
711
|
+
const widths = presentation.includeRowLabels
|
|
712
|
+
? ['auto', ...dataWidths]
|
|
713
|
+
: dataWidths;
|
|
714
|
+
return {
|
|
715
|
+
table: {
|
|
716
|
+
headerRows: hideHeader ? 0 : 1,
|
|
717
|
+
keepWithHeaderRows: hideHeader ? 0 : 1,
|
|
718
|
+
widths,
|
|
719
|
+
body,
|
|
720
|
+
dontBreakRows: true,
|
|
721
|
+
},
|
|
722
|
+
layout: resolveVisionTablePdfLayout(presentation),
|
|
723
|
+
margin: [0, 0, 0, 8],
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
function buildFirstRepeatableChunk(sectionLabel, table, leadingContent = [], omitSectionTitle = false, sectionHeaderStyle = {}) {
|
|
727
|
+
const stack = [];
|
|
728
|
+
if (!omitSectionTitle) {
|
|
729
|
+
const titleNode = buildRepeatableSectionTitleNode(sectionLabel, sectionHeaderStyle);
|
|
730
|
+
if (titleNode)
|
|
731
|
+
stack.push(titleNode);
|
|
732
|
+
}
|
|
733
|
+
stack.push(...leadingContent, table);
|
|
734
|
+
return {
|
|
735
|
+
unbreakable: true,
|
|
736
|
+
stack,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
function buildContinuationRepeatableChunk(sectionLabel, columnHeaderRow, chunkRows, columns, repeatSectionContent = [], pageHeaderMode = false, sectionHeaderStyle = {}, presentation = {}) {
|
|
740
|
+
const table = buildPdfTableFromBodyRows(columnHeaderRow, chunkRows, columns, presentation);
|
|
741
|
+
if (!table)
|
|
742
|
+
return null;
|
|
743
|
+
if (pageHeaderMode) {
|
|
744
|
+
return {
|
|
745
|
+
pageBreak: 'before',
|
|
746
|
+
stack: [table],
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
const stack = [];
|
|
750
|
+
const titleNode = buildRepeatableSectionTitleNode(sectionLabel, sectionHeaderStyle);
|
|
751
|
+
if (titleNode)
|
|
752
|
+
stack.push(titleNode);
|
|
753
|
+
stack.push(...repeatSectionContent, table);
|
|
754
|
+
return {
|
|
755
|
+
pageBreak: 'before',
|
|
756
|
+
stack,
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
function shrinkContinuationRowsUntilFitsPage(chunkRows, columnHeaderRow, columns, sectionLabel, repeatSectionContent, pageSetup, sectionHeaderStyle = {}, presentation = {}) {
|
|
760
|
+
const pageHeaderMode = usesRepeatablePageHeader(pageSetup);
|
|
761
|
+
let rows = [...chunkRows];
|
|
762
|
+
while (rows.length > 0) {
|
|
763
|
+
const chunk = buildContinuationRepeatableChunk(sectionLabel, columnHeaderRow, rows, columns, repeatSectionContent, pageHeaderMode, sectionHeaderStyle, presentation);
|
|
764
|
+
if (chunk && repeatableChunkFitsPage(chunk, pageSetup)) {
|
|
765
|
+
return rows;
|
|
766
|
+
}
|
|
767
|
+
rows.pop();
|
|
768
|
+
}
|
|
769
|
+
return rows;
|
|
770
|
+
}
|
|
771
|
+
function splitRepeatableTableRowChunks(bodyRows, firstChunkMaxBodyHeight, continuationMaxBodyHeight) {
|
|
772
|
+
return chunkTableBodyRowsVariable(bodyRows, firstChunkMaxBodyHeight, continuationMaxBodyHeight);
|
|
773
|
+
}
|
|
774
|
+
function shrinkFirstChunkUntilFitsPage(rowChunks, _bodyRows, columnHeaderRow, columns, sectionLabel, firstChunkLeadingContent, pageSetup, sectionHeaderStyle = {}, presentation = {}) {
|
|
775
|
+
if (!rowChunks.length || !rowChunks[0]?.length)
|
|
776
|
+
return rowChunks;
|
|
777
|
+
const omitSectionTitle = usesRepeatablePageHeader(pageSetup);
|
|
778
|
+
while (rowChunks[0].length > 0) {
|
|
779
|
+
const table = buildPdfTableFromBodyRows(columnHeaderRow, rowChunks[0], columns, presentation);
|
|
780
|
+
if (!table)
|
|
781
|
+
break;
|
|
782
|
+
const chunk = buildFirstRepeatableChunk(sectionLabel, table, firstChunkLeadingContent, omitSectionTitle, sectionHeaderStyle);
|
|
783
|
+
if (repeatableChunkFitsPage(chunk, pageSetup)) {
|
|
784
|
+
return rowChunks;
|
|
785
|
+
}
|
|
786
|
+
const movedRow = rowChunks[0].pop();
|
|
787
|
+
if (!movedRow)
|
|
788
|
+
break;
|
|
789
|
+
if (rowChunks.length > 1) {
|
|
790
|
+
rowChunks[1].unshift(movedRow);
|
|
791
|
+
}
|
|
792
|
+
else {
|
|
793
|
+
rowChunks.push([movedRow]);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
return rowChunks.filter((chunkRows) => chunkRows.length > 0);
|
|
797
|
+
}
|
|
798
|
+
export function buildPdfTable(tableId, ctx, segmentRows) {
|
|
799
|
+
const parts = buildPdfTableBodyRows(tableId, ctx, segmentRows);
|
|
800
|
+
if (!parts)
|
|
801
|
+
return null;
|
|
802
|
+
const { columnHeaderRow, bodyRows, columns, hideHeader, hideBorders, includeRowLabels } = parts;
|
|
803
|
+
return buildPdfTableFromBodyRows(columnHeaderRow, bodyRows, columns, {
|
|
804
|
+
hideHeader,
|
|
805
|
+
hideBorders,
|
|
806
|
+
includeRowLabels,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
export function buildRepeatableSectionTitleNode(sectionLabel, sectionHeaderStyle = {}) {
|
|
810
|
+
return buildPdfSectionTitleNode(sectionLabel, sectionHeaderStyle);
|
|
811
|
+
}
|
|
812
|
+
export function buildRepeatableTableContent(tableId, ctx, segmentRows, sectionLabel, pageSetup = {}, priorContent = [], repeatSectionContent = [], firstChunkOnlyContent = []) {
|
|
813
|
+
const parts = buildPdfTableBodyRows(tableId, ctx, segmentRows);
|
|
814
|
+
if (!parts)
|
|
815
|
+
return [];
|
|
816
|
+
const pageHeaderMode = usesRepeatablePageHeader(pageSetup);
|
|
817
|
+
const effectiveRepeatContent = pageHeaderMode ? [] : repeatSectionContent;
|
|
818
|
+
const { columnHeaderRow, bodyRows, columns, hideHeader, hideBorders, includeRowLabels } = parts;
|
|
819
|
+
const presentation = { hideHeader, hideBorders, includeRowLabels };
|
|
820
|
+
const priorContentHeightPt = estimatePdfContentHeightPt(priorContent);
|
|
821
|
+
const repeatSectionContentHeightPt = pageHeaderMode
|
|
822
|
+
? 0
|
|
823
|
+
: estimatePdfContentHeightPt(repeatSectionContent);
|
|
824
|
+
const firstChunkLeadingHeightPt = repeatSectionContentHeightPt
|
|
825
|
+
+ estimatePdfContentHeightPt(firstChunkOnlyContent);
|
|
826
|
+
const firstChunkMaxBodyHeight = estimateFirstChunkMaxBodyHeight(pageSetup, priorContentHeightPt, firstChunkLeadingHeightPt, pageHeaderMode);
|
|
827
|
+
const continuationMaxBodyHeight = estimateContinuationChunkMaxBodyHeight(pageSetup, repeatSectionContentHeightPt, pageHeaderMode);
|
|
828
|
+
let rowChunks = splitRepeatableTableRowChunks(bodyRows, firstChunkMaxBodyHeight, continuationMaxBodyHeight);
|
|
829
|
+
const firstChunkLeadingContent = pageHeaderMode
|
|
830
|
+
? firstChunkOnlyContent
|
|
831
|
+
: [...repeatSectionContent, ...firstChunkOnlyContent];
|
|
832
|
+
rowChunks = shrinkFirstChunkUntilFitsPage(rowChunks, bodyRows, columnHeaderRow, columns, sectionLabel, firstChunkLeadingContent, pageSetup, ctx.sectionHeaderPdfStyle, presentation);
|
|
833
|
+
const omitSectionTitle = pageHeaderMode;
|
|
834
|
+
const sectionHeaderStyle = ctx.sectionHeaderPdfStyle;
|
|
835
|
+
return rowChunks.map((chunkRows, chunkIndex) => {
|
|
836
|
+
if (chunkIndex === 0) {
|
|
837
|
+
const table = buildPdfTableFromBodyRows(columnHeaderRow, chunkRows, columns, presentation);
|
|
838
|
+
if (!table)
|
|
839
|
+
return null;
|
|
840
|
+
return buildFirstRepeatableChunk(sectionLabel, table, [], omitSectionTitle, sectionHeaderStyle);
|
|
841
|
+
}
|
|
842
|
+
const fittedRows = shrinkContinuationRowsUntilFitsPage(chunkRows, columnHeaderRow, columns, sectionLabel, effectiveRepeatContent, pageSetup, sectionHeaderStyle, presentation);
|
|
843
|
+
if (!fittedRows.length)
|
|
844
|
+
return null;
|
|
845
|
+
return buildContinuationRepeatableChunk(sectionLabel, columnHeaderRow, fittedRows, columns, effectiveRepeatContent, pageHeaderMode, sectionHeaderStyle, presentation);
|
|
846
|
+
}).filter(Boolean);
|
|
847
|
+
}
|
|
848
|
+
function getCompanionTableSegmentRows(adjacent) {
|
|
849
|
+
if (adjacent.blockKind !== 'section')
|
|
850
|
+
return null;
|
|
851
|
+
for (const seg of adjacent.block.data?.segments ?? []) {
|
|
852
|
+
if (seg.type === 'table' && seg.id)
|
|
853
|
+
return seg.rows;
|
|
854
|
+
}
|
|
855
|
+
return null;
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Segments that appear before the companion table in the next section.
|
|
859
|
+
* Without these, preview-first HTML keeps "Item group" above the table while
|
|
860
|
+
* PDF would emit the stolen table first and leave the prose for later.
|
|
861
|
+
*/
|
|
862
|
+
function getCompanionLeadingSegments(block, tableId) {
|
|
863
|
+
if (block?.type !== 'documentSection')
|
|
864
|
+
return [];
|
|
865
|
+
const segments = block.data?.segments ?? [];
|
|
866
|
+
const tableIndex = segments.findIndex((seg) => seg.type === 'table' && String(seg.id ?? '') === String(tableId));
|
|
867
|
+
if (tableIndex <= 0)
|
|
868
|
+
return [];
|
|
869
|
+
return segments.slice(0, tableIndex);
|
|
870
|
+
}
|
|
871
|
+
function companionTableHasContent(adjacent, ctx) {
|
|
872
|
+
if (!ctx.hideEmptyValues)
|
|
873
|
+
return !!adjacent.tableId;
|
|
874
|
+
const segmentRows = getCompanionTableSegmentRows(adjacent);
|
|
875
|
+
return tableHasPreviewContent(adjacent.tableId, ctx, segmentRows);
|
|
876
|
+
}
|
|
877
|
+
function isTableOnlyCompanionSection(block) {
|
|
878
|
+
if (block.type !== 'documentSection')
|
|
879
|
+
return true;
|
|
880
|
+
const segments = block.data?.segments ?? [];
|
|
881
|
+
return !segments.some((seg) => seg.type !== 'table');
|
|
882
|
+
}
|
|
883
|
+
/** True when the companion section has no segments after the consumed table. */
|
|
884
|
+
function companionHasOnlyLeadingAndTable(block, tableId) {
|
|
885
|
+
if (block?.type !== 'documentSection')
|
|
886
|
+
return true;
|
|
887
|
+
const segments = block.data?.segments ?? [];
|
|
888
|
+
const tableIndex = segments.findIndex((seg) => seg.type === 'table' && String(seg.id ?? '') === String(tableId));
|
|
889
|
+
if (tableIndex < 0)
|
|
890
|
+
return isTableOnlyCompanionSection(block);
|
|
891
|
+
return tableIndex >= segments.length - 1;
|
|
892
|
+
}
|
|
893
|
+
function buildFirstRepeatableChunkStack(chunk, leadingContent = [], pageSetup = {}, sectionHeaderStyle = {}) {
|
|
894
|
+
const table = findTableInChunkStack(chunk);
|
|
895
|
+
const sectionTitle = findRepeatableSectionTitleInChunk(chunk);
|
|
896
|
+
if (!table)
|
|
897
|
+
return chunk;
|
|
898
|
+
const sectionLabel = sectionTitle ? String(sectionTitle.text ?? '') : '';
|
|
899
|
+
const omitSectionTitle = usesRepeatablePageHeader(pageSetup) || !sectionTitle;
|
|
900
|
+
return buildFirstRepeatableChunk(sectionLabel, table, leadingContent, omitSectionTitle, sectionHeaderStyle);
|
|
901
|
+
}
|
|
902
|
+
function findRepeatableSectionTitleInChunk(chunk) {
|
|
903
|
+
if (!Array.isArray(chunk?.stack))
|
|
904
|
+
return null;
|
|
905
|
+
if (chunk.stack[0]?.style === 'sectionHeader')
|
|
906
|
+
return chunk.stack[0];
|
|
907
|
+
if (Array.isArray(chunk.stack[0]?.stack)) {
|
|
908
|
+
return chunk.stack[0].stack.find((child) => child?.style === 'sectionHeader') ?? null;
|
|
909
|
+
}
|
|
910
|
+
return null;
|
|
911
|
+
}
|
|
912
|
+
function findTableInChunkStack(chunk) {
|
|
913
|
+
if (!Array.isArray(chunk?.stack))
|
|
914
|
+
return null;
|
|
915
|
+
for (let i = chunk.stack.length - 1; i >= 0; i -= 1) {
|
|
916
|
+
if (chunk.stack[i]?.table)
|
|
917
|
+
return chunk.stack[i];
|
|
918
|
+
}
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
function appendRepeatableTableChunks(sectionStack, chunks, repeatSectionContent = [], firstChunkOnlyContent = [], pageSetup = {}, sectionHeaderStyle = {}) {
|
|
922
|
+
if (usesRepeatablePageHeader(pageSetup)) {
|
|
923
|
+
sectionStack.push(...chunks);
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
926
|
+
const pageHeaderMode = usesRepeatablePageHeader(pageSetup);
|
|
927
|
+
const firstChunkLeadingContent = pageHeaderMode
|
|
928
|
+
? firstChunkOnlyContent
|
|
929
|
+
: [...repeatSectionContent, ...firstChunkOnlyContent];
|
|
930
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
931
|
+
if (i === 0) {
|
|
932
|
+
sectionStack.push(buildFirstRepeatableChunkStack(chunks[i], firstChunkLeadingContent, pageSetup, sectionHeaderStyle));
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
sectionStack.push(chunks[i]);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
function renderRepeatableTableChunks(tableId, ctx, segmentRows, sectionLabel, pageSetup, priorContent, repeatSectionContent, firstChunkOnlyContent = []) {
|
|
939
|
+
if (usesRepeatablePageHeader(pageSetup)) {
|
|
940
|
+
const table = buildPdfTable(tableId, ctx, segmentRows);
|
|
941
|
+
if (!table)
|
|
942
|
+
return [];
|
|
943
|
+
return firstChunkOnlyContent.length ? [...firstChunkOnlyContent, table] : [table];
|
|
944
|
+
}
|
|
945
|
+
return buildRepeatableTableContent(tableId, ctx, segmentRows, sectionLabel, pageSetup, priorContent, repeatSectionContent, firstChunkOnlyContent);
|
|
946
|
+
}
|
|
947
|
+
function renderRepeaterFieldPdf(fieldId, ctx) {
|
|
948
|
+
const schema = ctx.fieldSchemas[fieldId];
|
|
949
|
+
if (!schema || (schema.type !== 'child' && schema.type !== 'repeater'))
|
|
950
|
+
return null;
|
|
951
|
+
const value = normalizeRepeaterValue(ctx.fieldValues[fieldId], schema);
|
|
952
|
+
if (ctx.hideEmptyValues && !repeaterHasContent(value, schema))
|
|
953
|
+
return null;
|
|
954
|
+
const childDoc = buildRepeaterFillDocument(schema, value);
|
|
955
|
+
const childContent = renderSinglePagePdfContent(childDoc, {
|
|
956
|
+
fieldValueStyle: ctx.fieldValueStyle,
|
|
957
|
+
resolveFontName: ctx.resolveFontName,
|
|
958
|
+
defaultFont: ctx.defaultFont,
|
|
959
|
+
hideEmptyValues: ctx.hideEmptyValues,
|
|
960
|
+
});
|
|
961
|
+
if (!childContent.length)
|
|
962
|
+
return null;
|
|
963
|
+
const label = schema.label ?? fieldId;
|
|
964
|
+
return {
|
|
965
|
+
stack: [
|
|
966
|
+
{ text: `${label}:`, margin: [0, 0, 0, 2] },
|
|
967
|
+
{ stack: childContent, margin: [8, 0, 0, 6] },
|
|
968
|
+
],
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
export function renderSinglePagePdfContent(doc, options) {
|
|
972
|
+
let pageSetup = { ...(doc.pageSetup ?? {}), ...(options.pageSetup ?? {}) };
|
|
973
|
+
if (!usesRepeatablePageHeader(pageSetup) && !options.skipRepeatablePageHeader) {
|
|
974
|
+
const repeatableHeader = buildRepeatableSectionPageHeader(doc, options);
|
|
975
|
+
if (repeatableHeader) {
|
|
976
|
+
pageSetup = {
|
|
977
|
+
...pageSetup,
|
|
978
|
+
header: {
|
|
979
|
+
height: repeatableHeader.heightMm,
|
|
980
|
+
fromRepeatableSection: true,
|
|
981
|
+
},
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
const ctx = createPdfRenderContext(doc, options);
|
|
986
|
+
const content = [];
|
|
987
|
+
const blocks = doc.blocks ?? [];
|
|
988
|
+
/** @type {Set<number>} */
|
|
989
|
+
const consumedBlockIndices = new Set();
|
|
990
|
+
/** @type {Map<number, { skipEntire: boolean, excludeTableIds?: Set<string>, consumedThroughTableId?: string }>} */
|
|
991
|
+
const consumedCompanionMeta = new Map();
|
|
992
|
+
for (let blockIndex = 0; blockIndex < blocks.length; blockIndex++) {
|
|
993
|
+
const block = blocks[blockIndex];
|
|
994
|
+
const data = block.data ?? {};
|
|
995
|
+
if (block.type === 'documentSection') {
|
|
996
|
+
const consumedMeta = consumedCompanionMeta.get(blockIndex);
|
|
997
|
+
if (consumedMeta?.skipEntire)
|
|
998
|
+
continue;
|
|
999
|
+
if (!evaluateSectionVisibility(data.visibility, ctx.fieldValues, ctx.fieldSchemas))
|
|
1000
|
+
continue;
|
|
1001
|
+
// Title already emitted with the stolen companion table — don't repeat it
|
|
1002
|
+
// when only trailing body content remains.
|
|
1003
|
+
const sectionLabel = consumedMeta?.consumedThroughTableId
|
|
1004
|
+
? ''
|
|
1005
|
+
: resolveVisibleSectionLabel(data);
|
|
1006
|
+
const repeatable = !!data.repeatable;
|
|
1007
|
+
const keepOnOnePage = !!data.keepOnOnePage;
|
|
1008
|
+
const filtered = ctx.hideEmptyValues
|
|
1009
|
+
? filterSegmentsForPreview(data.segments ?? [], ctx.fieldValues, ctx.fieldSchemas)
|
|
1010
|
+
: (data.segments ?? []);
|
|
1011
|
+
const spacingCollapsed = collapseBlankSpacingBeforeTables(filtered);
|
|
1012
|
+
const excludeTableIds = consumedMeta?.excludeTableIds ?? new Set();
|
|
1013
|
+
const consumedThroughTableId = consumedMeta?.consumedThroughTableId
|
|
1014
|
+
? String(consumedMeta.consumedThroughTableId)
|
|
1015
|
+
: '';
|
|
1016
|
+
// When a prior "Show on each page" section already emitted this section's
|
|
1017
|
+
// leading prose + table, only keep trailing segments for a later pass.
|
|
1018
|
+
const segmentsAfterConsumedTable = consumedThroughTableId
|
|
1019
|
+
? (() => {
|
|
1020
|
+
const tableIndex = spacingCollapsed.findIndex((seg) => seg.type === 'table' && String(seg.id ?? '') === consumedThroughTableId);
|
|
1021
|
+
return tableIndex >= 0 ? spacingCollapsed.slice(tableIndex + 1) : spacingCollapsed;
|
|
1022
|
+
})()
|
|
1023
|
+
: spacingCollapsed;
|
|
1024
|
+
const renderableSegments = segmentsAfterConsumedTable.filter((seg) => !(seg.type === 'table' && seg.id && excludeTableIds.has(String(seg.id))));
|
|
1025
|
+
const hasTableWithContent = renderableSegments.some((seg) => seg.type === 'table' &&
|
|
1026
|
+
seg.id &&
|
|
1027
|
+
(ctx.hideEmptyValues
|
|
1028
|
+
? tableHasPreviewContent(String(seg.id), ctx, seg.rows)
|
|
1029
|
+
: true));
|
|
1030
|
+
const repeatTableHeader = repeatable && hasTableWithContent;
|
|
1031
|
+
if (!sectionLabel && !renderableSegments.length)
|
|
1032
|
+
continue;
|
|
1033
|
+
const sectionStack = [];
|
|
1034
|
+
let usedRepeatableTableChunks = false;
|
|
1035
|
+
if (repeatTableHeader) {
|
|
1036
|
+
usedRepeatableTableChunks = true;
|
|
1037
|
+
const nonTableSegments = renderableSegments.filter((seg) => seg.type !== 'table');
|
|
1038
|
+
const tableSegments = renderableSegments.filter((seg) => seg.type === 'table');
|
|
1039
|
+
const nonTableContent = nonTableSegments.length
|
|
1040
|
+
? renderSegmentsToPdfContent(nonTableSegments, ctx)
|
|
1041
|
+
: [];
|
|
1042
|
+
for (const tableSeg of tableSegments) {
|
|
1043
|
+
const tableId = String(tableSeg.id ?? '');
|
|
1044
|
+
if (ctx.hideEmptyValues && !tableHasPreviewContent(tableId, ctx, tableSeg.rows)) {
|
|
1045
|
+
continue;
|
|
1046
|
+
}
|
|
1047
|
+
const chunks = renderRepeatableTableChunks(tableId, ctx, tableSeg.rows, sectionLabel, pageSetup, content, nonTableContent);
|
|
1048
|
+
appendRepeatableTableChunks(sectionStack, chunks, nonTableContent, [], pageSetup, ctx.sectionHeaderPdfStyle);
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
else if (repeatable && !consumedMeta) {
|
|
1052
|
+
const adjacent = findAdjacentTableBlock(blocks, blockIndex, {
|
|
1053
|
+
shouldUse: (candidate) => companionTableHasContent(candidate, ctx),
|
|
1054
|
+
});
|
|
1055
|
+
if (adjacent && !consumedBlockIndices.has(adjacent.index)) {
|
|
1056
|
+
const nonTableSegments = renderableSegments.filter((seg) => seg.type !== 'table');
|
|
1057
|
+
const nonTableContent = nonTableSegments.length
|
|
1058
|
+
? renderSegmentsToPdfContent(nonTableSegments, ctx)
|
|
1059
|
+
: [];
|
|
1060
|
+
let companionTitleNode = null;
|
|
1061
|
+
let companionLeadingContent = [];
|
|
1062
|
+
if (adjacent.blockKind === 'section') {
|
|
1063
|
+
const companionLabel = resolveVisibleSectionLabel(adjacent.block.data);
|
|
1064
|
+
if (companionLabel && companionLabel !== sectionLabel) {
|
|
1065
|
+
companionTitleNode = buildRepeatableSectionTitleNode(companionLabel, ctx.sectionHeaderPdfStyle);
|
|
1066
|
+
}
|
|
1067
|
+
const leadingSegments = getCompanionLeadingSegments(adjacent.block, adjacent.tableId);
|
|
1068
|
+
const leadingFiltered = ctx.hideEmptyValues
|
|
1069
|
+
? filterSegmentsForPreview(leadingSegments, ctx.fieldValues, ctx.fieldSchemas)
|
|
1070
|
+
: leadingSegments;
|
|
1071
|
+
const leadingCollapsed = collapseBlankSpacingBeforeTables(leadingFiltered);
|
|
1072
|
+
if (leadingCollapsed.length) {
|
|
1073
|
+
companionLeadingContent = renderSegmentsToPdfContent(leadingCollapsed, ctx);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
// When companion leading is attached to the page-1 header, keep it out of
|
|
1077
|
+
// the body. Multipage item instances keep leading in the body instead.
|
|
1078
|
+
const pageHeaderMode = usesRepeatablePageHeader(pageSetup);
|
|
1079
|
+
const companionInHeader = pageHeaderMode && shouldAttachCompanionLeadingToPageHeader(doc);
|
|
1080
|
+
const firstChunkOnlyContent = companionInHeader
|
|
1081
|
+
? []
|
|
1082
|
+
: [
|
|
1083
|
+
...(companionTitleNode ? [companionTitleNode] : []),
|
|
1084
|
+
...companionLeadingContent,
|
|
1085
|
+
];
|
|
1086
|
+
const chunks = renderRepeatableTableChunks(adjacent.tableId, ctx, getCompanionTableSegmentRows(adjacent), sectionLabel, pageSetup, content, nonTableContent, firstChunkOnlyContent);
|
|
1087
|
+
appendRepeatableTableChunks(sectionStack, chunks, nonTableContent, firstChunkOnlyContent, pageSetup, ctx.sectionHeaderPdfStyle);
|
|
1088
|
+
usedRepeatableTableChunks = true;
|
|
1089
|
+
consumedBlockIndices.add(adjacent.index);
|
|
1090
|
+
if (isTableOnlyCompanionSection(adjacent.block)
|
|
1091
|
+
|| companionHasOnlyLeadingAndTable(adjacent.block, adjacent.tableId)) {
|
|
1092
|
+
consumedCompanionMeta.set(adjacent.index, { skipEntire: true });
|
|
1093
|
+
}
|
|
1094
|
+
else {
|
|
1095
|
+
consumedCompanionMeta.set(adjacent.index, {
|
|
1096
|
+
skipEntire: false,
|
|
1097
|
+
excludeTableIds: new Set([adjacent.tableId]),
|
|
1098
|
+
consumedThroughTableId: String(adjacent.tableId),
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
else if (usesRepeatablePageHeader(pageSetup) && !options.skipRepeatablePageHeader) {
|
|
1103
|
+
// Repeatable section content is rendered in the pdfmake page header.
|
|
1104
|
+
}
|
|
1105
|
+
else {
|
|
1106
|
+
const sectionBody = renderSegmentsToPdfContent(renderableSegments, ctx);
|
|
1107
|
+
const hasBody = sectionBody.length > 0;
|
|
1108
|
+
if (!sectionLabel && !hasBody)
|
|
1109
|
+
continue;
|
|
1110
|
+
if (sectionLabel) {
|
|
1111
|
+
const titleNode = buildPdfSectionTitleNode(sectionLabel, ctx.sectionHeaderPdfStyle);
|
|
1112
|
+
if (titleNode)
|
|
1113
|
+
sectionStack.push(titleNode);
|
|
1114
|
+
}
|
|
1115
|
+
if (hasBody)
|
|
1116
|
+
sectionStack.push(...sectionBody);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
else {
|
|
1120
|
+
const sectionBody = renderSegmentsToPdfContent(renderableSegments, ctx);
|
|
1121
|
+
const hasBody = sectionBody.length > 0;
|
|
1122
|
+
if (!sectionLabel && !hasBody)
|
|
1123
|
+
continue;
|
|
1124
|
+
if (sectionLabel) {
|
|
1125
|
+
const titleNode = buildPdfSectionTitleNode(sectionLabel, ctx.sectionHeaderPdfStyle);
|
|
1126
|
+
if (titleNode)
|
|
1127
|
+
sectionStack.push(titleNode);
|
|
1128
|
+
}
|
|
1129
|
+
if (hasBody)
|
|
1130
|
+
sectionStack.push(...sectionBody);
|
|
1131
|
+
}
|
|
1132
|
+
if (!sectionStack.length)
|
|
1133
|
+
continue;
|
|
1134
|
+
const borderedStack = withPdfSectionBorderRules(sectionStack, data, estimateContentWidthPt(pageSetup));
|
|
1135
|
+
if (usedRepeatableTableChunks) {
|
|
1136
|
+
content.push(...borderedStack);
|
|
1137
|
+
}
|
|
1138
|
+
else {
|
|
1139
|
+
// Keep "Show on each page" / "Keep section on one page" stacks atomic so
|
|
1140
|
+
// headers are not orphaned from following fields/tables across PDF pages.
|
|
1141
|
+
content.push({
|
|
1142
|
+
stack: borderedStack,
|
|
1143
|
+
margin: [0, 0, 0, 6],
|
|
1144
|
+
...(repeatable || keepOnOnePage ? { unbreakable: true } : {}),
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
continue;
|
|
1148
|
+
}
|
|
1149
|
+
if (consumedBlockIndices.has(blockIndex))
|
|
1150
|
+
continue;
|
|
1151
|
+
if (block.type === 'visionTable') {
|
|
1152
|
+
const tableFieldId = data.fieldId ?? 'visionTable';
|
|
1153
|
+
const table = buildPdfTable(tableFieldId, ctx, null);
|
|
1154
|
+
if (table)
|
|
1155
|
+
content.push(table);
|
|
1156
|
+
continue;
|
|
1157
|
+
}
|
|
1158
|
+
if (block.type === 'templateBlock') {
|
|
1159
|
+
const blockContent = renderTemplateBlockPdf(data, ctx);
|
|
1160
|
+
if (blockContent.length)
|
|
1161
|
+
content.push(...blockContent);
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
if (!content.length && options.hideEmptyValues === true) {
|
|
1165
|
+
content.push({ text: 'No filled content to export.', style: 'empty' });
|
|
1166
|
+
}
|
|
1167
|
+
return content;
|
|
1168
|
+
}
|
|
1169
|
+
function renderTemplateBlockPdf(blockData, ctx) {
|
|
1170
|
+
const fieldId = String(blockData.fieldId ?? '');
|
|
1171
|
+
const fieldType = blockData.fieldType;
|
|
1172
|
+
if (fieldType === 'table') {
|
|
1173
|
+
const table = buildPdfTable(fieldId, ctx, null);
|
|
1174
|
+
return table ? [table] : [];
|
|
1175
|
+
}
|
|
1176
|
+
if (fieldType === 'repeater' || fieldType === 'child') {
|
|
1177
|
+
const repeater = renderRepeaterFieldPdf(fieldId, ctx);
|
|
1178
|
+
return repeater ? [repeater] : [];
|
|
1179
|
+
}
|
|
1180
|
+
const schema = ctx.fieldSchemas[fieldId];
|
|
1181
|
+
const value = resolveFieldValue(fieldId, ctx.fieldValues, ctx.fieldSchemas, ctx.blocks)
|
|
1182
|
+
?? blockData.value;
|
|
1183
|
+
const empty = isPdfFieldEmpty(value, schema);
|
|
1184
|
+
if (ctx.hideEmptyValues && empty && !schema?.required)
|
|
1185
|
+
return [];
|
|
1186
|
+
const label = schema?.label ?? blockData.label ?? '';
|
|
1187
|
+
const style = resolvePdfFieldStyleForExport(fieldId, ctx);
|
|
1188
|
+
const baseFontSize = Number(style.fontSize ?? ctx.bodyPdfStyle.fontSize) || DEFAULT_BODY_FONT_PT;
|
|
1189
|
+
const fieldAlignment = style.alignment;
|
|
1190
|
+
const inlineStyle = stripBlockAlignment(style);
|
|
1191
|
+
const fieldFont = String(style.font ?? ctx.defaultFont ?? '');
|
|
1192
|
+
if (empty && schema?.required) {
|
|
1193
|
+
const requiredInline = { ...inlineStyle, italics: true, color: '#888888' };
|
|
1194
|
+
const parts = finalizePdfInlineParts(plainTextToPdfText(String(label ?? '')), {
|
|
1195
|
+
font: fieldFont,
|
|
1196
|
+
inlineStyle: requiredInline,
|
|
1197
|
+
});
|
|
1198
|
+
const node = { text: parts, margin: [0, 0, 0, 4] };
|
|
1199
|
+
if (fieldAlignment)
|
|
1200
|
+
node.alignment = fieldAlignment;
|
|
1201
|
+
return [node];
|
|
1202
|
+
}
|
|
1203
|
+
if (resolvePdfRenderMode(schema) === 'html') {
|
|
1204
|
+
const blocks = htmlToPdfBlocks(String(value ?? ''), { baseFontSize, baseFont: fieldFont });
|
|
1205
|
+
const rendered = [];
|
|
1206
|
+
if (blockData.prefixText) {
|
|
1207
|
+
rendered.push({
|
|
1208
|
+
text: finalizePdfInlineParts(plainTextToPdfText(String(blockData.prefixText)), {
|
|
1209
|
+
font: String(ctx.bodyPdfStyle.font ?? ctx.defaultFont ?? ''),
|
|
1210
|
+
inlineStyle: stripBlockAlignment(ctx.bodyPdfStyle),
|
|
1211
|
+
}),
|
|
1212
|
+
margin: [0, 0, 0, 4],
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
for (const block of blocks) {
|
|
1216
|
+
const parts = finalizePdfInlineParts(block.parts, { font: fieldFont, inlineStyle });
|
|
1217
|
+
if (!pdfTextContent(parts))
|
|
1218
|
+
continue;
|
|
1219
|
+
const node = { text: parts, margin: block.margin ?? [0, 0, 0, 4] };
|
|
1220
|
+
const alignment = block.alignment ?? fieldAlignment;
|
|
1221
|
+
if (alignment)
|
|
1222
|
+
node.alignment = alignment;
|
|
1223
|
+
rendered.push(node);
|
|
1224
|
+
}
|
|
1225
|
+
return rendered;
|
|
1226
|
+
}
|
|
1227
|
+
let parts;
|
|
1228
|
+
if (fieldType === 'text') {
|
|
1229
|
+
parts = fieldValueToPdfParts(fieldId, value, schema, label, ctx);
|
|
1230
|
+
}
|
|
1231
|
+
else if (label) {
|
|
1232
|
+
parts = plainTextToPdfText(`${label}: ${formatFieldDisplay(fieldId, value, label, ctx.previewContext)}`);
|
|
1233
|
+
}
|
|
1234
|
+
else {
|
|
1235
|
+
parts = plainTextToPdfText(String(value));
|
|
1236
|
+
}
|
|
1237
|
+
parts = finalizePdfInlineParts(parts, { font: fieldFont, inlineStyle });
|
|
1238
|
+
if (blockData.prefixText) {
|
|
1239
|
+
parts = [
|
|
1240
|
+
...finalizePdfInlineParts(plainTextToPdfText(String(blockData.prefixText)), {
|
|
1241
|
+
font: String(ctx.bodyPdfStyle.font ?? ctx.defaultFont ?? ''),
|
|
1242
|
+
inlineStyle: stripBlockAlignment(ctx.bodyPdfStyle),
|
|
1243
|
+
}),
|
|
1244
|
+
...parts,
|
|
1245
|
+
];
|
|
1246
|
+
}
|
|
1247
|
+
const block = { text: parts, margin: [0, 0, 0, 4] };
|
|
1248
|
+
if (fieldAlignment)
|
|
1249
|
+
block.alignment = fieldAlignment;
|
|
1250
|
+
return pdfTextContent(parts) ? [block] : [];
|
|
1251
|
+
}
|
|
1252
|
+
//# sourceMappingURL=segment-renderer.js.map
|