-
Notifications
You must be signed in to change notification settings - Fork 0
/
makePDF.js
244 lines (206 loc) · 6.12 KB
/
makePDF.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import fetch from "node-fetch";
import PDFDocument from "./pdfWrap.cjs";
import cache from "./cache.js";
import * as fs from 'fs';
import { convertManifest } from './pdiiif-lib/lib/esm/index.js';
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// See https://github.com/vanda/cocktail
function v2Extract(manifest) {
let canvases = [];
for (const sequence of manifest["sequences"]) {
let index = 0;
for (const canvas of sequence["canvases"]) {
let imageLabel = canvas["label"]["@value"]
? canvas["label"]["@value"]
: canvas["label"];
for (const image of canvas["images"]) {
let imageUrl = image["resource"]["@id"];
canvases.push({ id: canvas.id, imageUrl, imageLabel, imagePos: index + 1 });
}
index++;
}
}
const id = manifest["@id"];
const label = manifest["label"];
return {
id,
label,
provider: "Information not Available",
canvases,
};
}
function v3Extract(manifest) {
let canvases = [];
let index = 0;
for (const item of manifest["items"]) {
let imageLabel = "";
if (
item.hasOwnProperty("label") &&
item["label"].hasOwnProperty("none") &&
Array.isArray(item["label"]["none"])
)
imageLabel = item["label"]["none"][0];
for (const itemDepth2 of item["items"]) {
for (const itemDepth3 of itemDepth2["items"]) {
if (
itemDepth3.hasOwnProperty("body") &&
itemDepth3["body"].hasOwnProperty("id")
) {
let imageUrl = itemDepth3["body"]["id"];
canvases.push({ id: item.id, imageUrl, imageLabel, imagePos: index + 1 });
}
}
}
index++;
}
const id = manifest["id"];
const label = manifest["label"]["none"].toString();
let provider = "Information not Available";
if (
manifest.hasOwnProperty("provider") &&
Array.isArray(manifest["provider"])
) {
let labels = "";
for (let providerObj of manifest["provider"]) {
if (providerObj.hasOwnProperty("label")) {
let labelArray = [];
for (let langKey in providerObj["label"]) {
labelArray.push(providerObj["label"][langKey].join(", "));
}
labels += labelArray.join(", ") + " ";
}
}
if (labels.length) provider = labels + " ";
}
return {
id,
label,
provider,
canvases,
};
}
function getManifestVersion(manifest) {
// V3 returns an array V2 is just a string
let versionUrl = manifest["@context"];
if (Array.isArray(versionUrl)) versionUrl = versionUrl[1];
const match = versionUrl.match(/\/presentation\/([0-9]+)\//);
let version = 0;
if (match) {
version = match[1];
}
return version;
}
// [ [start, end], [start], [start, end] ]
function extractCanvases(canvases, canvasPositionsArray) {
let extractedCanvases = [];
for (const subArray of canvasPositionsArray) {
if (subArray.length === 2) {
extractedCanvases = extractedCanvases.concat(
canvases.slice(subArray[0], subArray[1] + 1).map(canvas => canvas.id)
); // non inclusive so + 1
} else if (subArray.length === 1) {
extractedCanvases.push(canvases[subArray[0]].id);
}
}
return extractedCanvases;
}
async function generatePDF(
id,
label,
provider,
fileName,
extractedCanvases,
cacheName
) {
let doc = new PDFDocument();
doc
.text("Manifest Information")
.text("Id: " + id)
.text("Label: " + label)
.text("Provider: " + provider);
// Get a reference to the Outline root
const { outline } = doc;
// Add a top-level bookmark
const top = outline.addItem(fileName);
let item = 1;
for (const imageData of extractedCanvases) {
doc.addPage();
top.addItem(`${fileName}/${imageData.imagePos}`);
try {
const settings = { method: "Get", timeout: 8000000 };
const response = await fetch(imageData.imageUrl, settings);
const currentBuffer = Buffer.from(await response.arrayBuffer());
const currentURI = `data:image/jpeg;base64,${currentBuffer.toString(
"base64"
)}`;
doc
.text("Label: " + imageData.imageLabel, 15, 15)
.image(currentURI, 15, 30, {fit: [600, 700], align: 'center', valign: 'center'})
} catch (e) {
console.log("Error on: ", imageData);
console.log(e);
doc.text(
"Label: " + imageData.imageLabel + " Error could not grab image data.",
15,
15
);
}
const progress = Math.round((item / extractedCanvases.length) * 100)
await cache.set(cacheName, { progress })
item++
}
doc.end();
return doc;
//return await getStream.buffer(doc)
}
const MakePDF = async (
manifestURL,
canvasPositionsArray,
fileName,
cacheName
) => {
//"https://wellcomelibrary.org/iiif/b18035723/manifest";
// https://iiif.wellcomecollection.org/presentation/v3/b18035723
const settings = { method: "Get" };
const res = await fetch(manifestURL, settings);
const manifest = await res.json();
// TODO fix in CAP
delete manifest.provider;
const version = getManifestVersion(manifest);
let info = {};
if (version === "2") {
info = v2Extract(manifest);
} else if (version === "3") {
info = v3Extract(manifest);
}
if (info["canvases"].length) {
let extractedCanvases = extractCanvases(
info["canvases"],
canvasPositionsArray
);
console.log("extractedCanvases", extractedCanvases)
const webWritable = fs.createWriteStream(path.join(__dirname, `static/${cacheName}`));
// Start the PDF generation
const onProgress = status => {
let count = parseInt(status.pagesWritten)
const progress = Math.round(( count / extractedCanvases.length) * 100)
cache.set(cacheName, { progress }).then(() => {
console.log(`${extractedCanvases.length} -- ${cacheName} -- Pages Written: ${status.pagesWritten} -- Progress ${progress}%.`);
})
}
await convertManifest(
manifest,
webWritable,
{
maxWidth: 1500,
onProgress,
filterCanvases: extractedCanvases
});
} else throw "No canvases to export";
return
};
export default MakePDF;