-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaddPageNumbers.js
127 lines (107 loc) · 3.15 KB
/
addPageNumbers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const { PageSizes, StandardFonts } = require("pdf-lib");
const marginValuesNumber = {
Recommended: 10,
Small: 5,
Big: 15,
};
const marginIndexPagePosition = {
b: 3,
t: 1,
};
const getTextPosition = (
number,
width,
height,
marginValues,
textSize,
pageNumberPosition,
helveticaFont
) => {
const text = number.toString();
const textWidth = helveticaFont.widthOfTextAtSize(text, textSize);
const textHeight = helveticaFont.heightAtSize(textSize);
let textPositionX = 0,
textPositionY = 0;
if (pageNumberPosition[0] === "b") {
textPositionY = (textHeight - marginValues[1] - marginValues[3]) / 2;
}
if (pageNumberPosition[0] === "t") {
textPositionY =
height - textHeight - marginValues[1] / 2 - marginValues[3] / 2;
}
if (pageNumberPosition[2] === "l") {
textPositionX = textWidth * 2;
}
if (pageNumberPosition[2] === "c") {
textPositionX = width / 2 - textWidth / 2;
}
if (pageNumberPosition[2] === "r") {
textPositionX = width - textWidth * 2;
}
return { text, textPositionX, textPositionY };
};
const addPageNumbers = async (
pdfDoc,
pageNumberPosition = "b-c",
margin = "Recommended",
startFromPage = 1,
endPage = pdfDoc.getPageCount(),
startingNumber = 1,
textSize = 16
) => {
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const multiplier = PageSizes["A4"][0] / 210;
const marginValue = marginValuesNumber[margin];
const marginValues = [0, 0, 0, 0];
marginValues[marginIndexPagePosition[pageNumberPosition[0]]] =
marginValue * multiplier;
const pages = pdfDoc.getPages();
let number = startingNumber;
pages.slice(startFromPage - 1, endPage).forEach((page) => {
const { x, y, width, height } = page.getMediaBox();
const size = [width, height];
const newSize = [width, height + marginValues[1] + marginValues[3]];
const sizeRatio = Math.round((size[0] / size[1]) * 100);
const newSizeRatio = Math.round((newSize[0] / newSize[1]) * 100);
page.setSize(width, height + marginValues[1] + marginValues[3]);
page.translateContent(x, y + marginValues[3]);
const { text, textPositionX, textPositionY } = getTextPosition(
number,
width,
height,
marginValues,
textSize,
pageNumberPosition,
helveticaFont
);
page.drawText(text, {
x: textPositionX,
y: textPositionY,
size: textSize,
font: helveticaFont,
});
if (Math.abs(sizeRatio - newSizeRatio) > 1) {
// Change page size
page.setSize(size[0], size[1]);
const scale_content = Math.min(
size[0] / newSize[0],
size[1] / newSize[1]
);
// Scale content
page.scaleContent(scale_content, scale_content);
const scaledDiffernece = {
width: Math.round(size[0] - scale_content * newSize[0]),
height: Math.round(size[1] - scale_content * newSize[1]),
};
page.translateContent(
Math.round(scaledDiffernece.width / 2),
Math.round(scaledDiffernece.height / 2)
);
} else {
page.scale(size[0] / newSize[0], size[1] / newSize[1]);
}
number++;
});
return pdfDoc;
};
module.exports = addPageNumbers;