@openshift-migration-advisor/shared-components 0.25.0 → 0.27.0-5d0cc20d809e

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.
Files changed (129) hide show
  1. package/README.md +25 -0
  2. package/dist/report/ChartDownloadButton.d.ts +11 -0
  3. package/dist/report/ChartDownloadButton.d.ts.map +1 -0
  4. package/dist/report/ChartDownloadButton.js +24 -0
  5. package/dist/report/ChartExportProvider.d.ts +23 -0
  6. package/dist/report/ChartExportProvider.d.ts.map +1 -0
  7. package/dist/report/ChartExportProvider.js +222 -0
  8. package/dist/report/ChartExportSurface.d.ts +12 -0
  9. package/dist/report/ChartExportSurface.d.ts.map +1 -0
  10. package/dist/report/ChartExportSurface.js +32 -0
  11. package/dist/report/CpuAndMemoryOverview.d.ts +19 -0
  12. package/dist/report/CpuAndMemoryOverview.d.ts.map +1 -0
  13. package/dist/report/CpuAndMemoryOverview.js +112 -0
  14. package/dist/report/ErrorTable.d.ts +18 -0
  15. package/dist/report/ErrorTable.d.ts.map +1 -0
  16. package/dist/report/ErrorTable.js +20 -0
  17. package/dist/report/HostPowerStates.d.ts +0 -1
  18. package/dist/report/HostPowerStates.d.ts.map +1 -1
  19. package/dist/report/HostPowerStates.js +2 -2
  20. package/dist/report/HostsOverview.d.ts +13 -0
  21. package/dist/report/HostsOverview.d.ts.map +1 -0
  22. package/dist/report/HostsOverview.js +84 -0
  23. package/dist/report/InfrastructureSummary.d.ts.map +1 -1
  24. package/dist/report/InfrastructureSummary.js +6 -2
  25. package/dist/report/NetworkOverview.d.ts +31 -0
  26. package/dist/report/NetworkOverview.d.ts.map +1 -0
  27. package/dist/report/NetworkOverview.js +201 -0
  28. package/dist/report/OSDistribution.d.ts +0 -2
  29. package/dist/report/OSDistribution.d.ts.map +1 -1
  30. package/dist/report/OSDistribution.js +8 -5
  31. package/dist/report/OsUpgradeRecommendationPopover.d.ts +0 -1
  32. package/dist/report/OsUpgradeRecommendationPopover.d.ts.map +1 -1
  33. package/dist/report/OsUpgradeRecommendationPopover.js +4 -3
  34. package/dist/report/PowerStateCard.d.ts +0 -1
  35. package/dist/report/PowerStateCard.d.ts.map +1 -1
  36. package/dist/report/PowerStateCard.js +4 -2
  37. package/dist/report/ReportExportMenu.d.ts +19 -0
  38. package/dist/report/ReportExportMenu.d.ts.map +1 -0
  39. package/dist/report/ReportExportMenu.js +31 -0
  40. package/dist/report/ReportTable.d.ts +14 -0
  41. package/dist/report/ReportTable.d.ts.map +1 -0
  42. package/dist/report/ReportTable.js +23 -0
  43. package/dist/report/SupportTierBadge.d.ts +0 -1
  44. package/dist/report/SupportTierBadge.d.ts.map +1 -1
  45. package/dist/report/SupportTierBadge.js +1 -21
  46. package/dist/report/VCenterClusterDetails.d.ts +0 -1
  47. package/dist/report/VCenterClusterDetails.d.ts.map +1 -1
  48. package/dist/report/VCenterClusterDetails.js +9 -4
  49. package/dist/report/VmPowerStates.d.ts +0 -1
  50. package/dist/report/VmPowerStates.d.ts.map +1 -1
  51. package/dist/report/VmPowerStates.js +2 -2
  52. package/dist/report/WarningsTable.d.ts +9 -0
  53. package/dist/report/WarningsTable.d.ts.map +1 -0
  54. package/dist/report/WarningsTable.js +20 -0
  55. package/dist/report/captureChartElement.d.ts +8 -0
  56. package/dist/report/captureChartElement.d.ts.map +1 -0
  57. package/dist/report/captureChartElement.js +304 -0
  58. package/dist/report/chartExport.d.ts +80 -0
  59. package/dist/report/chartExport.d.ts.map +1 -0
  60. package/dist/report/chartExport.js +182 -0
  61. package/dist/report/chartExportContext.d.ts +32 -0
  62. package/dist/report/chartExportContext.d.ts.map +1 -0
  63. package/dist/report/chartExportContext.js +36 -0
  64. package/dist/report/chartExportFilenames.d.ts +4 -0
  65. package/dist/report/chartExportFilenames.d.ts.map +1 -0
  66. package/dist/report/chartExportFilenames.js +16 -0
  67. package/dist/report/constants.d.ts +2 -0
  68. package/dist/report/constants.d.ts.map +1 -1
  69. package/dist/report/constants.js +2 -0
  70. package/dist/report/dashboardStyles.d.ts +0 -4
  71. package/dist/report/dashboardStyles.d.ts.map +1 -1
  72. package/dist/report/dashboardStyles.js +0 -19
  73. package/dist/report/htmlExport.d.ts +9 -0
  74. package/dist/report/htmlExport.d.ts.map +1 -0
  75. package/dist/report/htmlExport.js +65 -0
  76. package/dist/report/index.d.ts +19 -0
  77. package/dist/report/index.d.ts.map +1 -1
  78. package/dist/report/index.js +19 -0
  79. package/dist/report/pdfExport.d.ts +3 -0
  80. package/dist/report/pdfExport.d.ts.map +1 -0
  81. package/dist/report/pdfExport.js +131 -0
  82. package/dist/report/pdfPage.d.ts +15 -0
  83. package/dist/report/pdfPage.d.ts.map +1 -0
  84. package/dist/report/pdfPage.js +44 -0
  85. package/dist/report/reportExportOptions.d.ts +17 -0
  86. package/dist/report/reportExportOptions.d.ts.map +1 -0
  87. package/dist/report/reportExportOptions.js +31 -0
  88. package/dist/report/zipChartPngs.d.ts +3 -0
  89. package/dist/report/zipChartPngs.d.ts.map +1 -0
  90. package/dist/report/zipChartPngs.js +8 -0
  91. package/package.json +7 -2
  92. package/src/report/ChartDownloadButton.tsx +59 -0
  93. package/src/report/ChartExportProvider.tsx +392 -0
  94. package/src/report/ChartExportSurface.tsx +60 -0
  95. package/src/report/CpuAndMemoryOverview.tsx +277 -0
  96. package/src/report/ErrorTable.tsx +85 -0
  97. package/src/report/HostPowerStates.tsx +0 -3
  98. package/src/report/HostsOverview.tsx +185 -0
  99. package/src/report/InfrastructureSummary.tsx +48 -31
  100. package/src/report/NetworkOverview.tsx +405 -0
  101. package/src/report/OSDistribution.tsx +46 -42
  102. package/src/report/OsUpgradeRecommendationPopover.tsx +3 -4
  103. package/src/report/PowerStateCard.tsx +49 -36
  104. package/src/report/ReportExportMenu.tsx +119 -0
  105. package/src/report/ReportTable.tsx +81 -0
  106. package/src/report/SupportTierBadge.tsx +12 -40
  107. package/src/report/VCenterClusterDetails.tsx +38 -16
  108. package/src/report/VmPowerStates.tsx +0 -3
  109. package/src/report/WarningsTable.tsx +75 -0
  110. package/src/report/__tests__/ChartExportProvider.test.tsx +582 -0
  111. package/src/report/__tests__/CpuAndMemoryOverview.test.tsx +200 -0
  112. package/src/report/__tests__/HostsOverview.test.tsx +145 -0
  113. package/src/report/__tests__/InfrastructureSummary.test.tsx +0 -22
  114. package/src/report/__tests__/NetworkOverview.test.tsx +269 -0
  115. package/src/report/__tests__/OSDistribution.test.tsx +10 -11
  116. package/src/report/__tests__/captureChartElement.test.ts +205 -0
  117. package/src/report/__tests__/chartExportPackagers.test.ts +299 -0
  118. package/src/report/captureChartElement.ts +398 -0
  119. package/src/report/chartExport.ts +277 -0
  120. package/src/report/chartExportContext.tsx +92 -0
  121. package/src/report/chartExportFilenames.ts +21 -0
  122. package/src/report/constants.ts +2 -0
  123. package/src/report/dashboardStyles.ts +0 -23
  124. package/src/report/htmlExport.ts +83 -0
  125. package/src/report/index.ts +102 -0
  126. package/src/report/pdfExport.ts +233 -0
  127. package/src/report/pdfPage.ts +83 -0
  128. package/src/report/reportExportOptions.ts +53 -0
  129. package/src/report/zipChartPngs.ts +10 -0
