Class: JPEG2PDF
Constant Summary
Constants included from RPDF
RPDF::EOL, RPDF::VERSION_10, RPDF::VERSION_11, RPDF::VERSION_12, RPDF::VERSION_13, RPDF::VERSION_14, RPDF::VERSION_15, RPDF::VERSION_DEFAULT
Instance Method Summary collapse
- #add_jpeg(filename) ⇒ Object
- #close ⇒ Object
-
#initialize(pdf) ⇒ JPEG2PDF
constructor
A new instance of JPEG2PDF.
Methods included from RPDF
includes_delimiter?, includes_whitespace?, topdfobject
Constructor Details
#initialize(pdf) ⇒ JPEG2PDF
Returns a new instance of JPEG2PDF.
36 37 38 39 |
# File 'lib/jpeg2pdf.rb', line 36 def initialize(pdf) @doc = Document.new(pdf) @imagenumber = 0 end |
Instance Method Details
#add_jpeg(filename) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jpeg2pdf.rb', line 41 def add_jpeg(filename) jpegfile = File.open(filename) imagedata = jpegfile.read jpegfile.close size = ImageSize.new(imagedata) if(size.get_type == 'JPEG') width = size.get_width height = size.get_height bpc = size.get_bpc components = size.get_components if components == 1 colorspace = :DeviceGray elsif components == 3 colorspace = :DeviceRGB elsif components == 4 colorspace = :DeviceCMYK else raise Exception.new("Unsupported colorspace in image #{filename}") end imagestream = PDFStream.new(imagedata, @doc) imagestream.dict[:Filter] = [:DCTDecode] imagestream.dict[:Name] = "Image#{@imagenumber}".intern imagedictionary = ImageDictionary.new(width, height, colorspace, bpc) imagestream.dict.update(imagedictionary) imagestream.invalidate # write jpeg as soon as possible page = Page.new(@doc, @doc.pagetree) page[:MediaBox] = [0, 0, width, height] page[:Resources] = { :XObject => { "Image#{@imagenumber}".intern => imagestream } } contentstream = PDFStream.new("q\n#{width} 0 0 #{height} 0 0 cm\n/Image#{@imagenumber} Do\nQ", @doc) page[:Contents] = contentstream @imagenumber = @imagenumber + 1 end end |
#close ⇒ Object
80 81 82 |
# File 'lib/jpeg2pdf.rb', line 80 def close @doc.close_pdf end |