pdf_tools 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 641f9978e0a51ba3e4453c7f4b793df7963760f86b367b94e50ac7ae9418aaa6
4
+ data.tar.gz: 7201cdfbbaeea72b9009ee59df7f8b4c103b3b4f6f9ff6e4e25677a047440e07
5
+ SHA512:
6
+ metadata.gz: 98261b8ce7aa2a6431c29681c9db10ddc0c3fc9264d6e6c21f40dff4ce3a0cca106d2f42c405acf5e2df785ec5cf3642ce90a9aa8b98ed2b7cc44cb492c2fac3
7
+ data.tar.gz: cc64f066896705b75beec9f51301ba0b4d013be5ae90ffd1ed73471cef0f5cfc5de79e525ff0db3349933aa1f5d5d4e79c5a5e65bc9dadb08a325d4c9fd3c512
@@ -0,0 +1,59 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+
58
+ # rspec failure tracking
59
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.5.3
@@ -0,0 +1,11 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ rvm:
5
+ - 2.5.3
6
+ script:
7
+ - bundle exec rspec
8
+ before_install: gem install bundler -v 2.0.2
9
+ branches:
10
+ only:
11
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pdf_tools.gemspec
4
+ gemspec
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pdf_tools (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ docile (1.3.2)
12
+ method_source (0.9.2)
13
+ pry (0.12.2)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.9.0)
16
+ rake (10.5.0)
17
+ rspec (3.9.0)
18
+ rspec-core (~> 3.9.0)
19
+ rspec-expectations (~> 3.9.0)
20
+ rspec-mocks (~> 3.9.0)
21
+ rspec-core (3.9.1)
22
+ rspec-support (~> 3.9.1)
23
+ rspec-expectations (3.9.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-mocks (3.9.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.9.0)
29
+ rspec-support (3.9.2)
30
+ simplecov (0.18.5)
31
+ docile (~> 1.1)
32
+ simplecov-html (~> 0.11)
33
+ simplecov-html (0.12.2)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (>= 1.17)
40
+ pdf_tools!
41
+ pry
42
+ rake (~> 10.0)
43
+ rspec (~> 3.7)
44
+ simplecov
45
+
46
+ BUNDLED WITH
47
+ 2.0.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Ilia K
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,54 @@
1
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/d402008317c8f6cc6715/test_coverage)](https://codeclimate.com/github/ldrner/pdf_tools/test_coverage)
2
+
3
+ # PdfTools
4
+
5
+ Ruby wrapper for the PDF Tools command-line utils.
6
+ [https://www.pdf-tools.com/](https://www.pdf-tools.com/)
7
+
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'pdf_tools'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install pdf_tools
25
+
26
+ ## Usage
27
+
28
+ Native support for next tools are available:
29
+ * 3-Heights™ PDF Merge Split
30
+ * 3-Heights™ Image to PDF Converter
31
+
32
+ ```ruby
33
+ PdfTools::Tool::ImageToPdf.new do |tool|
34
+ tool.lk "license key"
35
+ tool.f
36
+ tool << "input.tif"
37
+ tool << "output.pdf"
38
+ end
39
+ ```
40
+
41
+ ```ruby
42
+ PdfTools::Tool::MergeSplit.new do |tool|
43
+ tool.lk "license key"
44
+ tool.m
45
+ tool << "in1.pdf"
46
+ tool.ot "Bookmark"
47
+ tool << "in2.pdf"
48
+ tool << "out.pdf"
49
+ end
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ldrner/pdf_tools.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pdf_tools"
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require 'pdf_tools/version'
2
+
3
+ module PdfTools
4
+ class Error < StandardError; end
5
+ end
6
+
7
+ require 'pdf_tools/tool'
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open3'
4
+
5
+ module PdfTools
6
+ # Send commands to the shell
7
+ class Shell
8
+ def run(command, **options)
9
+ stdout, stderr, status = execute(command)
10
+
11
+ if status != 0
12
+ raise PdfTools::Error, "Command `#{command.join(' ')}` failed!\n Error: #{stderr}"
13
+ end
14
+
15
+ $stderr.print(stderr) unless options[:stderr] == false
16
+
17
+ [stdout, stderr, status]
18
+ end
19
+
20
+ def execute(command)
21
+ stdout, stderr, status =
22
+ Open3.popen3(*command) do |stdin, stdout, stderr, wait_thr|
23
+ [stdin, stdout, stderr].each(&:binmode)
24
+ stdout_reader = Thread.new { stdout.read }
25
+ stderr_reader = Thread.new { stderr.read }
26
+
27
+ [stdout_reader.value, stderr_reader.value, wait_thr.value]
28
+ end
29
+
30
+ [stdout, stderr, status.exitstatus]
31
+ rescue Errno::ENOENT, IOError
32
+ ['', "executable not found: \"#{command.first}\"", 127]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pdf_tools/shell'
4
+
5
+ module PdfTools
6
+ # Abstract class that wraps command-line tools.
7
+ #
8
+ # @example
9
+ # PdfTools::Tool::ImageToPdf.new do |tool|
10
+ # tool.lk "license key"
11
+ # tool.f
12
+ # tool << "input.tif"
13
+ # tool << "output.pdf"
14
+ # end
15
+ class Tool
16
+ KEY_VALUE_OPTIONS = %w[i]
17
+
18
+ # Execute the command.
19
+ #
20
+ # @example
21
+ # version = PdfTools::Tool::ImageToPdf.new { |b| b.version }
22
+ # puts version
23
+ #
24
+ # @return [PdfTools::Tool, String] If no block is given, returns an
25
+ # instance of the tool, if block is given, returns the command output.
26
+ def self.new(*args)
27
+ instance = super(*args)
28
+
29
+ if block_given?
30
+ yield instance
31
+ instance.call
32
+ else
33
+ instance
34
+ end
35
+ end
36
+
37
+ attr_reader :name, :args
38
+
39
+ def initialize(name)
40
+ @name = name
41
+ @args = []
42
+ end
43
+
44
+ # Build and execute the command.
45
+ #
46
+ # @example
47
+ # img2pdf = PdfTools::Tool::ImageToPdf.new
48
+ # img2pdf.d(150)
49
+ # img2pdf << "input.tif"
50
+ # img2pdf << "output.pdf"
51
+ # img2pdf.call # executes `img2pdf -d 150 input.tif output.pdf`
52
+ #
53
+ # @example
54
+ # img2pdf = PdfTools::Tool::ImageToPdf.new
55
+ #
56
+ # img2pdf.call do |stdout, stderr, status|
57
+ # # ...
58
+ # end
59
+ #
60
+ # @yield [Array] yields stdout, stderr, exit status (optional)
61
+ #
62
+ # @return [String] the output of the command
63
+ def call(*args)
64
+ options = args[-1].is_a?(Hash) ? args.pop : {}
65
+
66
+ options[:stderr] = false if block_given?
67
+
68
+ shell = PdfTools::Shell.new
69
+ stdout, stderr, status = shell.run(command, options)
70
+ yield stdout, stderr, status if block_given?
71
+
72
+ stdout.chomp("\n")
73
+ end
74
+
75
+ # The built command as array.
76
+ #
77
+ # @return [Array<String>]
78
+ #
79
+ # @example
80
+ # img2pdf = PdfTools::Tool::ImageToPdf.new
81
+ # img2pdf.d(150)
82
+ # img2pdf << "input.tif"
83
+ # img2pdf << "output.pdf"
84
+ # img2pdf.command #=> ["img2pdf", "-d", "150", "input.tif", "output.pdf"]
85
+ def command
86
+ [*name, *args]
87
+ end
88
+
89
+ # Append raw options.
90
+ #
91
+ # @return [self]
92
+ def <<(arg)
93
+ args << arg.to_s
94
+ self
95
+ end
96
+
97
+ # Merge a list of raw options.
98
+ #
99
+ # @return [self]
100
+ def merge!(add_args)
101
+ add_args.each { |arg| self << arg }
102
+ self
103
+ end
104
+
105
+ # Define option methods with key=value args.
106
+ # For Ex. Set Document Information -i <key=value>
107
+ #
108
+ # @example
109
+ # tool = PdfTools::Tool::ImageToPdf.new
110
+ # tool.i(:title => "My Title", :author => "My Name")
111
+ # tool << 'image.jpg'
112
+ # tool << 'output.pdf'
113
+ KEY_VALUE_OPTIONS.each do |option|
114
+ define_method(option.gsub('-', '_')) do |hash = {}|
115
+ hash.flat_map { |k, v| ["-#{option}", [k.capitalize, "\"#{v}\""].join('=')] }
116
+ .each { |el| self << el }
117
+ self
118
+ end
119
+ end
120
+
121
+ # Any missing method will be transformed into a CLI option
122
+ #
123
+ # @example
124
+ # licmgr = PdfTools::Tool.new("licmgr")
125
+ # licmgr.<< "info"
126
+ # licmgr << "your license key"
127
+ # licmgr.command #=> ["licmgr", "info", "your license key"]
128
+ def method_missing(name, *args)
129
+ option = "-#{name.to_s.tr('_', '-')}"
130
+ self << option
131
+ self.merge!(args)
132
+ self
133
+ end
134
+
135
+ def self.available_options
136
+ tool = new
137
+ tool << '-version'
138
+
139
+ help_page = tool.call(stderr: false)
140
+ available_options = help_page.scan(/^\s+-[a-z\-]+/).map(&:strip)
141
+ available_options.map { |o| o[1..-1].tr('-', '_') }
142
+ end
143
+ end
144
+ end
145
+
146
+ require 'pdf_tools/tool/image_to_pdf'
147
+ require 'pdf_tools/tool/merge_split'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdfTools
4
+ class Tool
5
+ # Wrapper for 3-Heights(TM) Image to PDF Converter Shell
6
+ # @see https://www.pdf-tools.com/public/downloads/manuals/Image2PdfShell.pdf
7
+ #
8
+ # Return Codes
9
+ # 0 Success
10
+ # 1 Input File could not be opened or invalid parameters
11
+ # 2 The PDF Output File could not be written
12
+ # 3 Option error
13
+ # 4 OCR error
14
+ # 5 Decode errors
15
+ # 10 License error
16
+ # none Information (stdout) "Done."
17
+ class ImageToPdf < PdfTools::Tool
18
+ TOOL_NAME = 'img2pdf'
19
+
20
+ def initialize(*args)
21
+ super(TOOL_NAME, *args)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdfTools
4
+ class Tool
5
+ # Wrapper for 3-Heights(TM) Split and merge PDF documents Shell
6
+ # @see https://www.pdf-tools.com/public/downloads/manuals/PdfSplitMergeShell.pd
7
+ #
8
+ # Return Codes
9
+ # 0 Success.
10
+ # 1 Couldn't open input file.
11
+ # 2 PDF output file could not be created.
12
+ # 3 Error with given options, e.g. too many parameters.
13
+ # 4 Generic processing error.
14
+ # 10 License error, e.g. invalid license key
15
+ class MergeSplit < PdfTools::Tool
16
+ TOOL_NAME = 'pdfsplmrg'
17
+
18
+ def initialize(*args)
19
+ super(TOOL_NAME, *args)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module PdfTools
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "pdf_tools/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'pdf_tools'
9
+ spec.version = PdfTools::VERSION
10
+ spec.authors = ['Ilya K.']
11
+ spec.email = ['ldrner@gmail.com']
12
+
13
+ spec.summary = 'Ruby wrapper for the PDF Tools command-line utils'
14
+ spec.description = 'Ruby wrapper for the PDF Tools command-line utils. https://www.pdf-tools.com/'
15
+ spec.homepage = 'https://github.com/ldrner/pdf_tools'
16
+ spec.license = 'MIT'
17
+
18
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ # spec.metadata['source_code_uri'] = "TODO: Put your gem's public repo URL here."
22
+ # spec.metadata['changelog_uri'] = "TODO: Put your gem's CHANGELOG.md URL here."
23
+
24
+ spec.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'bin'
27
+
28
+ spec.require_paths = ['lib']
29
+ spec.required_ruby_version = '~> 2.3'
30
+
31
+ spec.add_development_dependency 'bundler', '>= 1.17'
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec', '~> 3.7'
35
+ spec.add_development_dependency 'simplecov'
36
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdf_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ilya K.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Ruby wrapper for the PDF Tools command-line utils. https://www.pdf-tools.com/
84
+ email:
85
+ - ldrner@gmail.com
86
+ executables:
87
+ - console
88
+ - setup
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".ruby-version"
95
+ - ".travis.yml"
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - LICENSE
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - lib/pdf_tools.rb
104
+ - lib/pdf_tools/shell.rb
105
+ - lib/pdf_tools/tool.rb
106
+ - lib/pdf_tools/tool/image_to_pdf.rb
107
+ - lib/pdf_tools/tool/merge_split.rb
108
+ - lib/pdf_tools/version.rb
109
+ - pdf_tools.gemspec
110
+ homepage: https://github.com/ldrner/pdf_tools
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ homepage_uri: https://github.com/ldrner/pdf_tools
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '2.3'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.7.6
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Ruby wrapper for the PDF Tools command-line utils
135
+ test_files: []