mirror of
https://github.com/foliojs/pdfkit.git
synced 2025-12-08 20:15:54 +00:00
35 lines
848 B
CoffeeScript
35 lines
848 B
CoffeeScript
###
|
|
PDFImage - embeds images in PDF documents
|
|
By Devon Govett
|
|
###
|
|
|
|
fs = require 'fs'
|
|
Data = require './data'
|
|
JPEG = require './image/jpeg'
|
|
PNG = require './image/png'
|
|
|
|
class PDFImage
|
|
@open: (filenameOrBuffer, label) ->
|
|
if typeof filenameOrBuffer is 'object' and filenameOrBuffer instanceof Buffer
|
|
@contents = filenameOrBuffer
|
|
else
|
|
@contents = fs.readFileSync filenameOrBuffer
|
|
return unless @contents
|
|
|
|
@data = new Data @contents
|
|
@filter = null
|
|
|
|
# load info
|
|
data = @data
|
|
firstByte = data.byteAt(0)
|
|
|
|
if firstByte is 0xFF and data.byteAt(1) is 0xD8
|
|
return new JPEG(data, label)
|
|
|
|
else if firstByte is 0x89 and data.stringAt(1, 3) is "PNG"
|
|
return new PNG(data, label)
|
|
|
|
else
|
|
throw new Error 'Unknown image format.'
|
|
|
|
module.exports = PDFImage |