Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] New section node #1972

Draft
wants to merge 48 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
2d44e3b
start of implementing node section
liborm85 Mar 23, 2020
0e784b0
fixed wrapping section to new page
liborm85 Mar 23, 2020
4c60f75
refactor normalize page size and page margin
liborm85 Apr 26, 2020
551f518
support for different page sizes in sections
liborm85 Apr 28, 2020
dd901dd
add simple example
liborm85 Apr 29, 2020
4af6633
support pageMargins
liborm85 Apr 29, 2020
eae343b
fix set section page margin
liborm85 Apr 29, 2020
46ce02d
fix loading default pagesize and pagemargins
liborm85 Jun 14, 2020
b03372b
Merge branch 'master' into features/node-section
liborm85 Jul 10, 2020
bb89d18
fix formating
liborm85 Jul 10, 2020
a1d50cf
Merge branch 'master' into features/node-section
liborm85 Aug 23, 2020
5b728c3
update example by new interface
liborm85 Aug 23, 2020
ba935c2
Merge branch 'master' into features/node-section
liborm85 Dec 28, 2020
9ca2323
Merge branch 'master' into features/node-section
liborm85 Jul 6, 2021
d9bade3
Merge branch 'master' into features/node-section
liborm85 Nov 6, 2021
970773f
Merge branch 'master' into features/node-section
liborm85 Jan 1, 2022
122fb03
Merge branch 'master' into features/node-section
liborm85 Jan 8, 2022
befd38a
fix example
liborm85 Jan 8, 2022
dc76a77
fix header/footer in sections with pagemargins
liborm85 Jan 8, 2022
f00a33f
fix render wrong first page size
liborm85 Jan 9, 2022
a516fb9
fix tests
liborm85 Jan 9, 2022
04e942c
add docs
liborm85 Jan 15, 2022
ff321c7
fix
liborm85 Jan 15, 2022
fcb1001
fix inserting section first page
liborm85 Jan 29, 2022
19e5d1e
fix test first page initialization
liborm85 Jan 29, 2022
f694c35
remove dead code
liborm85 Jan 29, 2022
993daa7
refactoring header/footer conversion
liborm85 Jan 30, 2022
5680673
Merge branch 'master' into features/node-section
liborm85 Apr 2, 2022
7609b92
Merge branch 'master' into features/node-section
liborm85 Dec 18, 2022
656d24c
Merge branch 'master' into features/node-section
liborm85 Dec 21, 2023
814c032
Merge branch 'master' into features/node-section
liborm85 Jan 6, 2024
7c8d56d
Merge branch 'master' into features/node-section
liborm85 Nov 3, 2024
18b9c4f
Merge branch 'master' into features/node-section
liborm85 Nov 3, 2024
d10de3e
Merge branch 'master' into features/node-section
liborm85 Nov 3, 2024
b960a5d
Merge branch 'master' into features/node-section
liborm85 Nov 3, 2024
1cec32b
Merge branch 'master' into features/node-section
liborm85 Nov 3, 2024
d9afdc9
support inherit page definition
liborm85 Nov 3, 2024
53e59a3
Merge branch 'master' into features/node-section
liborm85 Nov 8, 2024
65eddf6
custom header/footer support
liborm85 Nov 8, 2024
12e8ae5
section node not support style overrides
liborm85 Nov 10, 2024
d7c3c37
custom background support
liborm85 Nov 10, 2024
8acc262
TODO
liborm85 Nov 10, 2024
05e7ccc
Merge branch 'master' into features/node-section
liborm85 Nov 23, 2024
fd9e562
fix build folder
liborm85 Nov 23, 2024
ece4927
custom watermark support
liborm85 Nov 24, 2024
3ed1342
section is allowed only in document root
liborm85 Dec 1, 2024
ebf51db
fix format
liborm85 Dec 6, 2024
db47d61
Merge branch 'master' into features/node-section
liborm85 Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/pdfs/sections.pdf
Binary file not shown.
90 changes: 90 additions & 0 deletions examples/sections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var pdfmake = require('../js/index'); // only during development, otherwise use the following line
//var pdfmake = require('pdfmake');

var Roboto = require('../fonts/Roboto');
pdfmake.addFonts(Roboto);

var docDefinition = {
header: function () { return 'default header'; },
footer: function () { return 'default footer'; },
background: function() { return { text:'global background', alignment: 'right' }; },
watermark: 'default watermark',
content: [
{
section: [
'SECTION 1',
'Text in section.'
]
},
{
header: function (currentPage, pageCount) { return 'header: ' + currentPage.toString() + ' of ' + pageCount; },
footer: function (currentPage, pageCount) { return 'footer: ' + currentPage.toString() + ' of ' + pageCount; },
background: function() { return { text:'SECTION 2 background', alignment: 'right' }; },
watermark: 'SECTION 2 watermark',
pageOrientation: 'landscape',
section: [
'SECTION 2',
'Text in section as landscape page.'
]
},
{
header: null,
footer: null,
background: null,
watermark: null,
pageSize: 'A7',
pageOrientation: 'portrait',
section: [
'SECTION 3',
'Text in section as A7 page.'
]
},
{
watermark: 'inherit',
pageSize: 'A6',
pageOrientation: 'portrait',
pageMargins: 5,
section: [
'SECTION 4',
'Text in section as A6 page with margin.'
]
},
{
watermark: 'watermark for inherit',
pageSize: 'A6',
pageOrientation: 'landscape',
pageMargins: 10,
section: [
'SECTION 5',
'Text in section as A6 landscape page with margin.'
]
},
{
watermark: 'inherit',
pageSize: 'inherit',
pageOrientation: 'inherit',
pageMargins: 'inherit',
section: [
'SECTION 6',
'Text in section with page definition as previous page. Page size, orientation and margins are inherited.'
]
},
{
header: function (currentPage, pageCount) { return 'header in section 8: ' + currentPage.toString() + ' of ' + pageCount; },
footer: function (currentPage, pageCount) { return 'footer in section 8: ' + currentPage.toString() + ' of ' + pageCount; },
section: [
'SECTION 7',
'Text in section with page definition as defined in document.'
]
}
]
};