@@ -0,0 +1,92 @@
1
+ import {
2
+ createContext,
3
+ type RefCallback,
4
+ useContext,
5
+ useEffect,
6
+ useState,
7
+ } from "react";
8
+ import {
9
+ type ChartExportMeta,
10
+ type ChartExportView,
11
+ chartPngFilename,
12
+ type PdfTextPage,
13
+ type RegisteredChart,
14
+ } from "./chartExport.js";
15
+
16
+ export type ChartExportFormat = "pdf" | "png" | "html";
17
+
18
+ export type ChartExportApi = {
19
+ downloadChart: (id: string) => Promise<void>;
20
+ downloadAll: () => Promise<void>;
21
+ /**
22
+ * @param extraPages - Native-text pages appended after the captured chart
23
+ * images (e.g. calculated recommendations that aren't rendered as charts).
24
+ */
25
+ downloadPdf: (
26
+ documentTitle: string,
27
+ extraPages?: PdfTextPage[],
28
+ ) => Promise<void>;
29
+ downloadHtml: (documentTitle: string) => Promise<void>;
30
+ downloadingChartId: string | null;
31
+ exportingFormat: ChartExportFormat | null;
32
+ exportLoadingLabel: string | null;
33
+ isExportingAll: boolean;
34
+ isBusy: boolean;
35
+ exportError: string | null;
36
+ clearExportError: () => void;
37
+ };
38
+
39
+ export type ChartExportRegistry = {
40
+ register: (chart: RegisteredChart) => () => void;
41
+ };
42
+
43
+ export const ChartExportRegistryContext =
44
+ createContext<ChartExportRegistry | null>(null);
45
+ export const ChartExportApiContext = createContext<ChartExportApi | null>(null);
46
+
47
+ export function useChartExport(): ChartExportApi | null {
48
+ return useContext(ChartExportApiContext);
49
+ }
50
+
51
+ export function useRegisterChart({
52
+ id,
53
+ title,
54
+ filename,
55
+ exportViews,
56
+ activeExportViewId,
57
+ setExportView,
58
+ }: ChartExportMeta & {
59
+ exportViews?: ChartExportView[];
60
+ activeExportViewId?: string;
61
+ setExportView?: (viewId: string) => Promise<void>;
62
+ }): RefCallback<HTMLElement> {
63
+ const registry = useContext(ChartExportRegistryContext);
64
+ const [element, setElement] = useState<HTMLElement | null>(null);
65
+ const resolvedFilename = filename ?? chartPngFilename(title, id);
66
+
67
+ useEffect(() => {
68
+ if (!registry || !element) {
69
+ return;
70
+ }
71
+ return registry.register({
72
+ id,
73
+ title,
74
+ filename: resolvedFilename,
75
+ element,
76
+ exportViews,
77
+ activeExportViewId,
78
+ setExportView,
79
+ });
80
+ }, [
81
+ activeExportViewId,
82
+ element,
83
+ exportViews,
84
+ id,
85
+ registry,
86
+ resolvedFilename,
87
+ setExportView,
88
+ title,
89
+ ]);
90
+
91
+ return setElement;
92
+ }
@@ -0,0 +1,21 @@
1
+ export function getDatedChartExportBasename(date = new Date()): string {
2
+ const year = date.getFullYear();
3
+ const month = String(date.getMonth() + 1).padStart(2, "0");
4
+ const day = String(date.getDate()).padStart(2, "0");
5
+ return `migration-export-${year}-${month}-${day}`;
6
+ }
7
+
8
+ export function getChartExportFilename(
9
+ format: "pdf" | "html" | "zip",
10
+ date = new Date(),
11
+ ): string {
12
+ const base = getDatedChartExportBasename(date);
13
+ if (format === "zip") {
14
+ return `${base}-charts.zip`;
15
+ }
16
+ return `${base}.${format}`;
17
+ }
18
+
19
+ export function getChartZipFilename(date = new Date()): string {
20
+ return getChartExportFilename("zip", date);
21
+ }
@@ -25,6 +25,8 @@ export const REPORT_CARD_EMPTY_STATE_TITLES = {
25
25
  operatingSystems: "Operating system data not collected",
26
26
  migrationStatus: "Migration status data not collected",
27
27
  issuesBreakdown: "Issues breakdown data not collected",
28
+ errors: "Error data not collected",
29
+ warnings: "Warning data not collected",
28
30
  hostPowerStates: "Host power state data not collected",
29
31
  vmPowerStates: "VM power state data not collected",
30
32
  } as const;
