Books are binded and therefore have different page margins for odd and even pages, so that the text on the page appears to be in the middle and not squeezed into the books fold. I searched the documentation/issues and tested it, the pageMargins seem to be static.
Yes, would be very helpful to be dynamic. I'd like to generate a different report footer just for the last page. It would nice to have pageMargins accept a function just like header/footer that has two parameters of currentPage and pageCount.
+1
+1
+1
+1
+1
+1
+1
+1
+1
i solve my problem with different header heights changing follow functions in documentContext.js.
function DocumentContext(pageSize, pageMargins) {
this.pages = [];
this.pageMargins = pageMargins;
this.pageMarginsFkt = null;
if (typeof this.pageMargins === 'function'){
this.pageMarginsFkt = this.pageMargins;
}
if (this.pageMarginsFkt){
this.pageMargins = this.pageMarginsFkt(1);
}
this.x =this. pageMargins.left;
this.availableWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right;
this.availableHeight = 0;
this.page = -1;
this.snapshots = [];
this.endingCell = null;
this.tracker = new TraversalTracker();
this.addPage(pageSize);
this.hasBackground = false;
}
DocumentContext.prototype.initializePage = function () {
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.addPage = function (pageSize) {
var page = {items: [], pageSize: pageSize};
this.pages.push(page);
this.page = this.pages.length - 1;
if (typeof this.pageMargins === 'function'){
this.pageMarginsFkt = this.pageMargins;
}
if (this.pageMarginsFkt){
this.pageMargins = this.pagerMarginsFkt(this.pages.length);
}
page.pageMargins = Object.assign( {} , this.pageMargins );
this.initializePage();
this.tracker.emit('pageAdded');
return page;
};
DocumentContext.prototype.getCurrentPosition = function () {
var pageSize = this.getCurrentPage().pageSize;
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.getCurrentPage().pageMargins.top) / innerHeight),
horizontalRatio: ((this.x - this.getCurrentPage().pageMargins.left) / innerWidth)
};
};
LayoutBuilder.prototype.addDynamicRepeatable = function (nodeGetter, sizeFunction) {
var pages = this.writer.context().pages;
for (var pageIndex = 0, l = pages.length; pageIndex < l; pageIndex++) {
this.writer.context().page = pageIndex;
var node = nodeGetter(pageIndex + 1, l, this.writer.context().pages[pageIndex].pageSize);
if (node) {
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));
this.writer.commitUnbreakableBlock(sizes.x, sizes.y);
}
}
};
md5-69957acd0f191e1cce7efc8b434b7efc
var docDefinition = {
......,
pageMargins : function(cp) {
if ( cp === 1 ){
return {left:30,top:150, right:30,bottom:60};
} else {
return {left:30,top:50, right:30,bottom:60};
}
},
.......
}
I save the pageMargins in the page object whenever a new one is created and if the parameters are needed I get them from the currentPage.
I have not found any negative side effects for me and I hope to have listed all my changes (I got them from a printout). Maybe that solves the problem of one or the other. Happy Easter :-)
sorry for my english, isn't my favorit language.
@icke792 Can you create a pull-request for it?
This was really useful for me. Please add it to the project. There seems to be a small fix to make it work with dynamic horizontal margins:
DocumentContext.prototype.initializePage = function () {
this.y = this.getCurrentPage().pageMargins.top;
this.x = this.getCurrentPage().pageMargins.left;
this.availableHeight = this.getCurrentPage().pageSize.height - this.getCurrentPage().pageMargins.top - this.getCurrentPage().pageMargins.bottom;
this.availableWidth = this.getCurrentPage().pageSize.width - this.getCurrentPage().pageMargins.left - this.getCurrentPage().pageMargins.right;
};
+1
+1
+1
This was really useful for me. Please add it to the project. There seems to be a small fix to make it work with dynamic horizontal margins:
DocumentContext.prototype.initializePage = function () { this.y = this.getCurrentPage().pageMargins.top; this.x = this.getCurrentPage().pageMargins.left; this.availableHeight = this.getCurrentPage().pageSize.height - this.getCurrentPage().pageMargins.top - this.getCurrentPage().pageMargins.bottom; this.availableWidth = this.getCurrentPage().pageSize.width - this.getCurrentPage().pageMargins.left - this.getCurrentPage().pageMargins.right; };
@hobbsi Did you just edit the source, build it and roll your own version?
@hobbsi Did you just edit the source, build it and roll your own version?
I used it while doing some experimentation on a personal project. And yes, that's what did. I believe you could also fork the project and do your modifications... I didn't.
This was really useful for me. Please add it to the project. There seems to be a small fix to make it work with dynamic horizontal margins:
DocumentContext.prototype.initializePage = function () { this.y = this.getCurrentPage().pageMargins.top; this.x = this.getCurrentPage().pageMargins.left; this.availableHeight = this.getCurrentPage().pageSize.height - this.getCurrentPage().pageMargins.top - this.getCurrentPage().pageMargins.bottom; this.availableWidth = this.getCurrentPage().pageSize.width - this.getCurrentPage().pageMargins.left - this.getCurrentPage().pageMargins.right; };
@hobbsi How would you apply this, once implemented, in the document definition?
@liborm85 any chance the approach of @icke792 with the fix of @hobbsi gets merged if I would create a PR?
Also, I have the need for a dynamic margin based on a dynamic header (see #2238) which is even a step further than what we have here. Any chance a solution taking dynamic headers into account gets merged in the end?
Most helpful comment
This was really useful for me. Please add it to the project. There seems to be a small fix to make it work with dynamic horizontal margins: