pdfkit/lib/image.js
2018-11-29 08:14:45 -08:00

40 lines
912 B
JavaScript

/*
PDFImage - embeds images in PDF documents
By Devon Govett
*/
import fs from 'fs';
import JPEG from './image/jpeg';
import PNG from './image/png';
class PDFImage {
static open(src, label) {
let data;
if (Buffer.isBuffer(src)) {
data = src;
} else if (src instanceof ArrayBuffer) {
data = new Buffer(new Uint8Array(src));
} else {
let match;
if (match = /^data:.+;base64,(.*)$/.exec(src)) {
data = new Buffer(match[1], 'base64');
} else {
data = fs.readFileSync(src);
if (!data) { return; }
}
}
if ((data[0] === 0xff) && (data[1] === 0xd8)) {
return new JPEG(data, label);
} else if ((data[0] === 0x89) && (data.toString('ascii', 1, 4) === 'PNG')) {
return new PNG(data, label);
} else {
throw new Error('Unknown image format.');
}
}
}
export default PDFImage;