Skip to content

Commit 08a68d9

Browse files
author
Andrey
committed
Added error handling so the server doesn't crash
1 parent 3db6d91 commit 08a68d9

File tree

3 files changed

+45
-64
lines changed

3 files changed

+45
-64
lines changed

files/document.docx.pdf.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Sample DOCX Document
2+
This document is just a sample of a docx document.
3+
It might include a table:
4+
Heading 1
5+
Entry 1
6+
Entry 2
7+
And some lorem ipsum:
8+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dui neque, hendrerit ac
9+
vulputate ac, dictum at urna. Vivamus pulvinar tempor lacus, vel aliquet lectus sollicitudin id.
10+
Phasellus eu volutpat erat. Nullam orci augue, malesuada vel tristique id, porta a lectus. Nunc
11+
sollicitudin, mi sit amet lobortis ultrices, mi libero egestas eros, consectetur euismod velit ante
12+
vel nibh. Phasellus in urna vehicula, vestibulum dolor non, pulvinar mauris. Phasellus porttitor
13+
rhoncus augue at pretium. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices
14+
posuere cubilia Curae; Mauris eu varius sapien. Integer pharetra mollis mi, vitae semper felis
15+
convallis id. Duis aliquam rutrum laoreet. Fusce eu ex metus.
16+
Etiam sit amet purus quis nisl sodales commodo. In et venenatis diam, et viverra enim.
17+
Curabitur vitae sem id neque interdum eleifend. Morbi eget est nec purus porta viverra a eget
18+
turpis. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
19+
Fusce faucibus ante nec ante sollicitudin cursus. Nullam porttitor magna viverra facilisis
20+
pharetra. Nunc eget maximus augue. Fusce in elementum augue, non volutpat sem. Aliquam
21+
vel eleifend velit, ut vulputate mi.
22+
Nullam velit nulla, malesuada consequat leo et, finibus auctor magna. Curabitur accumsan, nibh
23+
et commodo molestie, odio mi auctor velit, eget porta tortor orci nec urna. Vivamus id massa
24+
bibendum, molestie felis ut, mollis dolor. Pellentesque habitant morbi tristique senectus et netus
25+
et malesuada fames ac turpis egestas. Vestibulum iaculis viverra dictum. Nullam quis congue
26+
ipsum, in convallis nulla. Nulla dictum lacus in dolor ultricies, vel posuere nisi ultrices. Morbi
27+
vestibulum facilisis ligula, eget finibus tortor tristique vitae. Mauris elementum, elit et vestibulum
28+
pulvinar, purus libero ornare nisl, eu ultrices diam ex ut nunc. Quisque id tincidunt eros. Ut quis
29+
vulputate urna, eu suscipit libero.
30+
Heading 2
31+
Heading 3

files/webviewer.pdf.txt

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
A lower-quality library also encounters
2-
performance and memory issues, such as large
3-
documents with frustratingly long wait times for
4-
your users as well as complex documents that
5-
crash the viewer. This is often due to the absence
6-
of features such as PDF tiling, parallelization,
7-
and linearization that a more mature PDF SDK
8-
will incorporate.
9-
Some solutions (e.g., image servers) perform
10-
excellently when tested on a small number of
11-
documents and users but then inflict unexpected
12-
hidden costs when scaled up. When hundreds
13-
or thousands of users later view, mark up, comment
14-
on, and otherwise interact with (i.e.,scroll,
15-
pan, and zoom) documents, server resource and
16-
network data usage explodes. To maintain your
17-
desired UX, you have to pay higher fees or invest
18-
in more servers.
19-
The following types of documents have much
20-
more demanding rendering requirements:
21-
• CAD-based PDFs such as construction and
22-
engineering drawings with very large and
23-
complex designs.
24-
• Reports, textbooks, and marketing material
25-
using advanced PDF graphics such as shadings,
26-
gradients, soft masks, and patterns.
27-
• Geospatial maps with OCG layers that are
28-
switched off by default.
29-
• Pre-press documents which require an SDK
30-
with advanced color management features to
31-
print colors accurately.
32-
• High-speed accurate rendering (especially on
33-
native mobile apps and mobile browsers).
34-
• Context extraction of tables, text, etc. with
35-
document structure (e.g., text read order or
36-
table arrangement) in tact.
37-
To prevent crashes, slowness, and rendering
38-
issues from disrupting your UX, test functionality
39-
with the types of documents your users will work
40-
with. Also test a server-based solution at the
41-
anticipated load and usage.
42-
6
1+
6 Important Factors when
2+
Choosing a PDF Library
3+
ADAM PEZ

index.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ app.get('/optimize/:filename', (req, res) => {
4545
);
4646

4747
if (ext !== '.pdf') {
48-
res.statusCode = 500;
49-
res.end(
50-
`Only PDFs can be optimized. Cannot optimize file with extension: ${ext}.`,
51-
);
48+
throw `Only PDFs can be optimized. Cannot optimize file with extension: ${ext}.`;
5249
}
5350

5451
const main = async () => {
@@ -85,10 +82,7 @@ app.get('/thumbnail/:filename', (req, res) => {
8582
const outputPath = path.resolve(__dirname, filesPath, `${filename}.png`);
8683

8784
if (ext !== '.pdf') {
88-
res.statusCode = 500;
89-
res.end(
90-
`Only PDFs can return a thumbnail. Cannot return a thumb for a file with extension: ${ext}.`,
91-
);
85+
throw `Only PDFs can return a thumbnail. Cannot return a thumb for a file with extension: ${ext}.`;
9286
}
9387

9488
const main = async () => {
@@ -144,14 +138,12 @@ app.get('/textextract/:filename-:pagenumber', (req, res) => {
144138
const main = async () => {
145139
await PDFNet.initialize();
146140
try {
147-
await PDFNet.startDeallocateStack();
148141
const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
149142
await pdfdoc.initSecurityHandler();
150143
const page = await pdfdoc.getPage(pageNumber);
151144

152-
if (page.id === '0') {
153-
console.log('Page not found.');
154-
return 1;
145+
if (!page) {
146+
throw 'Page number is invalid.';
155147
}
156148

157149
const txt = await PDFNet.TextExtractor.create();
@@ -163,11 +155,8 @@ app.get('/textextract/:filename-:pagenumber', (req, res) => {
163155
fs.writeFile(outputPath, text, (err) => {
164156
if (err) return console.log(err);
165157
});
166-
await PDFNet.endDeallocateStack();
167158
} catch (err) {
168-
console.log(err);
169-
console.log(err.stack);
170-
return 1;
159+
throw err;
171160
}
172161
};
173162

@@ -176,13 +165,9 @@ app.get('/textextract/:filename-:pagenumber', (req, res) => {
176165

177166
const PDFNetEndpoint = (main, pathname, res) => {
178167
PDFNet.runWithCleanup(main)
179-
.catch(function (error) {
180-
res.statusCode = 500;
181-
res.end(`Error : ${JSON.stringify(error)}.`);
182-
})
183-
.then(function () {
168+
.then(() => {
184169
PDFNet.shutdown();
185-
fs.readFile(pathname, function (err, data) {
170+
fs.readFile(pathname, (err, data) => {
186171
if (err) {
187172
res.statusCode = 500;
188173
res.end(`Error getting the file: ${err}.`);
@@ -192,6 +177,10 @@ const PDFNetEndpoint = (main, pathname, res) => {
192177
res.end(data);
193178
}
194179
});
180+
})
181+
.catch((error) => {
182+
res.statusCode = 500;
183+
res.end(error);
195184
});
196185
};
197186

0 commit comments

Comments
 (0)