pdf_watermark 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6fa2c309b47fcfa6f4de0310141ddc3692a5ac3
4
+ data.tar.gz: 8b1ff1ac092ce9625b58e605608304901eb081c1
5
+ SHA512:
6
+ metadata.gz: c004bc7c419b9170782d4a7d82542e45f54bc30f02e2f47462555c2e6d36867a17ccfd452e3bd5d7481d61d38b04e641f5c2d0452de7cbe14af514dcb3074d19
7
+ data.tar.gz: 9e6b1e1879cec81d3bdf410e7d433ada515fdef380bb8a3e267ba9b6d639dd33d5e1ef60e9d4a2201a96bd667f5eccd77f9d9fc9e3a6a4225a1ae70004bba05a
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
11
+ /spec/data/result.pdf
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ # This is the configuration used to check the rubocop source code.
2
+
3
+ inherit_from: .rubocop_todo.yml
4
+
5
+ AllCops:
6
+ Exclude:
7
+ - 'vendor/**/*'
8
+ - 'spec/fixtures/**/*'
9
+ - 'tmp/**/*'
10
+ TargetRubyVersion: 2.3
11
+
12
+ Style/Encoding:
13
+ EnforcedStyle: when_needed
14
+ Enabled: true
15
+
16
+ Style/FrozenStringLiteralComment:
17
+ EnforcedStyle: always
18
+
19
+ Style/IndentHeredoc:
20
+ EnforcedStyle: powerpack
21
+
22
+ Metrics/BlockLength:
23
+ Exclude:
24
+ - 'Rakefile'
25
+ - '**/*.rake'
26
+ - 'spec/**/*.rb'
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://ruby.taobao.org/'
2
+ # Specify your gem's dependencies in pdf_watermark.gemspec
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 cr
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # PdfWatermark
2
+
3
+ Add watermark to your pdf. two external gem will be used: combine_pdf and prawn.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pdf_watermark'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pdf_watermark
20
+
21
+ ## Usage
22
+ ```ruby
23
+ require 'pdf_watermark'
24
+
25
+ PdfWatermark.watermark("Private and Confidential", "content.pdf", "result.pdf", {:margin => 20,:angle=>:diagonal})
26
+ ```
27
+ ## Development
28
+
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pdf_watermark. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
33
+
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
38
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pdf_watermark"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
@@ -0,0 +1,51 @@
1
+ require "pdf_watermark/version"
2
+ require 'pdf_watermark/water_mark'
3
+ require 'combine_pdf'
4
+ module PdfWatermark
5
+
6
+ LIB_DIR = File.dirname(File.realpath(__FILE__))
7
+ BASEDIR = File.join(LIB_DIR, '..')
8
+ FONT_DIR = File.join(LIB_DIR, '..', 'fonts')
9
+ MAX_FONT_SIZE= 75
10
+ REPEAT_X_OFFSET = 80
11
+ REPEAT_Y_OFFSET = 80
12
+
13
+ def self.watermark(mark_string, source, destination = nil, options: {})
14
+ default={
15
+ angle: :diagonal,
16
+ margin: [50, 50, 50, 50],
17
+ font: "#{FONT_DIR}/wqyzenhei.ttf",
18
+ font_size: 12,
19
+ font_color: "#999999",
20
+ transparent: 0.2,
21
+ align: :left,
22
+ valign: :center,
23
+ mode: :fill
24
+ }
25
+ options = default.merge(options)
26
+
27
+ source_pdf = CombinePDF.load(source, allow_optional_content: true)
28
+ source_size = page_size(source_pdf.pages[0])
29
+
30
+ options[:source_size] = source_size
31
+ options[:content_width] ||= source_size[0] - (options[:margin][1] + options[:margin][3])
32
+ options[:content_height] ||= source_size[1] - (options[:margin][0] + options[:margin][2])
33
+
34
+ wm = WaterMark.new(mark_string, options).render
35
+ water_mark_pdf = CombinePDF.parse(wm, allow_optional_content: true).pages[0]
36
+
37
+ source_pdf.pages.each do |page|
38
+ page << water_mark_pdf
39
+ end
40
+ if destination.nil?
41
+ source_pdf.to_pdf
42
+ else
43
+ source_pdf.save destination
44
+ end
45
+ end
46
+
47
+ def self.page_size(page)
48
+ mediabox = page.mediabox
49
+ [mediabox[2]-mediabox[0], mediabox[3] - mediabox[1]]
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module PdfWatermark
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,117 @@
1
+ require 'prawn'
2
+ module PdfWatermark
3
+ class WaterMark
4
+ include Prawn::View
5
+
6
+ def initialize(mark_string, options)
7
+ @options = options
8
+ @content_width = options[:content_width]
9
+ @content_height = options[:content_height]
10
+ @angle = options[:angle] == :diagonal ? rad_to_degree(Math.atan(@content_height.to_f/@content_width.to_f)) : options[:angle]
11
+ @mark_string = mark_string
12
+ @font_size = @options[:font_size]
13
+ @font = @options[:font]
14
+ @x = @options[:x] || 0
15
+ @y = @options[:y] || @content_height
16
+
17
+ end
18
+
19
+ def render
20
+ case @options[:mode]
21
+ when :fill
22
+ fill_content
23
+ when :repeat
24
+ repeat_content
25
+ else
26
+ repeat_content
27
+ end
28
+ document.render
29
+ end
30
+
31
+ def fill_content
32
+ @max_text_width = @content_width
33
+
34
+ rad = degree_to_rad(@angle)
35
+ if Math.tan(rad) < @content_height.to_f/@content_width.to_f
36
+ @max_text_width = (@content_width/Math.cos(rad)).abs
37
+ else
38
+ @max_text_width = (@content_height/Math.sin(rad)).abs
39
+ end
40
+ @font_size ||= calculated_font_size(@mark_string, MAX_FONT_SIZE, @max_text_width)
41
+
42
+ font(@options[:font]) do
43
+ bounding_box([@x, @y], width: @content_width, height: @content_height) do
44
+ rotate @angle, origin: [@content_width/2, @content_height/2] do
45
+ transparent(@options[:transparent]) do
46
+ formatted_text_box(
47
+ [{ text: @mark_string, color: @options[:font_color] }],
48
+ at: [@x - (@max_text_width - @content_width) / 2, @y],
49
+ width: @max_text_width, height: @content_height,
50
+ align: @options[:align], valign: @options[:valign],
51
+ size: @font_size,
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ def repeat_content
60
+ font(@font) do
61
+ transparent(@options[:transparent]) do
62
+ box_height = @font_size
63
+ box_width = text_width(@mark_string, @font_size)
64
+ temp_y = @y
65
+ indent = false
66
+ bounding_box([@x, @y], width: @content_width, height: @content_height) do
67
+ while temp_y > 0 do
68
+ rotate @angle, origin: [@content_width/2, @content_height/2] do
69
+ temp_x = indent ? ((box_width + REPEAT_X_OFFSET) / 2) : 0
70
+ while temp_x <= @content_width
71
+ formatted_text_box(
72
+ [{ text: @mark_string, color: @options[:font_color] }],
73
+ at: [temp_x, temp_y],
74
+ width: box_width, height: box_height,
75
+ align: @options[:align], valign: @options[:valign],
76
+ size: @font_size,
77
+ )
78
+ temp_x += box_width + REPEAT_X_OFFSET
79
+ end
80
+ end
81
+ temp_y -= box_height + REPEAT_Y_OFFSET
82
+ indent = !indent
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+
90
+ protected
91
+ def text_width(text, size)
92
+ document.width_of(text, size: size, margin: 0, left_margin: 0, right_margin: 0).ceil
93
+
94
+ end
95
+
96
+ def calculated_font_size(text, size, max)
97
+ text_width = self.text_width(text, size)
98
+ if text_width > max
99
+ (max / text.size).floor
100
+ else
101
+ size
102
+ end
103
+ end
104
+
105
+ def document
106
+ @document ||= Prawn::Document.new(page_size: @options[:source_size], margin: @options[:margin], font: @font)
107
+ end
108
+
109
+ def degree_to_rad(angle)
110
+ angle * Math::PI / 180
111
+ end
112
+
113
+ def rad_to_degree(rad)
114
+ rad * 180 / Math::PI
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pdf_watermark/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pdf_watermark"
8
+ spec.version = PdfWatermark::VERSION
9
+ spec.authors = ["bruce"]
10
+ spec.email = ["shibocuhk@gmail.com"]
11
+
12
+ spec.summary = %q{pdf watermark}
13
+ spec.description = %q{add watermark to pdf files}
14
+ spec.homepage = "http://bruceshi.me"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+ spec.test_files = Dir['spec/*_spec.rb']
25
+
26
+ spec.add_runtime_dependency('prawn', '~> 2.1', '>= 2.1.0')
27
+ spec.add_runtime_dependency('combine_pdf', '~> 0.2.34')
28
+
29
+ spec.add_development_dependency('bundler', '~> 1.10')
30
+ spec.add_development_dependency('rake', '~> 10.0')
31
+ spec.add_development_dependency('rspec', '~> 3.0')
32
+ spec.add_development_dependency('rubocop', '~> 0.47.1')
33
+ spec.add_development_dependency('rubocop-rspec', '~> 1.10')
34
+ spec.add_development_dependency('simplecov')
35
+ end
data/test.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'pdf_watermark'
2
+ require 'ruby-prof'
3
+ RubyProf.start
4
+ PdfWatermark.watermark('Private And Confidential ** For 黄依群 of 基石资本 Only ** Do Not Distribute',
5
+ "test.pdf", "result.pdf", options: { :angle => :diagonal, :font_color => "999999", :transparent => 0.5 })
6
+ result = RubyProf.stop
7
+ printer = RubyProf::FlatPrinter.new(result)
8
+ printer.print(STDOUT)
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdf_watermark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - bruce
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-21 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: '2.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.1.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: combine_pdf
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.2.34
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.2.34
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.10'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.10'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 0.47.1
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 0.47.1
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-rspec
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '1.10'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '1.10'
117
+ - !ruby/object:Gem::Dependency
118
+ name: simplecov
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ description: add watermark to pdf files
132
+ email:
133
+ - shibocuhk@gmail.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".gitignore"
139
+ - ".rspec"
140
+ - ".rubocop.yml"
141
+ - CODE_OF_CONDUCT.md
142
+ - Gemfile
143
+ - LICENSE.txt
144
+ - README.md
145
+ - Rakefile
146
+ - bin/console
147
+ - bin/setup
148
+ - fonts/wqyzenhei.ttf
149
+ - lib/pdf_watermark.rb
150
+ - lib/pdf_watermark/version.rb
151
+ - lib/pdf_watermark/water_mark.rb
152
+ - pdf_watermark.gemspec
153
+ - test.rb
154
+ homepage: http://bruceshi.me
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 2.4.5
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: pdf watermark
178
+ test_files: []