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

Conversation

seahorsepip
Copy link

Makes document definition pageMargins dynamic (based on current page):

var docDefinition = {
  // [left, top, right, bottom] or [horizontal, vertical] or just a number for equal margins
  // E.g. make top margin on first page larger for logo that's already on the paper
  pageMargins: currentPage =>  [ 40, currentPage === 1 ? 160 : 60, 40, 60 ],
};

Non dynamic page margins are still supported:

var docDefinition = {
  // [left, top, right, bottom] or [horizontal, vertical] or just a number for equal margins
  pageMargins: [ 40, 60, 40, 60 ],
};

Changes have been based on comments from Issue 368 with some additonal changes:

  • function return format is the same as non function format
  • use fixPageMargins to validate all return values from pageMargins function

@seahorsepip
Copy link
Author

^ I'll have a look at the tests.

@zubairmoolla
Copy link

zubairmoolla commented Jul 25, 2019

@seahorsepip
Would you know how to also parse the total page count. I'm struggling to define pageMargins according to the last page. Similar to how header and footer accepts a function which parses currentPage & pageCount values.

I would like to achieve the following;

pageMargins : function(currentPage, pageCount) {
	
	// Define pageMargins according to the last page.					
	if ( currentPage == pageCount ){							
		return [25, 120, 25, 240];
	} else {							
		return [25, 120, 25, 25];
	}						
}

@srallapalli
Copy link

Do you have solution for this issue , please provide the updates on this.

@rabraha3
Copy link

rabraha3 commented Oct 3, 2019

These merge conflicts seem pretty small.

@lieuzhenghong
Copy link

lieuzhenghong commented Nov 15, 2020

@seahorsepip It should but doesn't seem to work with horizontal margins, strangely enough.
See the following examples:

// left, top, right, bottom
pageMargins: currentPage => [100, ((currentPage % 2 === 1) ? 0 : 100), 100, 100], // OK
pageMargins: currentPage => [((currentPage % 2 === 1) ? 0 : 100), 100, 100, 100], // doesn't work

Ditto with

// horizontal, vertical
pageMargins: currentPage => [100, ((currentPage % 2 === 1) ? 0 : 100)], // works
pageMargins: currentPage => [((currentPage % 2 === 1) ? 0 : 100), 100], // doesn't

Would love your input as well @liborm85 .

@lieuzhenghong
Copy link

lieuzhenghong commented Nov 15, 2020

This is especially strange since we have already adjusted the availableWidth property in DocumentContext.prototype.initializePage:

this.pageSnapshot().availableWidth =
    this.getCurrentPage().pageSize.width -
    this.getCurrentPage().pageMargins.left -
    this.getCurrentPage().pageMargins.right;

@lieuzhenghong
Copy link

lieuzhenghong commented Dec 29, 2020

@liborm85 Have you taken a look at my examples? Thanks very much!

@seahorsepip
Copy link
Author

seahorsepip commented Dec 29, 2020

Sorry for the late reply, haven't looked at this MR (literally) for years.

I saw this comment about the horizontal margins: #368 (comment)

@lieuzhenghong
Copy link

@seahorsepip I believe I have tried that before

Sorry for the late reply, haven't looked at this MR (literally) for years.

I saw this comment about the horizontal margins: #368 (comment)

I have seen this comment as well but it does not work (see my second comment).

@seahorsepip
Copy link
Author

seahorsepip commented Dec 29, 2020

@lieuzhenghong
But pageMargins might be/is a function if I remember correctly?:

this.pageSnapshot().availableWidth =
    this.getCurrentPage().pageSize.width -
    this.getCurrentPage().pageMargins(* page nr *).left -
    this.getCurrentPage().pageMargins(* page nr *).right;

@albertoLGDev
Copy link

@seahorsepip Would you know how to also parse the total page count. I'm struggling to define pageMargins according to the last page. Similar to how header and footer accepts a function which parses currentPage & pageCount values.

I would like to achieve the following;

pageMargins : function(currentPage, pageCount) {
	
	// Define pageMargins according to the last page.					
	if ( currentPage == pageCount ){							
		return [25, 120, 25, 240];
	} else {							
		return [25, 120, 25, 25];
	}						
}

Has anyone found a solution for this?

Trim added a commit to Trim/pdfmake that referenced this pull request Jul 26, 2022
This function will receive the pageNumber as argument.

This commit is a port of original work of @seahorsepip in pull request bpampuch#1746

This rework is rebuild on 0.3 version, it adds some unit tests for
new helpers and it has integration tests following comments in original
pull request bpampuch#1746.
@Trim Trim mentioned this pull request Jul 26, 2022
Trim added a commit to Trim/pdfmake that referenced this pull request Jul 26, 2022
This function will receive the pageNumber as argument.

This commit is a port of original work of @seahorsepip in pull request bpampuch#1746

This rework is rebuild on 0.3 version, it adds some unit tests for
new helpers and it has integration tests following comments in original
pull request bpampuch#1746.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants