abstract-document 6.1.0 → 6.3.2

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 (184) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/LICENSE +21 -0
  3. package/lib/abstract-document/table/table-cell.d.ts +4 -0
  4. package/lib/abstract-document/table/table-cell.d.ts.map +1 -1
  5. package/lib/abstract-document/table/table-cell.js +4 -2
  6. package/lib/abstract-document/table/table-cell.js.map +1 -1
  7. package/lib/abstract-document/table/table-row.d.ts.map +1 -1
  8. package/lib/abstract-document/table/table-row.js +1 -1
  9. package/lib/abstract-document/table/table-row.js.map +1 -1
  10. package/lib/abstract-document-exporters/__tests__/docx2/test-defs/simple-table-colSpan-and-rowSpan.d.ts +3 -0
  11. package/lib/abstract-document-exporters/__tests__/docx2/test-defs/simple-table-colSpan-and-rowSpan.d.ts.map +1 -0
  12. package/lib/abstract-document-exporters/__tests__/docx2/test-defs/simple-table-colSpan-and-rowSpan.js +553 -0
  13. package/lib/abstract-document-exporters/__tests__/docx2/test-defs/simple-table-colSpan-and-rowSpan.js.map +1 -0
  14. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/group-too-big-for-one-page.d.ts +3 -0
  15. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/group-too-big-for-one-page.d.ts.map +1 -0
  16. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/group-too-big-for-one-page.js +251 -0
  17. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/group-too-big-for-one-page.js.map +1 -0
  18. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-row-that-doesnt-fit-page.d.ts +3 -0
  19. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-row-that-doesnt-fit-page.d.ts.map +1 -0
  20. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-row-that-doesnt-fit-page.js +294 -0
  21. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-row-that-doesnt-fit-page.js.map +1 -0
  22. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-single-row.d.ts +3 -0
  23. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-single-row.d.ts.map +1 -0
  24. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-single-row.js +209 -0
  25. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-single-row.js.map +1 -0
  26. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-group.d.ts +3 -0
  27. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-group.d.ts.map +1 -0
  28. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-group.js +465 -0
  29. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-group.js.map +1 -0
  30. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-bottom.d.ts +3 -0
  31. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-bottom.d.ts.map +1 -0
  32. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-bottom.js +423 -0
  33. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-bottom.js.map +1 -0
  34. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top-single-table.d.ts +3 -0
  35. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top-single-table.d.ts.map +1 -0
  36. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top-single-table.js +331 -0
  37. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top-single-table.js.map +1 -0
  38. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top.d.ts +3 -0
  39. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top.d.ts.map +1 -0
  40. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top.js +423 -0
  41. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top.js.map +1 -0
  42. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin.d.ts +3 -0
  43. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin.d.ts.map +1 -0
  44. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin.js +56 -0
  45. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin.js.map +1 -0
  46. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan-multipages.d.ts +3 -0
  47. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan-multipages.d.ts.map +1 -0
  48. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan-multipages.js +1541 -0
  49. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan-multipages.js.map +1 -0
  50. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan.d.ts +3 -0
  51. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan.d.ts.map +1 -0
  52. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan.js +512 -0
  53. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan.js.map +1 -0
  54. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-colSpan-and-rowSpan.d.ts +3 -0
  55. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-colSpan-and-rowSpan.d.ts.map +1 -0
  56. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-colSpan-and-rowSpan.js +396 -0
  57. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-colSpan-and-rowSpan.js.map +1 -0
  58. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-with-header.d.ts +3 -0
  59. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-with-header.d.ts.map +1 -0
  60. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-with-header.js +148 -0
  61. package/lib/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-with-header.js.map +1 -0
  62. package/lib/abstract-document-exporters/docx2/render.js +1 -0
  63. package/lib/abstract-document-exporters/docx2/render.js.map +1 -1
  64. package/lib/abstract-document-exporters/pdf/measure.d.ts +1 -0
  65. package/lib/abstract-document-exporters/pdf/measure.d.ts.map +1 -1
  66. package/lib/abstract-document-exporters/pdf/measure.js +2 -1
  67. package/lib/abstract-document-exporters/pdf/measure.js.map +1 -1
  68. package/lib/abstract-document-exporters/pdf/paginate.d.ts.map +1 -1
  69. package/lib/abstract-document-exporters/pdf/paginate.js +108 -25
  70. package/lib/abstract-document-exporters/pdf/paginate.js.map +1 -1
  71. package/lib/abstract-document-exporters/pdf/pre-process.d.ts.map +1 -1
  72. package/lib/abstract-document-exporters/pdf/pre-process.js +66 -30
  73. package/lib/abstract-document-exporters/pdf/pre-process.js.map +1 -1
  74. package/lib/abstract-document-exporters/pdf/render.d.ts.map +1 -1
  75. package/lib/abstract-document-exporters/pdf/render.js +19 -6
  76. package/lib/abstract-document-exporters/pdf/render.js.map +1 -1
  77. package/package.json +3 -2
  78. package/src/abstract-document/table/table-cell.ts +9 -7
  79. package/src/abstract-document/table/table-row.ts +2 -5
  80. package/src/abstract-document-exporters/__tests__/docx2/test-defs/simple-table-colSpan-and-rowSpan.tsx +587 -0
  81. package/src/abstract-document-exporters/__tests__/docx2/tmp/Group.docx +0 -0
  82. package/src/abstract-document-exporters/__tests__/docx2/tmp/Header and footer.docx and b/package/src/abstract-document-exporters/__tests__/docx2/tmp/Header → footer.docx +0 -0
  83. package/src/abstract-document-exporters/__tests__/docx2/tmp/Mixed hyperlink and textrun.docx and b/package/src/abstract-document-exporters/__tests__/docx2/tmp/Mixed hyperlink → textrun.docx +0 -0
  84. package/src/abstract-document-exporters/__tests__/docx2/tmp/Mixed textrun and images.docx and b/package/src/abstract-document-exporters/__tests__/docx2/tmp/Mixed textrun → images.docx +0 -0
  85. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple images.docx +0 -0
  86. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple tables.docx +0 -0
  87. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple textrun with Center alignment.docx +0 -0
  88. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple textrun with End alignment.docx +0 -0
  89. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple textrun with Start alignment that linebreaks.docx +0 -0
  90. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple textrun with Start alignment.docx +0 -0
  91. package/src/abstract-document-exporters/__tests__/docx2/tmp/Page numbering.docx +0 -0
  92. rowSpan.docx +0 -0
  93. package/src/abstract-document-exporters/__tests__/docx2/tmp/Simple table without rows.docx +0 -0
  94. package/src/abstract-document-exporters/__tests__/docx2/tmp/Simple table.docx +0 -0
  95. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single hyperlink.docx +0 -0
  96. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single image.docx +0 -0
  97. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with Center alignment that linebreaks.docx +0 -0
  98. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with Center alignment.docx +0 -0
  99. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with End alignment that linebreaks.docx +0 -0
  100. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with End alignment.docx +0 -0
  101. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with Start alignment that linebreaks.docx +0 -0
  102. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with Start alignment.docx +0 -0
  103. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun.docx +0 -0
  104. package/src/abstract-document-exporters/__tests__/docx2/tmp/hello.docx +0 -0
  105. package/src/abstract-document-exporters/__tests__/docx2/tmp/world.docx +0 -0
  106. package/src/abstract-document-exporters/__tests__/pdf/test-defs/group-too-big-for-one-page.tsx +272 -0
  107. package/src/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-row-that-doesnt-fit-page.tsx +317 -0
  108. package/src/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-single-row.tsx +227 -0
  109. package/src/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-group.tsx +500 -0
  110. package/src/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-bottom.tsx +455 -0
  111. package/src/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top-single-table.tsx +354 -0
  112. package/src/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-margin-top.tsx +455 -0
  113. package/src/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan-multipages.tsx +1567 -0
  114. package/src/abstract-document-exporters/__tests__/pdf/test-defs/page-break-table-with-rowSpan.tsx +545 -0
  115. package/src/abstract-document-exporters/__tests__/pdf/test-defs/simple-table-colSpan-and-rowSpan.tsx +425 -0
  116. package/src/abstract-document-exporters/__tests__/pdf/tmp/Group Paragraphs.pdf +2 -2
  117. package/src/abstract-document-exporters/__tests__/pdf/tmp/Group Tables.pdf +2 -2
  118. package/src/abstract-document-exporters/__tests__/pdf/tmp/Group too big for one page.pdf +268 -0
  119. package/src/abstract-document-exporters/__tests__/pdf/tmp/Header and footer.pdf +2 -2
  120. package/src/abstract-document-exporters/__tests__/pdf/tmp/Hyperlink internal linktarget.pdf +2 -2
  121. package/src/abstract-document-exporters/__tests__/pdf/tmp/Hyperlink internal.pdf +2 -2
  122. package/src/abstract-document-exporters/__tests__/pdf/tmp/Margins header body footer.pdf +2 -2
  123. package/src/abstract-document-exporters/__tests__/pdf/tmp/Mixed hyperlink and textrun.pdf +2 -2
  124. package/src/abstract-document-exporters/__tests__/pdf/tmp/Mixed textrun and images.pdf and b/package/src/abstract-document-exporters/__tests__/pdf/tmp/Mixed textrun → images.pdf +0 -0
  125. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple images overflow.pdf +0 -0
  126. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple images.pdf +0 -0
  127. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple textrun with Center alignment.pdf +2 -2
  128. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple textrun with End alignment.pdf +2 -2
  129. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple textrun with Start alignment that linebreaks.pdf +2 -2
  130. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple textrun with Start alignment.pdf +2 -2
  131. package/src/abstract-document-exporters/__tests__/pdf/tmp/Page numbering.pdf +2 -2
  132. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table row that doesnt fit page.pdf +302 -0
  133. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table single row.pdf +234 -0
  134. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with group.pdf +438 -0
  135. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with margin bottom.pdf +404 -0
  136. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with margin top single table.pdf +336 -0
  137. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with margin top.pdf +404 -0
  138. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with margin.pdf +404 -0
  139. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with rowSpan multipages.pdf +3983 -0
  140. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with rowSpan.pdf +486 -0
  141. package/src/abstract-document-exporters/__tests__/pdf/tmp/Paragraph numbering.pdf +143 -0
  142. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table all auto.pdf +2 -2
  143. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table all fix.pdf +2 -2
  144. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table background.pdf +2 -2
  145. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table cell padding of image.pdf +0 -0
  146. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table cell padding of text.pdf +2 -2
  147. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table center alignment.pdf +2 -2
  148. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table colSpan and rowSpan.pdf +375 -0
  149. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table mix auto fix.pdf +2 -2
  150. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table padding.pdf +2 -2
  151. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table right alignment.pdf +2 -2
  152. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table row alignment.pdf +2 -2
  153. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table text alignment in cell with wrapping.pdf +2 -2
  154. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table text alignment in cell.pdf +2 -2
  155. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table with default colored borders.pdf +2 -2
  156. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table with header.pdf +205 -0
  157. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table with multi colored borders.pdf +2 -2
  158. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table with single colored borders.pdf +2 -2
  159. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single date.pdf +14 -14
  160. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single hyperlink centered.pdf +2 -2
  161. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single hyperlink right aligned.pdf +2 -2
  162. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single hyperlink.pdf +2 -2
  163. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image svg color hex.pdf +2 -2
  164. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image svg color name.pdf +2 -2
  165. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image svg color url.pdf +2 -2
  166. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image.pdf +0 -0
  167. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Center alignment that linebreaks.pdf +2 -2
  168. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Center alignment.pdf +2 -2
  169. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with End alignment that linebreaks.pdf +2 -2
  170. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with End alignment.pdf +2 -2
  171. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Justify alignment.pdf +2 -2
  172. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Start alignment that linebreaks.pdf +2 -2
  173. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Start alignment.pdf +2 -2
  174. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with super and subscripts.pdf +2 -2
  175. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun.pdf +2 -2
  176. package/src/abstract-document-exporters/__tests__/pdf/tmp/Table of content separator.pdf +2 -2
  177. package/src/abstract-document-exporters/__tests__/pdf/tmp/hello.pdf +2 -2
  178. package/src/abstract-document-exporters/__tests__/pdf/tmp/world.pdf +2 -2
  179. package/src/abstract-document-exporters/docx2/render.ts +1 -0
  180. package/src/abstract-document-exporters/pdf/measure.ts +1 -1
  181. package/src/abstract-document-exporters/pdf/paginate.ts +181 -89
  182. package/src/abstract-document-exporters/pdf/pre-process.ts +76 -59
  183. package/src/abstract-document-exporters/pdf/render.ts +23 -5
  184. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple tables.zip +0 -0
