-
Notifications
You must be signed in to change notification settings - Fork 23
/
app.js
304 lines (248 loc) · 8.92 KB
/
app.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const express = require('express');
const fs = require('fs');
const path = require('path');
const { PDFNet } = require('@pdftron/pdfnet-node');
const mimeType = require('./modules/mimeType');
const filesPath = './files';
const app = express();
app.get('/files', (req, res) => {
const inputPath = path.resolve(__dirname, filesPath);
fs.readdir(inputPath, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
res.setHeader('Content-type', mimeType['.json']);
res.end(JSON.stringify(files));
});
});
app.get('/files/:filename', (req, res) => {
const inputPath = path.resolve(__dirname, filesPath, req.params.filename);
fs.readFile(inputPath, function (err, data) {
if (err) {
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
const ext = path.parse(inputPath).ext;
res.setHeader('Content-type', mimeType[ext] || 'text/plain');
res.end(data);
}
});
});
app.get('/optimize/:filename', (req, res) => {
const filename = req.params.filename;
const ext = path.parse(filename).ext;
const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(
__dirname,
filesPath,
`optimized_${filename}`,
);
if (ext !== '.pdf') {
throw `Only PDFs can be optimized. Cannot optimize file with extension: ${ext}.`;
}
const main = async () => {
const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
await doc.initSecurityHandler();
// compress
const image_settings = new PDFNet.Optimizer.ImageSettings();
image_settings.setCompressionMode(
PDFNet.Optimizer.ImageSettings.CompressionMode.e_jpeg,
);
const opt_settings = new PDFNet.Optimizer.OptimizerSettings();
opt_settings.setColorImageSettings(image_settings);
opt_settings.setGrayscaleImageSettings(image_settings);
await PDFNet.Optimizer.optimize(doc, opt_settings);
// viewer optimizer + linearization
const opts = new PDFNet.PDFDoc.ViewerOptimizedOptions();
opts.setThumbnailRenderingThreshold(0);
await doc.saveViewerOptimized(outputPath, opts);
};
PDFNetEndpoint(main, outputPath, res);
});
app.get('/thumbnail/:filename', (req, res) => {
const filename = req.params.filename;
let ext = path.parse(filename).ext;
const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(__dirname, filesPath, `${filename}.png`);
if (ext !== '.pdf') {
throw `Only PDFs can return a thumbnail. Cannot return a thumb for a file with extension: ${ext}.`;
}
const main = async () => {
const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
await doc.initSecurityHandler();
const pdfdraw = await PDFNet.PDFDraw.create(92);
const currPage = await doc.getPage(1);
await pdfdraw.export(currPage, outputPath, 'PNG');
};
PDFNetEndpoint(main, outputPath, res);
});
app.get('/convert/:filename', (req, res) => {
const filename = req.params.filename;
let ext = path.parse(filename).ext;
const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(__dirname, filesPath, `${filename}.pdf`);
if (ext === '.pdf') {
res.statusCode = 500;
res.end(`File is already PDF.`);
}
const main = async () => {
const pdfdoc = await PDFNet.PDFDoc.create();
await pdfdoc.initSecurityHandler();
await PDFNet.Convert.toPdf(pdfdoc, inputPath);
pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
};
PDFNetEndpoint(main, outputPath, res);
});
app.get('/convertHTML/:filename-:htmlPath', (req, res) => {
const filename = req.params.filename;
const htmlPath = req.params.htmlPath;
const inputPath = path.resolve(__dirname, filesPath, htmlPath);
const outputPath = path.resolve(__dirname, filesPath, `${filename}.pdf`);
const main = async () => {
try {
await PDFNet.HTML2PDF.setModulePath(
path.resolve(__dirname, './node_modules/@pdftron/pdfnet-node/lib/'),
);
const settings = await PDFNet.HTML2PDF.WebPageSettings.create();
settings.setAllowJavaScript(true);
settings.setProduceForms(true);
const html2pdf = await PDFNet.HTML2PDF.create();
const pdfdoc = await PDFNet.PDFDoc.create();
await html2pdf.insertFromUrl2(inputPath, settings);
await html2pdf.convert(pdfdoc);
await pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
} catch (err) {
console.log(err);
}
};
PDFNetEndpoint(main, outputPath, res);
});
app.get('/generate/:filename', (req, res) => {
const filename = req.params.filename;
const outputPath = path.resolve(__dirname, filesPath, `${filename}.pdf`);
const main = async () => {
const pdfdoc = await PDFNet.PDFDoc.create();
await pdfdoc.initSecurityHandler();
const page1 = await pdfdoc.pageCreate();
pdfdoc.pagePushBack(page1);
pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
};
PDFNetEndpoint(main, outputPath, res);
});
app.get('/textExtract/:filename-:pagenumber', (req, res) => {
const filename = req.params.filename;
let pageNumber = Number(req.params.pagenumber);
let ext = path.parse(filename).ext;
if (ext !== '.pdf') {
res.statusCode = 500;
res.end(`File is not a PDF. Please convert it first.`);
}
const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(
__dirname,
filesPath,
`${filename}-${pageNumber}.txt`,
);
const main = async () => {
await PDFNet.initialize();
try {
const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
await pdfdoc.initSecurityHandler();
const page = await pdfdoc.getPage(pageNumber);
if (!page) {
throw 'Page number is invalid.';
}
const txt = await PDFNet.TextExtractor.create();
const rect = new PDFNet.Rect(0, 0, 612, 794);
txt.begin(page, rect);
let text;
text = await txt.getAsText();
fs.writeFile(outputPath, text, err => {
if (err) return console.log(err);
});
} catch (err) {
throw err;
}
};
PDFNetEndpoint(main, outputPath, res);
});
app.get('/replaceContent/:name', (req, res) => {
const name = req.params.name.replace('_', ' ');
const filename = 'template_letter.pdf';
const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(
__dirname,
filesPath,
`${filename}_replaced.pdf`,
);
const main = async () => {
const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
await pdfdoc.initSecurityHandler();
const replacer = await PDFNet.ContentReplacer.create();
const page = await pdfdoc.getPage(1);
await replacer.addString('NAME', name);
await replacer.addString('Address', '123 Main St, Vancouver, BC CANADA');
await replacer.addString('DATE', new Date(Date.now()).toLocaleString());
await replacer.process(page);
pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
};
PDFNetEndpoint(main, outputPath, res);
});
app.get('/watermark/:filename-:watermark', (req, res) => {
const filename = req.params.filename;
const watermark = req.params.watermark;
let ext = path.parse(filename).ext;
if (ext !== '.pdf') {
res.statusCode = 500;
res.end(`File is not a PDF. Please convert it first.`);
}
const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(
__dirname,
filesPath,
`${filename}_watermarked.pdf`,
);
const main = async () => {
const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
await pdfdoc.initSecurityHandler();
const stamper = await PDFNet.Stamper.create(
PDFNet.Stamper.SizeType.e_relative_scale,
0.5,
0.5,
); // Stamp size is relative to the size of the crop box of the destination page
stamper.setAlignment(
PDFNet.Stamper.HorizontalAlignment.e_horizontal_center,
PDFNet.Stamper.VerticalAlignment.e_vertical_center,
);
const redColorPt = await PDFNet.ColorPt.init(1, 0, 0);
stamper.setFontColor(redColorPt);
const pgSet = await PDFNet.PageSet.createRange(
1,
await pdfdoc.getPageCount(),
);
stamper.stampText(pdfdoc, watermark, pgSet);
pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
};
PDFNetEndpoint(main, outputPath, res);
});
const PDFNetEndpoint = (main, pathname, res) => {
PDFNet.runWithCleanup(main) // you can add the key to PDFNet.runWithCleanup(main, process.env.PDFTRONKEY)
.then(() => {
PDFNet.shutdown();
fs.readFile(pathname, (err, data) => {
if (err) {
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
const ext = path.parse(pathname).ext;
res.setHeader('Content-type', mimeType[ext] || 'text/plain');
res.end(data);
}
});
})
.catch(error => {
res.statusCode = 500;
res.end(error);
});
};
module.exports = app;