diff --git a/src/documentContext.js b/src/documentContext.js index 9fcca4f4d..3afbe3725 100644 --- a/src/documentContext.js +++ b/src/documentContext.js @@ -12,8 +12,8 @@ function DocumentContext(pageSize, 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; @@ -135,9 +135,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 () { @@ -151,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; } }; @@ -253,12 +261,16 @@ DocumentContext.prototype.moveToNextPage = function (pageOrientation) { }; }; - DocumentContext.prototype.addPage = function (pageSize) { - var page = { items: [], pageSize: pageSize }; + var page = { + items: [], + pageSize: pageSize, + pageMargins: this.pageMargins(this.pages.length + 1) + }; this.pages.push(page); this.backgroundLength.push(0); this.page = this.pages.length - 1; + this.initializePage(); this.tracker.emit('pageAdded'); @@ -276,18 +288,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/elementWriter.js b/src/elementWriter.js index 9b176bcf1..b23dac6f7 100644 --- a/src/elementWriter.js +++ b/src/elementWriter.js @@ -303,7 +303,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); diff --git a/src/layoutBuilder.js b/src/layoutBuilder.js index 5f5428ed8..ee18a53dc 100644 --- a/src/layoutBuilder.js +++ b/src/layoutBuilder.js @@ -201,7 +201,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.writer.context().getCurrentPage().pageMargins); this.writer.beginUnbreakableBlock(sizes.width, sizes.height); node = this.docPreprocessor.preprocessDocument(node); this.processNode(this.docMeasure.measureDocument(node)); diff --git a/src/printer.js b/src/printer.js index 04a7ba235..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) { @@ -261,6 +261,17 @@ function fixPageSize(pageSize, pageOrientation) { return size; } +function pageMarginsFn(margin, def) { + var marginFn = margin; + if(!isFunction(margin)) marginFn = function () { + return margin; + }; + + return function (currentPage) { + return fixPageMargins(marginFn(currentPage) || def); + } +} + function fixPageMargins(margin) { if (isNumber(margin)) { margin = { left: margin, right: margin, top: margin, bottom: margin };