var now = new Date();

var pdf = pdfmake.createPdf(docDefinition);
pdf.write('pdfs/sections.pdf').then(() => {
console.log(new Date() - now);
}, err => {
console.error(err);
});
16 changes: 15 additions & 1 deletion src/DocMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ class DocMeasure {
return this.measureNode(docStructure);
}

measureBlock(node) {
return this.measureNode(node);
}

measureNode(node) {
return this.styleStack.auto(node, () => {
// TODO: refactor + rethink whether this is the proper way to handle margins
node._margin = getNodeMargin(node, this.styleStack);

if (node.columns) {
if (node.section) {
return extendMargins(this.measureSection(node));
} else if (node.columns) {
return extendMargins(this.measureColumns(node));
} else if (node.stack) {
return extendMargins(this.measureVerticalContainer(node));
Expand Down Expand Up @@ -465,6 +471,14 @@ class DocMeasure {
return node;
}

measureSection(node) {
// TODO: properties

node.section = this.measureNode(node.section);

return node;
}

measureColumns(node) {
let columns = node.columns;
node._gap = this.styleStack.getProperty('columnGap') || 0;
Expand Down
31 changes: 25 additions & 6 deletions src/DocPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ class DocPreprocessor {
this.parentNode = null;
this.tocs = [];
this.nodeReferences = [];
return this.preprocessNode(docStructure);
return this.preprocessNode(docStructure, true);
}

preprocessNode(node) {
preprocessBlock(node) {
this.parentNode = null;
this.tocs = [];
this.nodeReferences = [];
return this.preprocessNode(node);
}

preprocessNode(node, isSectionAllowed = false) {
// expand shortcuts and casting values
if (Array.isArray(node)) {
node = { stack: node };
Expand All @@ -33,10 +40,16 @@ class DocPreprocessor {
node.text = convertValueToString(node.text);
}

if (node.columns) {
if (node.section) {
if (!isSectionAllowed) {
throw new Error(`Incorrect document structure, section node is only allowed at the root level of document structure: ${stringifyNode(node)}`);
}

return this.preprocessSection(node);
} else if (node.columns) {
return this.preprocessColumns(node);
} else if (node.stack) {
return this.preprocessVerticalContainer(node);
return this.preprocessVerticalContainer(node, isSectionAllowed);
} else if (node.ul) {
return this.preprocessList(node);
} else if (node.ol) {
Expand Down Expand Up @@ -64,6 +77,12 @@ class DocPreprocessor {
}
}

preprocessSection(node) {
node.section = this.preprocessNode(node.section);

return node;
}

preprocessColumns(node) {
let columns = node.columns;

Expand All @@ -74,11 +93,11 @@ class DocPreprocessor {
return node;
}

preprocessVerticalContainer(node) {
preprocessVerticalContainer(node, isSectionAllowed) {
let items = node.stack;

for (let i = 0, l = items.length; i < l; i++) {
items[i] = this.preprocessNode(items[i]);
items[i] = this.preprocessNode(items[i], isSectionAllowed);
}

return node;
Expand Down
8 changes: 4 additions & 4 deletions src/DocumentContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class DocumentContext extends EventEmitter {
let currentPageOrientation = this.getCurrentPage().pageSize.orientation;

let pageSize = getPageSize(this.getCurrentPage(), pageOrientation);
this.addPage(pageSize);
this.addPage(pageSize, null, this.getCurrentPage().customProperties);

if (currentPageOrientation === pageSize.orientation) {
this.availableWidth = currentAvailableWidth;
Expand All @@ -240,20 +240,20 @@ class DocumentContext extends EventEmitter {
};
}

addPage(pageSize, pageMargin = null) {
addPage(pageSize, pageMargin = null, customProperties = {}) {
if (pageMargin !== null) {
this.pageMargins = pageMargin;
this.x = pageMargin.left;
this.availableWidth = pageSize.width - pageMargin.left - pageMargin.right;
}

let page = { items: [], pageSize: pageSize, pageMargins: this.pageMargins };
let page = { items: [], pageSize: pageSize, pageMargins: this.pageMargins, customProperties: customProperties };
this.pages.push(page);
this.backgroundLength.push(0);
this.page = this.pages.length - 1;
this.initializePage();

this.emit('pageAdded');
this.emit('pageAdded', page);

return page;
}
Expand Down
Loading
Loading