@@ -1,7 +1,10 @@
1
1
  import * as R from "ramda";
2
+ import { split } from "ramda";
2
3
  import * as AD from "../../abstract-document/index";
4
+ import { Table } from "../../abstract-document/section-elements/table";
3
5
  import { getResources } from "../shared/get_resources";
4
6
  import { registerFonts } from "./font";
7
+ import { measureTable } from "./measure";
5
8
 
6
9
  /* tslint:disable:no-any */
7
10
 
@@ -25,25 +28,23 @@ export function paginate(
25
28
  let pdf = new pdfKit({
26
29
  compress: false,
27
30
  autoFirstPage: false,
28
- bufferPages: true
31
+ bufferPages: true,
29
32
  }) as any;
30
33
 
31
- registerFonts(
32
- (fontName: string, fontSource: AD.Font.FontSource) =>
33
- pdf.registerFont(fontName, fontSource),
34
- document
35
- );
34
+ registerFonts((fontName: string, fontSource: AD.Font.FontSource) => pdf.registerFont(fontName, fontSource), document);
36
35
 
37
36
  let pages = new Array<Page>();
38
37
  for (let section of document.children) {
39
38
  const previousPage = pages.length > 0 ? pages[pages.length - 1] : undefined;
40
- pages.push(...splitSection(resources, desiredSizes, previousPage, section));
39
+ pages.push(...splitSection(pdfKit, document, resources, desiredSizes, previousPage, section));
41
40
  }
42
41
 
43
42
  return pages;
44
43
  }
