UNPKG

1.12 kBJavaScriptView Raw
1
2/*
3PDFImage - embeds images in PDF documents
4By Devon Govett
5 */
6
7(function() {
8 var Data, JPEG, PDFImage, PNG, fs;
9
10 fs = require('fs');
11
12 Data = require('./data');
13
14 JPEG = require('./image/jpeg');
15
16 PNG = require('./image/png');
17
18 PDFImage = (function() {
19 function PDFImage() {}
20
21 PDFImage.open = function(src, label) {
22 var data, match;
23 if (Buffer.isBuffer(src)) {
24 data = src;
25 } else if (src instanceof ArrayBuffer) {
26 data = new Buffer(new Uint8Array(src));
27 } else {
28 if (match = /^data:.+;base64,(.*)$/.exec(src)) {
29 data = new Buffer(match[1], 'base64');
30 } else {
31 data = fs.readFileSync(src);
32 if (!data) {
33 return;
34 }
35 }
36 }
37 if (data[0] === 0xff && data[1] === 0xd8) {
38 return new JPEG(data, label);
39 } else if (data[0] === 0x89 && data.toString('ascii', 1, 4) === 'PNG') {
40 return new PNG(data, label);
41 } else {
42 throw new Error('Unknown image format.');
43 }
44 };
45
46 return PDFImage;
47
48 })();
49
50 module.exports = PDFImage;
51
52}).call(this);