forked from awpeters/jspdftk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.js
84 lines (67 loc) · 1.96 KB
/
collect.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
function collectUsage(msg) {
var err = System.err;
err.print(msg + "\n");
err.print("Usage: collect.js width height\n");
err.print(" Collects as many pages into one page.\n");
err.print(" width / height: resulting page.\n");
quit(1);
}
function checkCollectArgs(args) {
if (args.length != 2) {
collectUsage("collect.js needs two arguments.");
} else {
width = mm2pt(parseInt(args[0]));
height = mm2pt(parseInt(args[1]));
}
return { width: width, height: height }
}
function collect(args) {
var err = System.err;
var params = checkCollectArgs(args);
var stdin = new BufferedInputStream(System["in"]);
var pdfIn = new PdfReader(stdin);
var w = params["width"];
var h = params["height"];
var stdout = new BufferedOutputStream(System.out);
var pageCount = pdfIn.getNumberOfPages();
var pageSize = pdfIn.getPageSize(1);
var wd = pageSize.getWidth();
var ht = pageSize.getHeight();
var document = new Document(new Rectangle(w, h));
var writer = PdfWriter.getInstance(document, stdout);
var xcopies = Math.round(w / wd, 0);
var ycopies = Math.round(h / ht, 0);
document.open();
err.println("collect (" + xcopies + " x " + ycopies + ")");
var x0 = 0.5 * (w - wd * xcopies);
var y0 = 0.5 * (h - ht * ycopies);
var c = 1.0;
var s = 0.0;
var x = x0;
var y = y0;
var cb = writer.getDirectContent();
for (var page = 0; page < pageCount; page++) {
if (0 === (page % (xcopies * ycopies))) {
err.print("[");
document.newPage();
x = x0;
y = y0;
} else {
x = x + wd;
}
if (x > w) {
x = x0;
y = y + ht;
}
var pg = writer.getImportedPage(pdfIn, page + 1);
cb.addTemplate(pg, c, s, -s, c, x, y);
err.print("[" + (page + 1) + "]");
}
err.print("]");
err.println();
document.close();
}
registerModule({'command': 'collect',
'name': 'collect pages into on PDFs',
'usage': collectUsage,
'entry': collect});