Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit ec35445

Browse files
committed
Add browser check for PDF images
1 parent eda7bc5 commit ec35445

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

lib/image.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,37 @@ PDFImage - embeds images in PDF documents
33
By Devon Govett
44
*/
55

6-
import fs from 'fs';
7-
import JPEG from './image/jpeg';
8-
import PNG from './image/png';
6+
import fs from 'fs'
7+
import JPEG from './image/jpeg'
8+
import PNG from './image/png'
99

1010
class PDFImage {
1111
static open(src, label) {
12-
let data;
12+
let data
1313
if (Buffer.isBuffer(src)) {
14-
data = src;
14+
data = src
1515
} else if (src instanceof ArrayBuffer) {
16-
data = new Buffer(new Uint8Array(src));
16+
data = new Buffer(new Uint8Array(src))
1717
} else {
18-
let match;
19-
if (match = /^data:.+;base64,(.*)$/.exec(src)) {
20-
data = new Buffer(match[1], 'base64');
21-
22-
} else {
23-
data = fs.readFileSync(src);
24-
if (!data) { return; }
18+
let match
19+
if ((match = /^data:.+;base64,(.*)$/.exec(src))) {
20+
data = new Buffer(match[1], 'base64')
21+
} else if (!BROWSER) {
22+
data = fs.readFileSync(src)
23+
if (!data) {
24+
return
25+
}
2526
}
2627
}
2728

28-
if ((data[0] === 0xff) && (data[1] === 0xd8)) {
29-
return new JPEG(data, label);
30-
31-
} else if ((data[0] === 0x89) && (data.toString('ascii', 1, 4) === 'PNG')) {
32-
return new PNG(data, label);
33-
29+
if (data[0] === 0xff && data[1] === 0xd8) {
30+
return new JPEG(data, label)
31+
} else if (data[0] === 0x89 && data.toString('ascii', 1, 4) === 'PNG') {
32+
return new PNG(data, label)
3433
} else {
35-
throw new Error('Unknown image format.');
34+
throw new Error('Unknown image format.')
3635
}
3736
}
3837
}
3938

40-
export default PDFImage;
39+
export default PDFImage

0 commit comments

Comments
 (0)