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

Dynamic page margins #1746

Open
wants to merge 15 commits into
base: 0.1
Choose a base branch
from
39 changes: 26 additions & 13 deletions src/documentContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 () {
Expand All @@ -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;
}
};

Expand Down Expand Up @@ -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');
Expand All @@ -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)
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/elementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
13 changes: 12 additions & 1 deletion src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 };
Expand Down