Skip to content

Commit 640ab3a

Browse files
author
Andrey
committed
Added Optimizer by PDFTron
1 parent ba7c39c commit 640ab3a

File tree

6 files changed

+457
-3
lines changed

6 files changed

+457
-3
lines changed

files/document.docx

7.66 KB
Binary file not shown.

files/image.jpg

165 KB
Loading
File renamed without changes.

index.js

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const fs = require('fs');
33
const path = require('path');
4+
const { PDFNet } = require('@pdftron/pdfnet-node');
45
const mimeType = require('./modules/mimeType');
56
const port = 9000;
67

@@ -16,14 +17,69 @@ app.get('/files', (req, res) => {
1617
});
1718
});
1819

19-
app.get('/files/:fileName', (req, res) => {
20-
const pathname = `./files/${req.params.fileName}`;
20+
app.get('/optimize/:filename', (req, res) => {
21+
const pathname = './files/';
22+
const filename = req.params.filename;
23+
24+
const main = async () => {
25+
const doc = await PDFNet.PDFDoc.createFromFilePath(pathname+filename);
26+
await doc.initSecurityHandler();
27+
28+
// compress
29+
const image_settings = new PDFNet.Optimizer.ImageSettings();
30+
31+
image_settings.setCompressionMode(
32+
PDFNet.Optimizer.ImageSettings.CompressionMode.e_jpeg,
33+
);
34+
35+
const opt_settings = new PDFNet.Optimizer.OptimizerSettings();
36+
opt_settings.setColorImageSettings(image_settings);
37+
opt_settings.setGrayscaleImageSettings(image_settings);
38+
39+
await PDFNet.Optimizer.optimize(doc, opt_settings);
40+
41+
// flattener
42+
const fl = await PDFNet.Flattener.create();
43+
await fl.process(doc, PDFNet.Flattener.Mode.e_fast);
44+
45+
// viewer optimizer
46+
const opts = new PDFNet.PDFDoc.ViewerOptimizedOptions();
47+
opts.setThumbnailRenderingThreshold(0);
48+
49+
await doc.saveViewerOptimized(`${pathname}optimized_${filename}`, opts);
50+
};
51+
52+
// add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
53+
PDFNet.runWithCleanup(main)
54+
.catch(function (error) {
55+
console.log('Error: ' + JSON.stringify(error));
56+
res.statusCode = 500;
57+
res.end(`Error : ${JSON.stringify(error)}.`);
58+
})
59+
.then(function () {
60+
PDFNet.shutdown();
61+
const newpath = `${pathname}optimized_${filename}`;
62+
fs.readFile(newpath, function (err, data) {
63+
if (err) {
64+
res.statusCode = 500;
65+
res.end(`Error getting the file: ${err}.`);
66+
} else {
67+
const ext = path.parse(newpath).ext;
68+
// if the file is found, set Content-type and send data
69+
res.setHeader('Content-type', mimeType[ext] || 'text/plain');
70+
res.end(data);
71+
}
72+
});
73+
});
74+
});
75+
76+
app.get('/files/:filename', (req, res) => {
77+
const pathname = `./files/${req.params.filename}`;
2178
fs.readFile(pathname, function (err, data) {
2279
if (err) {
2380
res.statusCode = 500;
2481
res.end(`Error getting the file: ${err}.`);
2582
} else {
26-
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
2783
const ext = path.parse(pathname).ext;
2884
// if the file is found, set Content-type and send data
2985
res.setHeader('Content-type', mimeType[ext] || 'text/plain');

0 commit comments

Comments
 (0)