45
44
 
46
45
  function splitSection(
46
+ pdfKit: any,
47
+ document: AD.AbstractDoc.AbstractDoc,
47
48
  parentResources: AD.Resources.Resources,
48
49
  desiredSizes: Map<{}, AD.Size.Size>,
49
50
  previousPage: Page | undefined,
@@ -60,14 +61,7 @@ function splitSection(
60
61
  for (let i = 0; i < children.length; ++i) {
61
62
  const element = children[i];
62
63
  if (element.type === "PageBreak") {
63
- currentPage = createPage(
64
- resources,
65
- desiredSizes,
66
- currentPage,
67
- section,
68
- elements,
69
- pages.length === 0
70
- );
64
+ currentPage = createPage(resources, desiredSizes, currentPage, section, elements, pages.length === 0);
71
65
  pages.push(currentPage);
72
66
  elements = [];
73
67
  elementsHeight = 0;
@@ -75,13 +69,9 @@ function splitSection(
75
69
  }
76
70
  const elementSize = getDesiredSize(element, desiredSizes);
77
71
 
78
- // Collapse groups the doesn't fit on empty page
72
+ // Collapse groups that doesn't fit on empty page
79
73
  if (elementSize.height > contentRect.height && element.type === "Group") {
80
- children = [
81
- ...children.slice(0, i),
82
- ...element.children,
83
- ...children.slice(i + 1)
84
- ];
74
+ children = [...children.slice(0, i), ...element.children, ...children.slice(i + 1)];
85
75
  i--;
86
76
  continue;
87
77
  }
@@ -89,25 +79,53 @@ function splitSection(
89
79
  elements.push(element);
90
80
  elementsHeight += elementSize.height;
91
81
 
92
- const [leadingSpace, trailingSpace] = getLeadingAndTrailingSpace(
93
- resources,
94
- section,
95
- elements
96
- );
82
+ const [leadingSpace, trailingSpace] = getLeadingAndTrailingSpace(resources, section, elements);
97
83
  const availableHeight = contentRect.height + leadingSpace + trailingSpace;
98
84
  if (elementsHeight > availableHeight) {
85
+ if (element.type === "Table" && element.children.length > 1) {
86
+ //Remove last element
87
+ elements.pop();
88
+ elementsHeight -= elementSize.height;
89
+
90
+ //Try to split table
91
+ const [tableHead, tableTail] = splitTable(
92
+ pdfKit,
93
+ document,
94
+ resources,
95
+ desiredSizes,
96
+ element,
97
+ elementsHeight,
98
+ availableHeight,
99
+ elements.length
100
+ );
101
+
102
+ const tableSplit: AD.Table.Table[] = [];
103
+ if (tableHead) {
104
+ elements.push(tableHead);
105
+ tableSplit.push(tableHead);
106
+ } else {
107
+ i--;
108
+ }
109
+ if (tableTail) {
110
+ tableSplit.push(tableTail);
111
+ }
112
+
113
+ //Add split table to children to process tableTail in next iteration
114
+ children = [...children.slice(0, i), ...tableSplit, ...children.slice(i + 1)];
115
+
116
+ currentPage = createPage(resources, desiredSizes, currentPage, section, elements, pages.length === 0);
117
+ pages.push(currentPage);
118
+ elements = [];
119
+ elementsHeight = 0;
120
+
121
+ continue;
122
+ }
123
+
99
124
  if (elements.length > 1) {
100
125
  elements.pop();
101
126
  i--;
102
127
  }
103
- currentPage = createPage(
104
- resources,
105
- desiredSizes,
106
- currentPage,
107
- section,
108
- elements,
109
- pages.length === 0
110
- );
128
+ currentPage = createPage(resources, desiredSizes, currentPage, section, elements, pages.length === 0);
111
129
  pages.push(currentPage);
112
130
  elements = [];
113
131
  elementsHeight = 0;
@@ -115,19 +133,62 @@ function splitSection(
115
133
  }
116
134
 
117
135
  if (elements.length > 0) {
118
- pages.push(
119
- createPage(
136
+ pages.push(createPage(resources, desiredSizes, currentPage, section, elements, pages.length === 0));
137
+ }
138
+
139
+ return pages;
140
+ }
141
+
142
+ function splitTable(
143
+ pdfKit: any,
144
+ document: AD.AbstractDoc.AbstractDoc,
145
+ resources: AD.Resources.Resources,
146
+ desiredSizes: Map<{}, AD.Size.Size>,
147
+ table: AD.Table.Table,
148
+ elementsHeight: number,
149
+ availableHeight: number,
150
+ elementsLength: number
151
+ ): [AD.Table.Table | undefined, AD.Table.Table | undefined] {
152
+ let tableHead = undefined;
153
+ let tableRest = undefined;
154
+ elementsHeight += table.style.margins.top || 0;
155
+
156
+ //Find where to split table
157
+ for (const [rowIndex, row] of table.children.entries()) {
158
+ const rowSize = getDesiredSize(row, desiredSizes);
159
+ elementsHeight += rowSize.height;
160
+
161
+ if (elementsHeight > availableHeight) {
162
+ if (rowIndex === 0 && elementsLength !== 0) {
163
+ //Not even first row fit on the page, shove it all to the next page
164
+ return [undefined, table];
165
+ }
166
+
167
+ const [newTableHead, newTableRest] = splitTableAt(
168
+ pdfKit,
169
+ document,
120
170
  resources,
121
171
  desiredSizes,
122
- currentPage,
123
- section,
124
- elements,
125
- pages.length === 0
126
- )
127
- );
172
+ table,
173
+ Math.max(rowIndex, 1)
174
+ );
175
+
176
+ if (newTableHead.children.length > 0) {
177
+ tableHead = newTableHead;
178
+ }
179
+ if (newTableRest.children.length > 0) {
180
+ tableRest = newTableRest;
181
+ }
182
+ break;
183
+ }
128
184
  }
129
185
 
130
- return pages;
186
+ if (!tableHead && !tableRest) {
187
+ //Didnt find a place to split the table, meaning the whole table fits on current page
188
+ return [table, undefined];
189
+ }
190
+
191
+ return [tableHead, tableRest];
131
192
  }
132
193
 
133
194
  function createPage(
@@ -149,36 +210,22 @@ function createPage(
149
210
  top: 0,
150
211
  left: 0,
151
212
  right: 0,
152
- bottom: 0
153
- }
213
+ bottom: 0,
214
+ },
154
215
  };
155
216
  const pageNo = previousPage ? previousPage.pageNo + 1 : 1;
156
217
 
157
218
  const sectionName = isFirst && section.id !== "" ? [section.id] : [];
158
219
  // For now, only support link targets at base level. Tree search would be needed to find all targets.
159
220
  const targetNames = R.unnest(
160
- elements.map(
161
- e =>
162
- e.type === "Paragraph"
163
- ? e.children.map(c => (c.type === "LinkTarget" ? c.name : ""))
164
- : []
165
- )
166
- ).filter(t => t !== "");
221
+ elements.map((e) => (e.type === "Paragraph" ? e.children.map((c) => (c.type === "LinkTarget" ? c.name : "")) : []))
222
+ ).filter((t) => t !== "");
167
223
  const namedDestionations = [...sectionName, ...targetNames];
168
224
 
169
225
  // Ignore leading space by expanding the content rect upwards
170
226
  const rect = getPageContentRect(desiredSizes, section);
171
- const [leadingSpace] = getLeadingAndTrailingSpace(
172
- resources,
173
- section,
174
- elements
175
- );
176
- const contentRect = AD.Rect.create(
177
- rect.x,
178
- rect.y - leadingSpace,
179
- rect.width,
180
- rect.height + leadingSpace
181
- );
227
+ const [leadingSpace] = getLeadingAndTrailingSpace(resources, section, elements);
228
+ const contentRect = AD.Rect.create(rect.x, rect.y - leadingSpace, rect.width, rect.height + leadingSpace);
182
229
 
183
230
  return {
184
231
  pageNo: pageNo,
@@ -188,14 +235,11 @@ function createPage(
188
235
  contentRect: contentRect,
189
236
  elements: elements,
190
237
  header: section.page.header,
191
- footer: section.page.footer
238
+ footer: section.page.footer,
192
239
  };
193
240
  }
194
241
 
195
- function getPageContentRect(
196
- desiredSizes: Map<{}, AD.Size.Size>,
197
- section: AD.Section.Section
198
- ): AD.Rect.Rect {
242
+ function getPageContentRect(desiredSizes: Map<{}, AD.Size.Size>, section: AD.Section.Section): AD.Rect.Rect {
199
243
  const style = section.page.style;
200
244
  const pageWidth = AD.PageStyle.getWidth(style);
201
245
  const pageHeight = AD.PageStyle.getHeight(style);
@@ -218,14 +262,8 @@ function getPageContentRect(
218
262
 
219
263
  const rectX = style.contentMargins.left;
220
264
  const rectY = headerY + style.contentMargins.top;
221
- const rectWidth =
222
- pageWidth - (style.contentMargins.left + style.contentMargins.right);
223
- const rectHeight =
224
- pageHeight -
225
- headerHeight -
226
- footerHeight -
227
- style.contentMargins.top -
228
- style.contentMargins.bottom;
265
+ const rectWidth = pageWidth - (style.contentMargins.left + style.contentMargins.right);
266
+ const rectHeight = pageHeight - headerHeight - footerHeight - style.contentMargins.top - style.contentMargins.bottom;
229
267
 
230
268
  return AD.Rect.create(rectX, rectY, rectWidth, rectHeight);
231
269
  }
@@ -243,8 +281,7 @@ function getLeadingAndTrailingSpace(
243
281
 
244
282
  const last = elements.length > 0 ? elements[elements.length - 1] : undefined;
245
283
  const lastMargins = last && getSectionElementMargin(resources, last);
246
- const trailingSpace =
247
- lastMargins && noTopBottomMargin ? lastMargins.bottom : 0;
284
+ const trailingSpace = lastMargins && noTopBottomMargin ? lastMargins.bottom : 0;
248
285
 
249
286
  return [leadingSpace, trailingSpace];
250
287
  }
@@ -266,7 +303,7 @@ function getSectionElementMargin(
266
303
  bottom: 0,
267
304
  left: 0,
268
305
  right: 0,
269
- top: 0
306
+ top: 0,
270
307
  };
271
308
  }
272
309
  }
@@ -290,23 +327,20 @@ function getGroupMargins(
290
327
  group: AD.Group.Group
291
328
  ): AD.LayoutFoundation.LayoutFoundation {
292
329
  const first = group.children.length > 0 ? group.children[0] : undefined;
293
- const last =
294
- group.children.length > 0
295
- ? group.children[group.children.length - 1]
296
- : undefined;
330
+ const last = group.children.length > 0 ? group.children[group.children.length - 1] : undefined;
297
331
  const firstMargin = first && getSectionElementMargin(resources, first);
298
332
  const lastMargin = last && getSectionElementMargin(resources, last);
299
333
  if (firstMargin && lastMargin) {
300
334
  return {
301
335
  ...firstMargin,
302
- bottom: lastMargin.bottom
336
+ bottom: lastMargin.bottom,
303
337
  };
304
338
  } else {
305
339
  return {
306
340
  bottom: 0,
307
341
  left: 0,
308
342
  right: 0,
309
- top: 0
343
+ top: 0,
310
344
  };
311
345
  }
312
346
  }
@@ -325,13 +359,71 @@ function getTableMargins(
325
359
  return style.margins;
326
360
  }
327
361
 
328
- function getDesiredSize(
329
- element: {},
330
- desiredSizes: Map<{}, AD.Size.Size>
331
- ): AD.Size.Size {
362
+ function getDesiredSize(element: {}, desiredSizes: Map<{}, AD.Size.Size>): AD.Size.Size {
332
363
  const size = desiredSizes.get(element);
333
364
  if (size) {
334
365
  return size;
335
366
  }
336
367
  throw new Error("Could not find size for element!");
337
368
  }
369
+
370
+ function splitTableAt(
371
+ pdfKit: any,
372
+ document: AD.AbstractDoc.AbstractDoc,
373
+ resources: AD.Resources.Resources,
374
+ desiredSizes: Map<{}, AD.Size.Size>,
375
+ table: AD.Table.Table,
376
+ splitIndex: number
377
+ ): [AD.Table.Table, AD.Table.Table] {
378
+ //Push row/cells to head table while splitting rowspan
379
+ const headRows: AD.TableRow.TableRow[] = [];
380
+ for (const [rowIndex, row] of table.children.slice(0, splitIndex).entries()) {
381
+ const newRow: AD.TableCell.TableCell[] = [];
382
+ for (const cell of row.children) {
383
+ //If this cell would span over the split index
384
+ if (rowIndex + (cell.rowSpan || 1) - 1 >= splitIndex) {
385
+ newRow.push({ ...cell, rowSpan: splitIndex - rowIndex });
386
+ } else {
387
+ newRow.push(cell);
388
+ }
389
+ }
390
+ headRows.push({ ...row, children: newRow });
391
+ }
392
+
393
+ //Set dummy to false for all cells in the splitRow
394
+ //This causes them to be rendered like an empty cell
395
+ //while keeping the remaining rowspan which was calculated
396
+ //in the pre-process step
397
+ const splitRow = table.children[splitIndex];
398
+ const firstTailRow: AD.TableCell.TableCell[] = [];
399
+ for (const cell of splitRow.children) {
400
+ firstTailRow.push({ ...cell, dummy: false });
401
+ }
402
+
403
+ // Push the rest of the rows to tail table
404
+ const tailRows: AD.TableRow.TableRow[] = [{ ...splitRow, children: firstTailRow }];
405
+ tailRows.push(...table.children.slice(splitIndex + 1));
406
+
407
+ //Create tables and remeasure them
408
+ const tableHead = {
409
+ ...table,
410
+ style: { ...table.style, margins: { ...table.style.margins, bottom: 0 } },
411
+ children: headRows,
412
+ };
413
+ const tableTail = {
414
+ ...table,
415
+ style: { ...table.style, margins: { ...table.style.margins, top: 0 } },
416
+ children: tailRows,
417
+ };
418
+
419
+ const availableSize = getDesiredSize(table, desiredSizes);
420
+ let pdf = new pdfKit();
421
+ registerFonts((fontName: string, fontSource: AD.Font.FontSource) => pdf.registerFont(fontName, fontSource), document);
422
+ const headSizes = measureTable(pdf, resources, availableSize, tableHead);
423
+ const tailSizes = measureTable(pdf, resources, availableSize, tableTail);
424
+
425
+ headSizes.forEach((value, key) => desiredSizes.set(key, value));
426
+ tailSizes.forEach((value, key) => desiredSizes.set(key, value));
427
+
428
+ return [tableHead, tableTail];
429
+ }
@@ -29,45 +29,40 @@ const alphabet = [
29
29
  "w",
30
30
  "x",
31
31
  "y",
32
- "z"
32
+ "z",
33
33
  ];
34
34
 
35
35
  const _numberingLevelItems = new Map<string, number>();
36
36
 
37
- export function preProcess(
38
- doc: AD.AbstractDoc.AbstractDoc
39
- ): AD.AbstractDoc.AbstractDoc {
40
- const children = doc.children.map(s => preProcessSection(s, doc));
37
+ export function preProcess(doc: AD.AbstractDoc.AbstractDoc): AD.AbstractDoc.AbstractDoc {
38
+ const children = doc.children.map((s) => preProcessSection(s, doc));
41
39
  return AD.AbstractDoc.create(
42
40
  {
43
41
  fonts: doc.fonts,
44
42
  imageResources: doc.imageResources,
45
43
  styles: doc.styles,
46
44
  numberings: doc.numberings,
47
- numberingDefinitions: doc.numberingDefinitions
45
+ numberingDefinitions: doc.numberingDefinitions,
48
46
  },
49
47
  children
50
48
  );
51
49
  }
52
50
 
53
- function preProcessSection(
54
- s: AD.Section.Section,
55
- parentResources: AD.Resources.Resources
56
- ): AD.Section.Section {
51
+ function preProcessSection(s: AD.Section.Section, parentResources: AD.Resources.Resources): AD.Section.Section {
57
52
  const resources = AD.Resources.mergeResources([parentResources, s]);
58
53
  const header = R.unnest<AD.SectionElement.SectionElement>(
59
- s.page.header.map(e => preProcessSectionElement(e, resources))
54
+ s.page.header.map((e) => preProcessSectionElement(e, resources))
60
55
  );
61
56
  const footer = R.unnest<AD.SectionElement.SectionElement>(
62
- s.page.footer.map(e => preProcessSectionElement(e, resources))
57
+ s.page.footer.map((e) => preProcessSectionElement(e, resources))
63
58
  );
64
59
  const page = AD.MasterPage.create({
65
60
  style: s.page.style,
66
61
  header: header,
67
- footer: footer
62
+ footer: footer,
68
63
  });
69
64
  const children = R.unnest<AD.SectionElement.SectionElement>(
70
- s.children.map(e => preProcessSectionElement(e, resources))
65
+ s.children.map((e) => preProcessSectionElement(e, resources))
71
66
  );
72
67
  return AD.Section.create({ page: page, id: s.id }, children);
73
68
  }
@@ -105,10 +100,8 @@ function preProcessParagraph(
105
100
  const level = paragraph.numbering.level;
106
101
  const key = numbering + "_" + level.toString();
107
102
  const levelDefinitions = resources.numberingDefinitions[numbering].levels;
108
- for (let levelDefinition of levelDefinitions.filter(l => l.level > level)) {
109
- _numberingLevelItems.delete(
110
- numbering + "_" + levelDefinition.level.toString()
111
- );
103
+ for (let levelDefinition of levelDefinitions.filter((l) => l.level > level)) {
104
+ _numberingLevelItems.delete(numbering + "_" + levelDefinition.level.toString());
112
105
  }
113
106
 
114
107
  const numberOverride = paragraph.numbering.numberOverride;
@@ -122,7 +115,7 @@ function preProcessParagraph(
122
115
  _numberingLevelItems.set(key, (_numberingLevelItems.get(key) || 0) + 1);
123
116
  }
124
117
 
125
- for (let levelDefinition of levelDefinitions.filter(l => l.level <= level)) {
118
+ for (let levelDefinition of levelDefinitions.filter((l) => l.level <= level)) {
126
119
  const numberingLevel = numbering + "_" + levelDefinition.level.toString();
127
120
  const currentNumber = _numberingLevelItems.get(numberingLevel) || 0;
128
121
  const levelText = generateLevelText(levelDefinition.format, currentNumber);
@@ -135,24 +128,21 @@ function preProcessParagraph(
135
128
 
136
129
  children.push(AD.TableCell.create());
137
130
 
138
- const numberTextStyle = TextStyle.overrideWith(
139
- levelDefinitions[level].style,
140
- paragraph.style.textStyle
141
- );
131
+ const numberTextStyle = TextStyle.overrideWith(levelDefinitions[level].style, paragraph.style.textStyle);
142
132
 
143
133
  children.push(
144
134
  AD.TableCell.create({}, [
145
135
  AD.Paragraph.create(
146
136
  {
147
- styleName: paragraph.styleName
137
+ styleName: paragraph.styleName,
148
138
  },
149
139
  [
150
140
  AD.TextRun.create({
151
141
  style: numberTextStyle,
152
- text: paragraph.numbering.append !== true ? numberText : ""
153
- })
142
+ text: paragraph.numbering.append !== true ? numberText : "",
143
+ }),
154
144
  ]
155
- )
145
+ ),
156
146
  ])
157
147
  );
158
148
 
@@ -166,22 +156,16 @@ function preProcessParagraph(
166
156
  {
167
157
  style: AD.TableStyle.create({
168
158
  alignment: "Left",
169
- cellStyle: AD.TableCellStyle.create({ verticalAlignment: "Top" })
159
+ cellStyle: AD.TableCellStyle.create({ verticalAlignment: "Top" }),
170
160
  }),
171
- columnWidths: [
172
- indentationWidth - numberingWidth,
173
- numberingWidth,
174
- Infinity
175
- ]
161
+ columnWidths: [indentationWidth - numberingWidth, numberingWidth, Infinity],
176
162
  },
177
163
  rows
178
- )
164
+ ),
179
165
  ];
180
166
  }
181
167
 
182
- function adjustParagraph(
183
- paragraph: AD.Paragraph.Paragraph
184
- ): Array<AD.SectionElement.SectionElement> {
168
+ function adjustParagraph(paragraph: AD.Paragraph.Paragraph): Array<AD.SectionElement.SectionElement> {
185
169
  return [paragraph];
186
170
  // let adjustedParagraphs: Array<AD.SectionElement.SectionElement> = [];
187
171
  // paragraph.children.forEach((a) => {
@@ -217,10 +201,7 @@ function adjustParagraph(
217
201
 
218
202
  // return adjustedParagraphs;
219
203
  }
220
- function generateLevelText(
221
- numberingFormat: AD.NumberingFormat.NumberingFormat,
222
- num: number
223
- ): string {
204
+ function generateLevelText(numberingFormat: AD.NumberingFormat.NumberingFormat, num: number): string {
224
205
  switch (numberingFormat) {
225
206
  case "Decimal":
226
207
  return num.toString();
@@ -301,41 +282,77 @@ function toChar(num: number): string {
301
282
  // });
302
283
  // }
303
284
 
304
- function preProcessTable(
305
- table: AD.Table.Table,
306
- resources: AD.Resources.Resources
307
- ): AD.SectionElement.SectionElement {
308
- const children = table.children.map(r => preProcessTableRow(r, resources));
285
+ function preProcessTable(table: AD.Table.Table, resources: AD.Resources.Resources): AD.SectionElement.SectionElement {
286
+ const processedChildren = [];
287
+ let rowSpans: Map<number, AD.TableCell.TableCell> = new Map();
288
+ for (const row of table.children) {
289
+ const children = preProcessTableRow(row, rowSpans, resources);
290
+ processedChildren.push(children);
291
+ }
309
292
  return AD.Table.create(
310
293
  {
311
294
  columnWidths: table.columnWidths,
312
295
  styleName: table.styleName,
313
- style: table.style
296
+ style: table.style,
314
297
  },
315
- children
298
+ processedChildren
316
299
  );
317
300
  }
318
301
 
319
302
  function preProcessTableRow(
320
303
  r: AD.TableRow.TableRow,
304
+ rowSpans: Map<number, AD.TableCell.TableCell>,
321
305
  resources: AD.Resources.Resources
322
306
  ): AD.TableRow.TableRow {
323
- const children = r.children.map(c => preProcessTableCell(c, resources));
324
- return AD.TableRow.create({}, children);
307
+ const processedChildren = [];
308
+ let columnIndex = 0;
309
+ for (const cell of r.children) {
310
+ let dummyCell = rowSpans.get(columnIndex);
311
+ while (dummyCell) {
312
+ processedChildren.push(dummyCell);
313
+ if (dummyCell.rowSpan <= 1) {
314
+ rowSpans.delete(columnIndex);
315
+ } else {
316
+ rowSpans.set(columnIndex, { ...dummyCell, rowSpan: dummyCell.rowSpan - 1 });
317
+ }
318
+ columnIndex += dummyCell.columnSpan || 1;
319
+ dummyCell = rowSpans.get(columnIndex);
320
+ }
321
+
322
+ const processedCell = preProcessTableCell(cell, resources);
323
+ processedChildren.push(processedCell);
324
+ if ((processedCell.rowSpan || 1) > 1) {
325
+ rowSpans.set(columnIndex, { ...processedCell, children: [], rowSpan: processedCell.rowSpan - 1, dummy: true });
326
+ }
327
+ columnIndex += cell.columnSpan || 1;
328
+ }
329
+
330
+ // Make sure to reduce the rowSpanLeft of any cell that is at the end
331
+ let dummyCell = rowSpans.get(columnIndex);
332
+ while (dummyCell) {
333
+ processedChildren.push(dummyCell);
334
+ if (dummyCell.rowSpan <= 1) {
335
+ rowSpans.delete(columnIndex);
336
+ } else {
337
+ rowSpans.set(columnIndex, { ...dummyCell, rowSpan: dummyCell.rowSpan - 1 });
338
+ }
339
+ columnIndex += dummyCell.columnSpan || 1;
340
+ dummyCell = rowSpans.get(columnIndex);
341
+ }
342
+
343
+ return AD.TableRow.create({}, processedChildren);
325
344
  }
326
345
 
327
- function preProcessTableCell(
328
- c: AD.TableCell.TableCell,
329
- resources: AD.Resources.Resources
330
- ): AD.TableCell.TableCell {
346
+ function preProcessTableCell(c: AD.TableCell.TableCell, resources: AD.Resources.Resources): AD.TableCell.TableCell {
331
347
  const children = R.unnest<AD.SectionElement.SectionElement>(
332
- c.children.map(e => preProcessSectionElement(e, resources))
348
+ c.children.map((e) => preProcessSectionElement(e, resources))
333
349
  );
334
350
  return AD.TableCell.create(
335
351
  {
336
352
  styleName: c.styleName,
337
353
  columnSpan: c.columnSpan,
338
- style: c.style
354
+ rowSpan: c.rowSpan,
355
+ style: c.style,
339
356
  },
340
357
  children
341
358
  );
@@ -346,17 +363,17 @@ function preProcessGroup(
346
363
  parentResources: AD.Resources.Resources
347
364
  ): Array<AD.SectionElement.SectionElement> {
348
365
  const children = R.unnest<AD.SectionElement.SectionElement>(
349
- group.children.map(e => preProcessSectionElement(e, parentResources))
366
+ group.children.map((e) => preProcessSectionElement(e, parentResources))
350
367
  );
351
368
  if (group.keepTogether || AD.Resources.hasResources(group)) {
352
369
  return [
353
370
  AD.Group.create(
354
371
  {
355
372
  keepTogether: group.keepTogether,
356
- ...AD.Resources.extractResources(group)
373
+ ...AD.Resources.extractResources(group),
357
374
  },
358
375
  children
359
- )
376
+ ),
360
377
  ];
361
378
  }
362
379
  return children;