Skip to content

Commit

Permalink
✨ Support dynamic page margins
Browse files Browse the repository at this point in the history
Pages with odd numbers ("recto") often need a larger margin on the left
than on the right. For duplex printing, the margins need to be reversed
on the even pages ("verso").

This commit adds support for dynamic page margins by accepting a
function in the `margin` attribute of a document definition.
  • Loading branch information
ralfstx committed Jun 17, 2023
1 parent 48cfd5b commit 707771a
Show file tree
Hide file tree
Showing 7 changed files with 646 additions and 590 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog

## [0.5.0] - Unreleased
## [0.5.1] - Unreleased

### Added

* The attribute `margin` in a document definition now supports a
function that returns the margin for a given page.

## [0.5.0] - 2023-05-18

### Breaking changes

Expand Down
19 changes: 14 additions & 5 deletions examples/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ export default {
liberty: { data: readFileSync('examples/liberty.jpg') },
},
pageSize: 'A4',
margin: { x: '2.5cm', y: '0.5cm' },
margin: ({ pageNumber }) =>
pageNumber % 2
? { left: '25mm', right: '15mm', y: '0.5cm' }
: { left: '15mm', right: '25mm', y: '0.5cm' },
defaultStyle: {
fontSize: 14,
},
header: {
header: ({ pageNumber }) => ({
columns: [{ text: 'PDF Maker' }, { text: 'sample', textAlign: 'right' }],
margin: { x: '2.5cm', top: '1cm' },
},
margin:
pageNumber % 2
? { left: '25mm', right: '15mm', top: '1cm' }
: { left: '15mm', right: '25mm', top: '1cm' },
}),
footer: ({ pageNumber, pageCount }) => ({
text: `${pageNumber}/${pageCount ?? 0}`,
textAlign: 'right',
margin: { x: '2.5cm', bottom: '1cm' },
margin:
pageNumber % 2
? { left: '25mm', right: '15mm', bottom: '1cm' }
: { left: '15mm', right: '25mm', bottom: '1cm' },
}),
content: [
{
Expand Down
Loading

0 comments on commit 707771a

Please sign in to comment.