From 0fb2aedf95bd0ace590b071f3eb299bf7fccfbc6 Mon Sep 17 00:00:00 2001 From: seahorsepip Date: Sat, 15 Jun 2019 13:23:58 +0200 Subject: [PATCH 01/14] Implemented dynamic page margins. --- src/documentContext.js | 37 +++++++++++++++++++++++++++---------- src/layoutBuilder.js | 2 +- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/documentContext.js b/src/documentContext.js index 34d3e5501..58155843d 100644 --- a/src/documentContext.js +++ b/src/documentContext.js @@ -12,7 +12,12 @@ function DocumentContext(pageSize, pageMargins) { this.pageMargins = pageMargins; - this.x = pageMargins.left; + if (typeof pageMargins === 'function') { + this.pageMarginsFn = pageMargins; + this.pageMargins = pageMargins(1) + } + + this.x = pageMargins.left; this.availableWidth = pageSize.width - pageMargins.left - pageMargins.right; this.availableHeight = 0; this.page = -1; @@ -135,9 +140,17 @@ DocumentContext.prototype.moveDown = function (offset) { }; DocumentContext.prototype.initializePage = function () { - this.y = this.pageMargins.top; - this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom; - this.pageSnapshot().availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right; + this.y = this.getCurrentPage().pageMargins.top; + + this.availableHeight = + this.getCurrentPage().pageSize.height - + this.getCurrentPage().pageMargins.top - + this.getCurrentPage().pageMargins.bottom; + + this.pageSnapshot().availableWidth = + this.getCurrentPage().pageSize.width - + this.getCurrentPage().pageMargins.left - + this.getCurrentPage().pageMargins.right; }; DocumentContext.prototype.pageSnapshot = function () { @@ -253,13 +266,16 @@ DocumentContext.prototype.moveToNextPage = function (pageOrientation) { }; }; - DocumentContext.prototype.addPage = function (pageSize) { var page = {items: [], pageSize: pageSize}; + + if (this.pageMarginsFn) page.pageMargins = this.pageMarginsFn(this.pages.length); + this.pages.push(page); this.backgroundLength.push(0); this.page = this.pages.length - 1; - this.initializePage(); + + this.initializePage(); this.tracker.emit('pageAdded'); @@ -276,18 +292,19 @@ DocumentContext.prototype.getCurrentPage = function () { DocumentContext.prototype.getCurrentPosition = function () { var pageSize = this.getCurrentPage().pageSize; - var innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom; - var innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right; + var innerHeight = pageSize.height - this.getCurrentPage().pageMargins.top - this.getCurrentPage().pageMargins.bottom; + var innerWidth = pageSize.width - this.getCurrentPage().pageMargins.left - this.getCurrentPage().pageMargins.right; return { + pageMargins: this.getCurrentPage().pageMargins, pageNumber: this.page + 1, pageOrientation: pageSize.orientation, pageInnerHeight: innerHeight, pageInnerWidth: innerWidth, left: this.x, top: this.y, - verticalRatio: ((this.y - this.pageMargins.top) / innerHeight), - horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth) + verticalRatio: ((this.y - this.getCurrentPage().pageMargins.top) / innerHeight), + horizontalRatio: ((this.x - this.getCurrentPage().pageMargins.left) / innerWidth) }; }; diff --git a/src/layoutBuilder.js b/src/layoutBuilder.js index e0bd9f2bd..0c12dd23d 100644 --- a/src/layoutBuilder.js +++ b/src/layoutBuilder.js @@ -202,7 +202,7 @@ LayoutBuilder.prototype.addDynamicRepeatable = function (nodeGetter, sizeFunctio var node = nodeGetter(pageIndex + 1, l, this.writer.context().pages[pageIndex].pageSize); if (node) { - var sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.pageMargins); + var sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.getCurrentPage().pageMargins); this.writer.beginUnbreakableBlock(sizes.width, sizes.height); node = this.docPreprocessor.preprocessDocument(node); this.processNode(this.docMeasure.measureDocument(node)); From e9e2e73219a79a25f844f63a9cfd931255c65705 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 13:35:08 +0200 Subject: [PATCH 02/14] Update documentContext.js Tab indents instead of spaces (using indents based on source) --- src/documentContext.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/documentContext.js b/src/documentContext.js index 58155843d..1e987cf34 100644 --- a/src/documentContext.js +++ b/src/documentContext.js @@ -12,12 +12,12 @@ function DocumentContext(pageSize, pageMargins) { this.pageMargins = pageMargins; - if (typeof pageMargins === 'function') { - this.pageMarginsFn = pageMargins; - this.pageMargins = pageMargins(1) - } + if (typeof pageMargins === 'function') { + this.pageMarginsFn = pageMargins; + this.pageMargins = pageMargins(1) + } - this.x = pageMargins.left; + this.x = pageMargins.left; this.availableWidth = pageSize.width - pageMargins.left - pageMargins.right; this.availableHeight = 0; this.page = -1; @@ -269,7 +269,7 @@ DocumentContext.prototype.moveToNextPage = function (pageOrientation) { DocumentContext.prototype.addPage = function (pageSize) { var page = {items: [], pageSize: pageSize}; - if (this.pageMarginsFn) page.pageMargins = this.pageMarginsFn(this.pages.length); + if (this.pageMarginsFn) page.pageMargins = this.pageMarginsFn(this.pages.length); this.pages.push(page); this.backgroundLength.push(0); From 9186f7abe0ec696dac0f1db5fd6e22f91736b488 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 13:43:48 +0200 Subject: [PATCH 03/14] Update documentContext.js --- src/documentContext.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/documentContext.js b/src/documentContext.js index 1e987cf34..9de5be01f 100644 --- a/src/documentContext.js +++ b/src/documentContext.js @@ -10,12 +10,7 @@ var isString = require('./helpers').isString; function DocumentContext(pageSize, pageMargins) { this.pages = []; - this.pageMargins = pageMargins; - - if (typeof pageMargins === 'function') { - this.pageMarginsFn = pageMargins; - this.pageMargins = pageMargins(1) - } + this.pageMargins = typeof pageMargins === 'function' ? pageMargins : function () {return pageMargins}; this.x = pageMargins.left; this.availableWidth = pageSize.width - pageMargins.left - pageMargins.right; @@ -267,10 +262,11 @@ DocumentContext.prototype.moveToNextPage = function (pageOrientation) { }; DocumentContext.prototype.addPage = function (pageSize) { - var page = {items: [], pageSize: pageSize}; - - if (this.pageMarginsFn) page.pageMargins = this.pageMarginsFn(this.pages.length); - + var page = { + items: [], + pageSize: pageSize, + pageMargins: this.pageMargins(this.pages.length) + }; this.pages.push(page); this.backgroundLength.push(0); this.page = this.pages.length - 1; @@ -296,7 +292,7 @@ DocumentContext.prototype.getCurrentPosition = function () { var innerWidth = pageSize.width - this.getCurrentPage().pageMargins.left - this.getCurrentPage().pageMargins.right; return { - pageMargins: this.getCurrentPage().pageMargins, + pageMargins: this.getCurrentPage().pageMargins, pageNumber: this.page + 1, pageOrientation: pageSize.orientation, pageInnerHeight: innerHeight, From e2fea36771bd5b873c1d502df1ac92e3514a02a4 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 13:49:35 +0200 Subject: [PATCH 04/14] Update layoutBuilder.js --- src/layoutBuilder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/layoutBuilder.js b/src/layoutBuilder.js index 0c12dd23d..64f06932b 100644 --- a/src/layoutBuilder.js +++ b/src/layoutBuilder.js @@ -202,7 +202,7 @@ LayoutBuilder.prototype.addDynamicRepeatable = function (nodeGetter, sizeFunctio var node = nodeGetter(pageIndex + 1, l, this.writer.context().pages[pageIndex].pageSize); if (node) { - var sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.getCurrentPage().pageMargins); + var sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.writer.context().getCurrentPage().pageMargins); this.writer.beginUnbreakableBlock(sizes.width, sizes.height); node = this.docPreprocessor.preprocessDocument(node); this.processNode(this.docMeasure.measureDocument(node)); From 49e5a39d70b6d2a92ea7dcec914b9aec2bf4dab0 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 14:05:35 +0200 Subject: [PATCH 05/14] Update printer.js --- src/printer.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/printer.js b/src/printer.js index f1e2af22b..98f20b080 100644 --- a/src/printer.js +++ b/src/printer.js @@ -231,6 +231,16 @@ function fixPageSize(pageSize, pageOrientation) { return size; } +function pageMarginsFn(margin) { + if(!isFunction(margin)) margin = function () { + return margin; + }; + + return function (currentPage) { + return fixPageMargins(margin(currentPage)) + } +} + function fixPageMargins(margin) { if (!margin) { return null; From 66568cf1ff5f247665775a21d2a219d6e4db4cb6 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 14:08:09 +0200 Subject: [PATCH 06/14] Update printer.js --- src/printer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/printer.js b/src/printer.js index 98f20b080..ee050f923 100644 --- a/src/printer.js +++ b/src/printer.js @@ -109,7 +109,7 @@ PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) { this.fontProvider = new FontProvider(this.fontDescriptors, this.pdfKitDoc); - var builder = new LayoutBuilder(pageSize, fixPageMargins(docDefinition.pageMargins || 40), new ImageMeasure(this.pdfKitDoc, docDefinition.images)); + var builder = new LayoutBuilder(pageSize, pageMarginsFn(docDefinition.pageMargins, 40), new ImageMeasure(this.pdfKitDoc, docDefinition.images)); registerDefaultTableLayouts(builder); if (options.tableLayouts) { @@ -231,13 +231,13 @@ function fixPageSize(pageSize, pageOrientation) { return size; } -function pageMarginsFn(margin) { +function pageMarginsFn(margin, def) { if(!isFunction(margin)) margin = function () { return margin; }; return function (currentPage) { - return fixPageMargins(margin(currentPage)) + return fixPageMargins(margin(currentPage)) || def; } } From c2900f307999a4be150ec54e972cfbbc6a1dae7c Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 14:12:02 +0200 Subject: [PATCH 07/14] Update documentContext.js --- src/documentContext.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/documentContext.js b/src/documentContext.js index 9de5be01f..38fb04f2d 100644 --- a/src/documentContext.js +++ b/src/documentContext.js @@ -10,10 +10,10 @@ var isString = require('./helpers').isString; function DocumentContext(pageSize, pageMargins) { this.pages = []; - this.pageMargins = typeof pageMargins === 'function' ? pageMargins : function () {return pageMargins}; + this.pageMargins = pageMargins; - this.x = pageMargins.left; - this.availableWidth = pageSize.width - pageMargins.left - pageMargins.right; + this.x = pageMargins(1).left; + this.availableWidth = pageSize.width - pageMargins(1).left - pageMargins(1).right; this.availableHeight = 0; this.page = -1; @@ -271,7 +271,7 @@ DocumentContext.prototype.addPage = function (pageSize) { this.backgroundLength.push(0); this.page = this.pages.length - 1; - this.initializePage(); + this.initializePage(); this.tracker.emit('pageAdded'); From 63497fa8782470c93487f394d14eab94e1d6c8f5 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 14:30:29 +0200 Subject: [PATCH 08/14] Update documentContext.js --- src/documentContext.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/documentContext.js b/src/documentContext.js index 38fb04f2d..0294500b6 100644 --- a/src/documentContext.js +++ b/src/documentContext.js @@ -159,11 +159,11 @@ DocumentContext.prototype.pageSnapshot = function () { DocumentContext.prototype.moveTo = function (x, y) { if (x !== undefined && x !== null) { this.x = x; - this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.pageMargins.right; + this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.getCurrentPage().pageMargins.right; } if (y !== undefined && y !== null) { this.y = y; - this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.pageMargins.bottom; + this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.getCurrentPage().pageMargins.bottom; } }; From 1cebb9bb6e1dae0f373db62811f24dddb1a2b7fd Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 14:36:10 +0200 Subject: [PATCH 09/14] Update elementWriter.js --- src/elementWriter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/elementWriter.js b/src/elementWriter.js index 2177b73b9..9e55f9464 100644 --- a/src/elementWriter.js +++ b/src/elementWriter.js @@ -298,7 +298,7 @@ ElementWriter.prototype.pushContext = function (contextOrWidth, height) { } if (isNumber(contextOrWidth)) { - contextOrWidth = new DocumentContext({width: contextOrWidth, height: height}, {left: 0, right: 0, top: 0, bottom: 0}); + contextOrWidth = new DocumentContext({width: contextOrWidth, height: height}, function () {return {left: 0, right: 0, top: 0, bottom: 0}}); } this.contextStack.push(this.context); From ea578f1c7c2d93619e67f5f7d92f9c59f97cf7c9 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 14:37:37 +0200 Subject: [PATCH 10/14] Update printer.js --- src/printer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index ee050f923..291eb6e04 100644 --- a/src/printer.js +++ b/src/printer.js @@ -237,7 +237,7 @@ function pageMarginsFn(margin, def) { }; return function (currentPage) { - return fixPageMargins(margin(currentPage)) || def; + return fixPageMargins(margin(currentPage) || def); } } From 1db8785196b6acbd3205fe6afeffe000ba6725e2 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 14:46:50 +0200 Subject: [PATCH 11/14] Update printer.js --- src/printer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index 291eb6e04..f2e182e19 100644 --- a/src/printer.js +++ b/src/printer.js @@ -232,7 +232,8 @@ function fixPageSize(pageSize, pageOrientation) { } function pageMarginsFn(margin, def) { - if(!isFunction(margin)) margin = function () { + var marginFn = margin; + if(!isFunction(margin)) marginFn = function () { return margin; }; From 7251a51459c02abfd6ee3776673791bd1333ac1a Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 14:48:52 +0200 Subject: [PATCH 12/14] Update printer.js --- src/printer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index f2e182e19..f63ef1bf9 100644 --- a/src/printer.js +++ b/src/printer.js @@ -238,7 +238,7 @@ function pageMarginsFn(margin, def) { }; return function (currentPage) { - return fixPageMargins(margin(currentPage) || def); + return fixPageMargins(marginFn(currentPage) || def); } } From 0eb872b136c32cb0a5dfe31db89b1977a203be60 Mon Sep 17 00:00:00 2001 From: Thomas Gladdines Date: Sat, 15 Jun 2019 15:08:52 +0200 Subject: [PATCH 13/14] Update documentContext.js --- src/documentContext.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/documentContext.js b/src/documentContext.js index 0294500b6..3afbe3725 100644 --- a/src/documentContext.js +++ b/src/documentContext.js @@ -265,7 +265,7 @@ DocumentContext.prototype.addPage = function (pageSize) { var page = { items: [], pageSize: pageSize, - pageMargins: this.pageMargins(this.pages.length) + pageMargins: this.pageMargins(this.pages.length + 1) }; this.pages.push(page); this.backgroundLength.push(0); From a7fbab366f23b921f7f7d0d9133ef3b0b1d0331d Mon Sep 17 00:00:00 2001 From: Libor M Date: Mon, 28 Dec 2020 16:21:41 +0100 Subject: [PATCH 14/14] fix merge --- src/printer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/printer.js b/src/printer.js index 182b05b6b..7210ccc8e 100644 --- a/src/printer.js +++ b/src/printer.js @@ -134,7 +134,7 @@ PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) { this.fontProvider = new FontProvider(this.fontDescriptors, this.pdfKitDoc); - var builder = new LayoutBuilder(pageSize, fixPageMargins(docDefinition.pageMargins), new ImageMeasure(this.pdfKitDoc, docDefinition.images), new SVGMeasure()); + var builder = new LayoutBuilder(pageSize, pageMarginsFn(docDefinition.pageMargins), new ImageMeasure(this.pdfKitDoc, docDefinition.images), new SVGMeasure()); registerDefaultTableLayouts(builder); if (options.tableLayouts) { @@ -266,7 +266,7 @@ function pageMarginsFn(margin, def) { if(!isFunction(margin)) marginFn = function () { return margin; }; - + return function (currentPage) { return fixPageMargins(marginFn(currentPage) || def); }