@@ -47,23 +47,10 @@ export const dashboardStyles = {
47
47
  overflow: hidden !important;
48
48
  `,
49
49
 
50
- cardPrint: css`
51
- border: 1px solid #d2d2d2 !important;
52
- border-radius: 10px;
53
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
54
- min-height: unset;
55
- max-height: unset;
56
- page-break-inside: avoid;
57
- `,
58
-
59
50
  storageCardOverflowHidden: css`
60
51
  overflow: hidden;
61
52
  `,
62
53
 
63
- storageCardOverflowVisible: css`
64
- overflow: visible;
65
- `,
66
-
67
54
  storageChartWrapper: css`
68
55
  display: flex;
69
56
  justify-content: center;
@@ -72,16 +59,6 @@ export const dashboardStyles = {
72
59
  padding: 20px 0;
73
60
  `,
74
61
 
75
- storageExportSectionMargin: css`
76
- margin-bottom: 24px;
77
- `,
78
-
79
- storageExportSectionTitle: css`
80
- font-size: 14px;
81
- font-weight: 600;
82
- margin-bottom: 12px;
83
- `,
84
-
85
62
  storageFlexFullWidth: css`
86
63
  width: 100%;
87
64
  `,
@@ -0,0 +1,83 @@
1
+ import { type ChartCaptureSource, releaseCanvas } from "./chartExport.js";
2
+
3
+ export interface HtmlReportImage {
4
+ name: string;
5
+ dataUrl: string;
6
+ }
7
+
8
+ export function escapeHtml(value: string): string {
9
+ return value
10
+ .replace(/&/g, "&amp;")
11
+ .replace(/</g, "&lt;")
12
+ .replace(/>/g, "&gt;")
13
+ .replace(/"/g, "&quot;")
14
+ .replace(/'/g, "&#39;");
15
+ }
16
+
17
+ export function buildHtmlReport(
18
+ documentTitle: string,
19
+ images: HtmlReportImage[],
20
+ generatedAt = new Date(),
21
+ ): string {
22
+ const sections = images
23
+ .map(
24
+ (image) => `
25
+ <section>
26
+ <h2>${escapeHtml(image.name)}</h2>
27
+ <img src="${image.dataUrl}" alt="${escapeHtml(image.name)}" />
28
+ </section>`,
29
+ )
30
+ .join("\n");
31
+
32
+ return `<!DOCTYPE html>
33
+ <html lang="en">
34
+ <head>
35
+ <meta charset="utf-8" />
36
+ <title>${escapeHtml(documentTitle)}</title>
37
+ <style>
38
+ body {
39
+ font-family: RedHatText, Helvetica, Arial, sans-serif;
40
+ margin: 32px auto;
41
+ max-width: 1100px;
42
+ background: #fff;
43
+ color: #151515;
44
+ }
45
+ h1 { font-size: 28px; margin-bottom: 8px; }
46
+ h2 { font-size: 18px; margin: 32px 0 12px; }
47
+ .meta { color: #6a6e73; margin-bottom: 24px; }
48
+ img {
49
+ width: 100%;
50
+ height: auto;
51
+ }
52
+ </style>
53
+ </head>
54
+ <body>
55
+ <h1>${escapeHtml(documentTitle)}</h1>
56
+ <p class="meta">Generated ${escapeHtml(generatedAt.toLocaleString())}</p>
57
+ ${sections}
58
+ </body>
59
+ </html>
60
+ `;
61
+ }
62
+
63
+ export async function buildHtmlFromCharts(
64
+ charts: ChartCaptureSource[],
65
+ documentTitle: string,
66
+ ): Promise<Blob> {
67
+ const images: HtmlReportImage[] = [];
68
+ for (const chart of charts) {
69
+ const canvas = await chart.capture();
70
+ try {
71
+ images.push({
72
+ name: chart.title,
73
+ dataUrl: canvas.toDataURL("image/png"),
74
+ });
75
+ } finally {
76
+ releaseCanvas(canvas);
77
+ }
78
+ }
79
+
80
+ return new Blob([buildHtmlReport(documentTitle, images)], {
81
+ type: "text/html;charset=utf-8",
82
+ });
83
+ }
@@ -3,6 +3,61 @@ export {
3
3
  CardEmptyState,
4
4
  type CardEmptyStateProps,
5
5
  } from "./CardEmptyState.js";
6
+ export {
7
+ ChartDownloadButton,
8
+ ChartHeaderActions,
9
+ } from "./ChartDownloadButton.js";
10
+ export {
11
+ type BuildChartDocument,
12
+ type BuildPdfDocument,
13
+ type CaptureChartElement,
14
+ ChartExportProvider,
15
+ type ChartExportProviderProps,
16
+ type EncodeChartPng,
17
+ type ZipChartFiles,
18
+ } from "./ChartExportProvider.js";
19
+ export { ChartExportSurface } from "./ChartExportSurface.js";
20
+ export {
21
+ CpuAndMemoryOverview,
22
+ type CpuAndMemoryOverviewProps,
23
+ } from "./CpuAndMemoryOverview.js";
24
+ export { captureChartElement } from "./captureChartElement.js";
25
+ export {
26
+ CHART_EXPORT_CAPTURING_ATTR,
27
+ CHART_EXPORT_HIDE_ATTR,
28
+ CHART_EXPORT_SCROLL_ATTR,
29
+ type ChartCaptureSource,
30
+ type ChartExportFile,
31
+ type ChartExportMeta,
32
+ type ChartExportView,
33
+ canvasToBlob,
34
+ chartExportHideProps,
35
+ chartExportRootStyle,
36
+ chartExportScrollProps,
37
+ chartExportViewsFromLabels,
38
+ chartPngFilename,
39
+ downloadBlob,
40
+ type PdfTextPage,
41
+ type PdfTextPageItem,
42
+ type RegisteredChart,
43
+ releaseCanvas,
44
+ restoreChartExportViews,
45
+ shouldIgnoreChartExportElement,
46
+ slugifyExportName,
47
+ sortRegisteredChartsByDocumentOrder,
48
+ toBulkChartCaptureSources,
49
+ } from "./chartExport.js";
50
+ export {
51
+ type ChartExportApi,
52
+ type ChartExportFormat,
53
+ useChartExport,
54
+ useRegisterChart,
55
+ } from "./chartExportContext.js";
56
+ export {
57
+ getChartExportFilename,
58
+ getChartZipFilename,
59
+ getDatedChartExportBasename,
60
+ } from "./chartExportFilenames.js";
6
61
  export {
7
62
  chartColorFailure,
8
63
  chartColorSuccess,
@@ -17,6 +72,11 @@ export {
17
72
  EmptySearchResults,
18
73
  type EmptySearchResultsProps,
19
74
  } from "./EmptySearchResults.js";
75
+ export {
76
+ ErrorTable,
77
+ type ErrorTableProps,
78
+ type MigrationIssueLike,
79
+ } from "./ErrorTable.js";
20
80
  export {
21
81
  FeatureStatusBadge,
22
82
  type FeatureStatusBadgeProps,
@@ -25,6 +85,17 @@ export {
25
85
  HostPowerStates,
26
86
  type HostPowerStatesProps,
27
87
  } from "./HostPowerStates.js";
88
+ export {
89
+ type HostLike,
90
+ HostsOverview,
91
+ type HostsOverviewProps,
92
+ } from "./HostsOverview.js";
93
+ export {
94
+ buildHtmlFromCharts,
95
+ buildHtmlReport,
96
+ escapeHtml,
97
+ type HtmlReportImage,
98
+ } from "./htmlExport.js";
28
99
  export {
29
100
  InfrastructureSummary,
30
101
  type InfrastructureSummaryProps,
@@ -54,6 +125,14 @@ export {
54
125
  visibleNetworks,
55
126
  vsanStatus,
56
127
  } from "./infrastructureSummaryModel.js";
128
+ export {
129
+ type NetworkInfraLike,
130
+ type NetworkLike,
131
+ NetworkOverview,
132
+ type NetworkOverviewProps,
133
+ type NicCountHistogram,
134
+ type NicCountSummary,
135
+ } from "./NetworkOverview.js";
57
136
  export { OSBarChart, OSDistribution } from "./OSDistribution.js";
58
137
  export { OsSupportTiersHelpPopover } from "./OsSupportTiersHelpPopover.js";
59
138
  export { OsUpgradeNotice } from "./OsUpgradeNotice.js";
@@ -86,6 +165,12 @@ export {
86
165
  PowerStateCard,
87
166
  type PowerStateCardProps,
88
167
  } from "./PowerStateCard.js";
168
+ export { buildPdfFromCharts } from "./pdfExport.js";
169
+ export {
170
+ type PdfExportSegment,
171
+ sliceCanvas,
172
+ splitSegmentForPageHeight,
173
+ } from "./pdfPage.js";
89
174
  export type { PowerStateChartModel } from "./powerStates.js";
90
175
  export {
91
176
  buildHostPowerStateChart,
@@ -93,6 +178,18 @@ export {
93
178
  HOST_POWER_COLORS,
94
179
  VM_POWER_COLORS,
95
180
  } from "./powerStates.js";
181
+ export {
182
+ ExportReportButton,
183
+ type ExportReportButtonProps,
184
+ ReportExportMenu,
185
+ type ReportExportMenuProps,
186
+ } from "./ReportExportMenu.js";
187
+ export { ReportTable, type ReportTableProps } from "./ReportTable.js";
188
+ export {
189
+ type ReportExportOption,
190
+ type StandardReportExportHandlers,
191
+ standardReportExportOptions,
192
+ } from "./reportExportOptions.js";
96
193
  export { SupportTierBadge } from "./SupportTierBadge.js";
97
194
  export {
98
195
  ALL_TIERS_FILTER,
@@ -108,3 +205,8 @@ export {
108
205
  VmPowerStates,
109
206
  type VmPowerStatesProps,
110
207
  } from "./VmPowerStates.js";
208
+ export {
209
+ WarningsTable,
210
+ type WarningsTableProps,
211
+ } from "./WarningsTable.js";
212
+ export { zipChartPngs } from "./zipChartPngs.js";
@@ -0,0 +1,233 @@
1
+ import jsPDF from "jspdf";
2
+ import {
3
+ type ChartCaptureSource,
4
+ type PdfTextPage,
5
+ releaseCanvas,
6
+ } from "./chartExport.js";
7
+ import { fitPdfImageSize, placePdfBlock, sliceCanvas } from "./pdfPage.js";
8
+
9
+ const MARGIN_MM = 10;
10
+ const GAP_MM = 8;
11
+ /** Flatten captures onto white; jsPDF cannot embed transparent PNG alpha cleanly. */
12
+ const PAGE_BACKGROUND = "#ffffff";
13
+
14
+ export async function buildPdfFromCharts(
15
+ charts: ChartCaptureSource[],
16
+ documentTitle: string,
17
+ extraPages: PdfTextPage[] = [],
18
+ ): Promise<Blob> {
19
+ const pdf = new jsPDF("p", "mm", "a4");
20
+ const pageWidth = pdf.internal.pageSize.getWidth();
21
+ const pageHeight = pdf.internal.pageSize.getHeight();
22
+ const contentWidth = pageWidth - MARGIN_MM * 2;
23
+ const contentHeight = pageHeight - MARGIN_MM * 2;
24
+ const pageTop = MARGIN_MM;
25
+ const pageBottom = pageHeight - MARGIN_MM;
26
+
27
+ addCoverPage(
28
+ pdf,
29
+ documentTitle,
30
+ [...charts.map((chart) => chart.title), ...extraPages.map((p) => p.title)],
31
+ pageWidth,
32
+ pageHeight,
33
+ );
34
+
35
+ let cursorY: number | null = null;
36
+ for (const chart of charts) {
37
+ const canvas = await chart.capture();
38
+ try {
39
+ if (canvas.width < 1 || canvas.height < 1) {
40
+ throw new Error("Chart capture produced an empty image");
41
+ }
42
+ const size = fitPdfImageSize(
43
+ canvas.width,
44
+ canvas.height,
45
+ contentWidth,
46
+ contentHeight,
47
+ );
48
+ const placement = placePdfBlock(
49
+ cursorY,
50
+ size.heightMm,
51
+ pageTop,
52
+ pageBottom,
53
+ );
54
+ if (placement.needsNewPage) {
55
+ pdf.addPage();
56
+ }
57
+ addChartImage(
58
+ pdf,
59
+ canvas,
60
+ MARGIN_MM + (contentWidth - size.widthMm) / 2,
61
+ placement.y,
62
+ size.widthMm,
63
+ size.heightMm,
64
+ );
65
+ cursorY = placement.y + size.heightMm + GAP_MM;
66
+ } finally {
67
+ releaseCanvas(canvas);
68
+ }
69
+ }
70
+
71
+ if (extraPages.length > 0) {
72
+ addExtraPages(pdf, extraPages, pageWidth, pageHeight);
73
+ }
74
+
75
+ addPageNumbers(pdf, pageWidth, pageHeight);
76
+ return pdf.output("blob");
77
+ }
78
+
79
+ function addCoverPage(
80
+ pdf: jsPDF,
81
+ documentTitle: string,
82
+ tocItems: string[],
83
+ pageWidth: number,
84
+ pageHeight: number,
85
+ ): void {
86
+ const contentWidth = pageWidth - MARGIN_MM * 2;
87
+ pdf.setFontSize(18);
88
+ const titleLines = pdf.splitTextToSize(
89
+ documentTitle,
90
+ contentWidth,
91
+ ) as string[];
92
+ let titleY = MARGIN_MM + 8;
93
+ for (const line of titleLines) {
94
+ pdf.text(line, pageWidth / 2, titleY, { align: "center" });
95
+ titleY += 8;
96
+ }
97
+
98
+ pdf.setFontSize(11);
99
+ const generatedAt = new Date();
100
+ pdf.text(
101
+ `Generated: ${generatedAt.toLocaleDateString()} ${generatedAt.toLocaleTimeString()}`,
102
+ pageWidth / 2,
103
+ titleY + 4,
104
+ { align: "center" },
105
+ );
106
+
107
+ pdf.setFontSize(14);
108
+ pdf.text("Table of contents", MARGIN_MM, titleY + 16);
109
+ pdf.setFontSize(11);
110
+ let tocY = titleY + 26;
111
+ for (const item of tocItems) {
112
+ if (tocY > pageHeight - MARGIN_MM - 10) {
113
+ pdf.addPage();
114
+ tocY = MARGIN_MM;
115
+ }
116
+ pdf.text(`- ${item}`, MARGIN_MM, tocY);
117
+ tocY += 7;
118
+ }
119
+ }
120
+
121
+ function addChartImage(
122
+ pdf: jsPDF,
123
+ canvas: HTMLCanvasElement,
124
+ x: number,
125
+ y: number,
126
+ widthMm: number,
127
+ heightMm: number,
128
+ ): void {
129
+ const flattened = sliceCanvas(
130
+ canvas,
131
+ canvas.width,
132
+ canvas.height,
133
+ 0,
134
+ PAGE_BACKGROUND,
135
+ );
136
+ try {
137
+ pdf.addImage(
138
+ flattened.toDataURL("image/jpeg", 0.92),
139
+ "JPEG",
140
+ x,
141
+ y,
142
+ widthMm,
143
+ heightMm,
144
+ );
145
+ } finally {
146
+ releaseCanvas(flattened);
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Append one native-text PDF page per entry in `extraPages`, rendered as
152
+ * label/value rows rather than a captured image. Each page starts fresh so
153
+ * its content never bleeds into a chart page.
154
+ */
155
+ function addExtraPages(
156
+ pdf: jsPDF,
157
+ extraPages: PdfTextPage[],
158
+ pageWidth: number,
159
+ pageHeight: number,
160
+ ): void {
161
+ const LABEL_COL_WIDTH = 68; // mm — enough for the longest label
162
+ const VALUE_COL_X = MARGIN_MM + LABEL_COL_WIDTH;
163
+ const VALUE_COL_WIDTH = pageWidth - MARGIN_MM - LABEL_COL_WIDTH;
164
+ const LINE_HEIGHT = 7; // mm between rows
165
+
166
+ for (const page of extraPages) {
167
+ pdf.addPage();
168
+ let y = MARGIN_MM + 8;
169
+
170
+ pdf.setFontSize(16);
171
+ pdf.setFont("helvetica", "bold");
172
+ pdf.text(page.title, MARGIN_MM, y);
173
+ y += 10;
174
+
175
+ pdf.setDrawColor(0, 103, 187);
176
+ pdf.setLineWidth(0.5);
177
+ pdf.line(MARGIN_MM, y, pageWidth - MARGIN_MM, y);
178
+ y += 10;
179
+
180
+ pdf.setFontSize(11);
181
+
182
+ for (const item of page.items) {
183
+ pdf.setFont("helvetica", "bold");
184
+ const labelLines = pdf.splitTextToSize(
185
+ item.label,
186
+ LABEL_COL_WIDTH - 4,
187
+ ) as string[];
188
+ pdf.text(labelLines, MARGIN_MM, y);
189
+
190
+ pdf.setFont("helvetica", "normal");
191
+ const valueLines = pdf.splitTextToSize(
192
+ item.value,
193
+ VALUE_COL_WIDTH,
194
+ ) as string[];
195
+ pdf.text(valueLines, VALUE_COL_X, y);
196
+
197
+ y += LINE_HEIGHT * Math.max(labelLines.length, valueLines.length);
198
+
199
+ if (y > pageHeight - MARGIN_MM - 25) {
200
+ pdf.addPage();
201
+ y = MARGIN_MM + 10;
202
+ }
203
+ }
204
+
205
+ if (page.footer) {
206
+ y += 4;
207
+ pdf.setFontSize(9);
208
+ pdf.setFont("helvetica", "bolditalic");
209
+ const footerLines = pdf.splitTextToSize(
210
+ page.footer,
211
+ pageWidth - MARGIN_MM * 2,
212
+ ) as string[];
213
+ pdf.text(footerLines, MARGIN_MM, y);
214
+ pdf.setFont("helvetica", "normal");
215
+ pdf.setFontSize(11);
216
+ }
217
+ }
218
+ }
219
+
220
+ function addPageNumbers(
221
+ pdf: jsPDF,
222
+ pageWidth: number,
223
+ pageHeight: number,
224
+ ): void {
225
+ const totalPages = pdf.getNumberOfPages();
226
+ pdf.setFontSize(9);
227
+ for (let page = 1; page <= totalPages; page++) {
228
+ pdf.setPage(page);
229
+ pdf.text(`Page ${page} of ${totalPages}`, pageWidth / 2, pageHeight - 6, {
230
+ align: "center",
231
+ });
232
+ }
233
+ }
@@ -0,0 +1,83 @@
1
+ export interface PdfExportSegment {
2
+ top: number;
3
+ height: number;
4
+ }
5
+
6
+ export function splitSegmentForPageHeight(
7
+ segment: PdfExportSegment,
8
+ imgHeight: number,
9
+ pageHeightPx: number,
10
+ ): PdfExportSegment[] {
11
+ const total = Math.max(1, Math.min(segment.height, imgHeight - segment.top));
12
+ if (pageHeightPx <= 0 || total <= pageHeightPx) {
13
+ return [{ top: segment.top, height: total }];
14
+ }
15
+
16
+ const slices: PdfExportSegment[] = [];
17
+ let offset = 0;
18
+ while (offset < total) {
19
+ const height = Math.min(pageHeightPx, total - offset);
20
+ slices.push({ top: segment.top + offset, height });
21
+ offset += height;
22
+ }
23
+ return slices;
24
+ }
25
+
26
+ export function fitPdfImageSize(
27
+ pixelWidth: number,
28
+ pixelHeight: number,
29
+ maxWidthMm: number,
30
+ maxHeightMm: number,
31
+ ): { widthMm: number; heightMm: number } {
32
+ const safeWidth = Math.max(1, pixelWidth);
33
+ const safeHeight = Math.max(1, pixelHeight);
34
+ const widthScale = maxWidthMm / safeWidth;
35
+ const heightAtFullWidth = safeHeight * widthScale;
36
+ if (heightAtFullWidth <= maxHeightMm) {
37
+ return { widthMm: maxWidthMm, heightMm: heightAtFullWidth };
38
+ }
39
+ const scale = maxHeightMm / safeHeight;
40
+ return { widthMm: safeWidth * scale, heightMm: maxHeightMm };
41
+ }
42
+
43
+ export function placePdfBlock(
44
+ cursorY: number | null,
45
+ blockHeightMm: number,
46
+ pageTopMm: number,
47
+ pageBottomMm: number,
48
+ ): { y: number; needsNewPage: boolean } {
49
+ if (cursorY === null || cursorY + blockHeightMm > pageBottomMm) {
50
+ return { y: pageTopMm, needsNewPage: true };
51
+ }
52
+ return { y: cursorY, needsNewPage: false };
53
+ }
54
+
55
+ export function sliceCanvas(
56
+ sourceCanvas: HTMLCanvasElement,
57
+ width: number,
58
+ height: number,
59
+ offsetY: number,
60
+ backgroundColor: string,
61
+ ): HTMLCanvasElement {
62
+ const pageCanvas = document.createElement("canvas");
63
+ pageCanvas.width = width;
64
+ pageCanvas.height = height;
65
+ const context = pageCanvas.getContext("2d");
66
+ if (!context) {
67
+ throw new Error("Canvas 2D context unavailable");
68
+ }
69
+ context.fillStyle = backgroundColor;
70
+ context.fillRect(0, 0, width, height);
71
+ context.drawImage(
72
+ sourceCanvas,
73
+ 0,
74
+ offsetY,
75
+ width,
76
+ height,
77
+ 0,
78
+ 0,
79
+ width,
80
+ height,
81
+ );
82
+ return pageCanvas;
83
+ }