prawn-pdfimage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.markdown +49 -0
  3. data/Rakefile +7 -0
  4. data/lib/prawn-pdfimage.rb +122 -0
  5. metadata +109 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 780170bcc691bd1849d090a2dd267484ce5d8614
4
+ data.tar.gz: ba40f161d698ea03de36ab598e8d2f30f74f901d
5
+ SHA512:
6
+ metadata.gz: 193f130eb069f5b6678b624e28bb31f9b974a1704e3c0f29200283ce2225d10999c062712a9e9b45da5439e779f556cccf66cfb037cb54c3bfc0cb6b9d47f7e2
7
+ data.tar.gz: 281fbd70b1f88235a2845b7ca3f1db64afa5a8aed4a2fe262362f73df1018b67d7b7ec4831ac3bd346214ece6266c24a3265120a68500dbc86d122b748237b63
@@ -0,0 +1,49 @@
1
+ # prawn-pdfimage
2
+
3
+ Adds support for a [prawn](https://github.com/prawnpdf/prawn) to use
4
+ a PDF that contains an image as an image.
5
+
6
+
7
+ ## Usage
8
+
9
+ **Gemfile**
10
+ ```ruby
11
+ gem 'prawn-pdfimage'
12
+ ```
13
+
14
+ **Your code**
15
+ ```ruby
16
+ require 'prawn'
17
+ require 'prawn-pdfimage'
18
+
19
+ Prawn::Document.generate('output.pdf') do |pdf|
20
+ pdf.image 'image.pdf'
21
+ end
22
+ ```
23
+
24
+
25
+ ## Limitations
26
+ The current code will add the first xobject on the first page as the image to
27
+ the Prawn document. For the common case (someone exporting an image as a PDF)
28
+ this works just fine. If you need more control over which object is inserted
29
+ into your Prawn document, PRs are welcome.
30
+
31
+
32
+ ## Found a bug?
33
+ Open a [github issue](https://github.com/packetmonkey/prawn-pdfimage/issues)
34
+
35
+
36
+ ## Contributing & Development
37
+ 1. Fork the project.
38
+ 2. Make your feature addition or bug fix. All specs should pass.
39
+ 3. Add specs for your changes.
40
+ 4. Commit
41
+ 5. Send a pull request. Bonus points for topic branches.
42
+
43
+
44
+ ## Licence
45
+ prawn-pdfimage is released under the [MIT Licence](http://choosealicense.com/licenses/mit/)
46
+
47
+
48
+ ## Authors
49
+ prawn-pdfimage is written and maintained by Evan Sharp.
@@ -0,0 +1,7 @@
1
+ require 'rubocop/rake_task'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+ RuboCop::RakeTask.new
6
+
7
+ task default: [:spec, :rubocop]
@@ -0,0 +1,122 @@
1
+ require 'prawn'
2
+ require 'pdf-reader'
3
+
4
+ # Implements support for pulling an image out of an existing PDF and adds
5
+ # that image to a prawn document.
6
+ #
7
+ # In this file, the pdf_* prefix refers to the PDF that contains the image
8
+ # we are adding to our document, prawn_* refers to the objects we are creating
9
+ # to be added to our prawn document.
10
+ class PrawnPDFImage < Prawn::Images::Image
11
+ def self.can_render?(image_blob)
12
+ image_blob.unpack('C5') == [37, 80, 68, 70, 45]
13
+ end
14
+
15
+ def initialize(image_blob)
16
+ self.image_blob = image_blob
17
+ self.reader = ::PDF::Reader.new StringIO.new(image_blob)
18
+ end
19
+
20
+ def build_pdf_object(document)
21
+ self.document = document
22
+
23
+ prawn_image_reference
24
+ end
25
+
26
+ private
27
+
28
+ attr_accessor :image_blob, :reader, :document
29
+
30
+ def prawn_image_reference
31
+ document.ref!(prawn_image_reference_options).tap do |ref|
32
+ ref << pdf_image_data
33
+ ref.stream.filters << { pdf_image_filter => nil }
34
+ ref.data[:SMask] = prawn_soft_mask if pdf_soft_mask
35
+ end
36
+ end
37
+
38
+ def prawn_image_reference_options
39
+ {
40
+ Type: :XObject,
41
+ Subtype: :Image,
42
+ ColorSpace: pdf_image_colorspace,
43
+ Height: pdf_image_height,
44
+ Width: pdf_image_width,
45
+ BitsPerComponent: pdf_image_bits_per_component
46
+ }
47
+ end
48
+
49
+ def prawn_soft_mask
50
+ document.ref!(prawn_soft_mask_options).tap do |ref|
51
+ ref.stream << pdf_soft_mask.unfiltered_data
52
+ ref.stream.filters << { FlateDecode: nil }
53
+ end
54
+ end
55
+
56
+ def prawn_soft_mask_options
57
+ {
58
+ Type: :XObject,
59
+ Subtype: :Image,
60
+ Width: pdf_image_width,
61
+ Height: pdf_image_height,
62
+ BitsPerComponent: pdf_image_bits_per_component,
63
+ ColorSpace: :DeviceGray
64
+ }
65
+ end
66
+
67
+ def pdf_soft_mask
68
+ pdf_image_stream.hash[:SMask]
69
+ end
70
+
71
+ def pdf_image_filter
72
+ pdf_image_stream.hash[:Filter]
73
+ end
74
+
75
+ # In my tests all the PDF color spaces where contained in an ICCBased
76
+ # PDF reference to a dictionary with an Alternate key.
77
+ #
78
+ # If images are not displaying correctly this is the first place to start
79
+ # looking.
80
+ def pdf_image_colorspace
81
+ cs = pdf_image_stream.hash[:ColorSpace]
82
+
83
+ if cs.is_a? Symbol
84
+ cs
85
+ else
86
+ return cs[1].hash[:Alternate]
87
+ end
88
+ end
89
+
90
+ def pdf_image_data
91
+ pdf_image_stream.unfiltered_data
92
+ end
93
+
94
+ def pdf_image_bits_per_component
95
+ pdf_image_stream.hash[:BitsPerComponent]
96
+ end
97
+
98
+ def pdf_image_height
99
+ pdf_image_stream.hash[:Height]
100
+ end
101
+
102
+ def pdf_image_width
103
+ pdf_image_stream.hash[:Width]
104
+ end
105
+
106
+ def pdf_image_stream
107
+ page.xobjects.first[1]
108
+ end
109
+
110
+ def page
111
+ reader.page 1
112
+ end
113
+
114
+ # These methods are to conform to the API expected by Prawn Images and
115
+ # are not used internally.
116
+ attr_accessor :scaled_width, :scaled_height
117
+
118
+ alias_method :width, :pdf_image_width
119
+ alias_method :height, :pdf_image_height
120
+ end
121
+
122
+ Prawn.image_handler.register PrawnPDFImage
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn-pdfimage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evan Sharp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prawn
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.15'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: pdf-reader
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: 1.3.3
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.3
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description:
76
+ email:
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - README.markdown
82
+ - Rakefile
83
+ - lib/prawn-pdfimage.rb
84
+ homepage:
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.5
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Add PDF 'images' as images in prawn documents
108
+ test_files: []
109
+ has_rdoc: