pdf_matcher 1.0.0

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
+ SHA256:
3
+ metadata.gz: 9b370a574cfb2023ac0f80454d526294e3512c3cfe7f99511b56ccd8489eef97
4
+ data.tar.gz: 38bfdff05969e7e072775045f2d909f5917f6896f75ecace56f5f936139e78c9
5
+ SHA512:
6
+ metadata.gz: f0eda7923db1b84f51f4e7588434c24ef2a45adc976c4287e8e63e28fd9050608594a95a94e2d72f9d2f99d887aec54d90435791a2e5b04448774bf675ac2f74
7
+ data.tar.gz: 3a6c2511fa9c6e5946373ba1765317b437ac8d56adebe5bb22e6f60ed7f6b16f6a404926c799c3a632c9908572fd9457c00e5583f41a251035970f7ac1a9fe36
@@ -0,0 +1,35 @@
1
+ name: Test
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ name: Test on ruby ${{ matrix.ruby_version }}
8
+ runs-on: ubuntu-latest
9
+
10
+ if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
11
+
12
+ strategy:
13
+ matrix:
14
+ ruby_version:
15
+ - 2.6
16
+ - 2.7
17
+ - 3.0
18
+
19
+ steps:
20
+ - uses: actions/checkout@v2
21
+
22
+ - uses: hidakatsuya/setup-diff-pdf@v1
23
+ with:
24
+ diff-pdf-version: 0.5
25
+
26
+ - name: Set up Ruby ${{ matrix.ruby_version }}
27
+ uses: ruby/setup-ruby@v1
28
+ with:
29
+ ruby-version: ${{ matrix.ruby_version }}
30
+
31
+ - name: Install dependencies
32
+ run: bundle install --jobs 4 --retry 3
33
+
34
+ - name: Run Tests
35
+ run: bundle exec rake test
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ /.bundle/
2
+ /pkg/
3
+ /tmp/
4
+ /Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## Main (unreleased)
2
+
3
+ ## 1.0.0
4
+
5
+ The first release. See [README.md](README.md) for detailed instructions.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hidakatsuya@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Dockerfile ADDED
@@ -0,0 +1,18 @@
1
+ FROM ruby:3.0.1
2
+
3
+ RUN apt-get update -qq && apt-get install -y build-essential xvfb
4
+
5
+ WORKDIR /tmp
6
+
7
+ RUN apt-get install -y libpoppler-glib-dev poppler-utils libwxgtk3.0-dev && \
8
+ curl -L https://github.com/vslavik/diff-pdf/archive/master.tar.gz -o diff-pdf-master.tar.gz && \
9
+ tar zxf diff-pdf-master.tar.gz && \
10
+ cd diff-pdf-master && \
11
+ ./bootstrap && \
12
+ ./configure && \
13
+ make && make install
14
+
15
+ RUN mkdir /src
16
+
17
+ WORKDIR /src
18
+ VOLUME /src
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'test-unit'
7
+ gem 'test-unit-rr'
8
+ gem 'prawn'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Katsuya Hidaka
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,96 @@
1
+ # PdfMatcher
2
+
3
+ [![Test](https://github.com/hidakatsuya/pdf_matcher/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/hidakatsuya/pdf_matcher/actions/workflows/test.yml)
4
+
5
+ A gem to compare two PDFs and output the differences using [diff-pdf](https://github.com/vslavik/diff-pdf).
6
+
7
+ ## Prerequisites
8
+
9
+ This gem requires [diff-pdf](https://github.com/vslavik/diff-pdf). See [the README.md](https://github.com/vslavik/diff-pdf) for how to install it.
10
+
11
+ Note that you can install it with [hidakatsuya/setup-diff-pdf](https://github.com/hidakatsuya/setup-diff-pdf) in GitHub Action.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'pdf_matcher'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install pdf_matcher
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ require 'pdf_matcher'
33
+
34
+ PdfMatcher.match?(pdf_a, pdf_b) #=> boolean
35
+
36
+ PdfMatcher.match?('/path/to/a.pdf', '/path/to/b.pdf') #=> boolean
37
+ PdfMatcher.match?(Pathname('/path/to/a.pdf'), Pathname('/path/to/b.pdf')) #=> boolean
38
+
39
+ PdfMatcher.match?(pdf_a, pdf_b, output_diff: '/path/to/diff.pdf') #=> boolean
40
+ PdfMatcher.match?(pdf_a, pdf_b, diff_pdf_opts: ['--dpi=300']) #=> boolean
41
+
42
+ result = PdfMatcher.match(
43
+ pdf_a, pdf_b,
44
+ output_diff: nil, # or '/path/to/diff.pdf' or Pathname('/path/to/diff.pdf')
45
+ diff_pdf_opts: nil # or ['--dpi=300']
46
+ )
47
+ result.matched? #=> boolean
48
+
49
+ # Returns nil if pdf data is passed, otherwise returns path as Pathname.
50
+ result.pdf1_path #=> Pathname or nil
51
+ result.pdf2_path #=> Pathname or nil
52
+
53
+ result.pdf1_data #=> "%PDF-..."
54
+ result.pdf2_data #=> "%PDF-..."
55
+
56
+ # Returns nil if the output_diff parameter is nil.
57
+ result.diff_pdf_path #=> Pathname or nil
58
+ result.diff_pdf_data #=> "%PDF-..." or nil
59
+
60
+ PdfMatcher.config.diff_pdf_opts = ['--dpi=300']
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hidakatsuya/pdf_matcher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hidakatsuya/pdf_matcher/blob/master/CODE_OF_CONDUCT.md).
66
+
67
+ ## Testing
68
+
69
+ ```
70
+ $ bundle exec rake test
71
+ ```
72
+
73
+ However, this gem requires diff-pdf. You can install it locally or use a docker container.
74
+
75
+ ```
76
+ $ docker build -t pdf_matcher:latest .
77
+ $ docker run -v $PWD:/src:cached -it pdf_matcher bash
78
+
79
+ > src# bundle install
80
+ > src# bundle exec rake test
81
+ ```
82
+
83
+ Or, you can run tests instantlly like this:
84
+
85
+ ```
86
+ $ docker run --rm -v $PWD:/src -it pdf_matcher bash -c "bundle install && bundle exec rake test"
87
+ ```
88
+
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
93
+
94
+ ## Code of Conduct
95
+
96
+ Everyone interacting in the PdfMatcher project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hidakatsuya/pdf_matcher/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'pdf_matcher/version'
4
+ require_relative 'pdf_matcher/matcher'
5
+
6
+ module PdfMatcher
7
+ Config = Struct.new(
8
+ # Options for diff-pdf.
9
+ # It will be applied as the default options when executing diff-pdf. Default is nil.
10
+ #
11
+ # PdfMatcher.config.diff_pdf_opts = ['--dpi=300', '--grayscale']
12
+ # PdfMatcher.config.diff_pdf_opts = %w(--dpi=300 --grayscale)
13
+ #
14
+ # See `diff-pdf --help` for available options.
15
+ :diff_pdf_opts
16
+ )
17
+ def self.config
18
+ @config ||= Config.new(nil)
19
+ end
20
+
21
+ def self.match?(pdf1, pdf2, output_diff: nil, diff_pdf_opts: nil)
22
+ match(pdf1, pdf2, output_diff: output_diff, diff_pdf_opts: diff_pdf_opts).matched?
23
+ end
24
+
25
+ def self.match(pdf1, pdf2, output_diff: nil, diff_pdf_opts: nil)
26
+ Matcher.new(pdf1, pdf2, output_diff: output_diff, diff_pdf_opts: diff_pdf_opts).match
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+
6
+ module PdfMatcher
7
+ class DiffPdf
8
+ include Singleton
9
+
10
+ class << self
11
+ extend Forwardable
12
+ def_delegator :instance, :exec
13
+ end
14
+
15
+ class CommandNotAvailable < StandardError
16
+ def initialize
17
+ super 'pdf_matcher requires diff-pdf command, but it does not seem to be installed. ' \
18
+ 'Please install it and try again.'
19
+ end
20
+ end
21
+
22
+ def initialize
23
+ verify_available!
24
+ end
25
+
26
+ def exec(pdf1_path, pdf2_path, output_diff: nil, options: nil)
27
+ system("diff-pdf #{build_options(output_diff, options).join(' ')} #{pdf1_path} #{pdf2_path}")
28
+ end
29
+
30
+ private
31
+
32
+ def verify_available!
33
+ raise CommandNotAvailable unless system('which diff-pdf > /dev/null 2>&1')
34
+ end
35
+
36
+ def build_options(output_diff, options)
37
+ (options || []).tap do |opts|
38
+ opts << "--output-diff=#{output_diff}" if output_diff
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module PdfMatcher
6
+ class MatchResult < Struct.new(:matched, :pdf1_file, :pdf2_file, :diff_pdf_path)
7
+ extend Forwardable
8
+
9
+ alias :matched? :matched
10
+
11
+ def_delegator :pdf1_file, :path, :pdf1_path
12
+ def_delegator :pdf1_file, :data, :pdf1_data
13
+ def_delegator :pdf2_file, :path, :pdf2_path
14
+ def_delegator :pdf2_file, :data, :pdf2_data
15
+
16
+ def diff_pdf_data
17
+ @diff_pdf_data ||= diff_pdf_path&.binread
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'diff_pdf'
4
+ require_relative 'pdf_file'
5
+ require_relative 'match_result'
6
+
7
+ module PdfMatcher
8
+ class Matcher
9
+ def initialize(pdf1, pdf2, output_diff: nil, diff_pdf_opts: nil)
10
+ @pdf1 = PdfFile.init(pdf1)
11
+ @pdf2 = PdfFile.init(pdf2)
12
+ @output_diff_path = output_diff ? Pathname(output_diff) : nil
13
+ @diff_pdf_opts = diff_pdf_opts || PdfMatcher.config.diff_pdf_opts
14
+ end
15
+
16
+ def match
17
+ @result ||= MatchResult.new(match_pdfs, pdf1, pdf2, output_diff_path)
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :pdf1, :pdf2, :output_diff_path, :diff_pdf_opts
23
+
24
+ def pdfs
25
+ @pdfs ||= [pdf1, pdf2]
26
+ end
27
+
28
+ def match_pdfs
29
+ open_pdf_files do |pdf1_path, pdf2_path|
30
+ DiffPdf.exec(
31
+ pdf1_path, pdf2_path,
32
+ output_diff: output_diff_path,
33
+ options: diff_pdf_opts
34
+ )
35
+ end
36
+ end
37
+
38
+ def open_pdf_files
39
+ pdfs.each(&:open)
40
+ yield(pdf1.path, pdf2.path)
41
+ ensure
42
+ pdfs.each(&:close)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tempfile'
4
+
5
+ module PdfMatcher
6
+ module PdfFile
7
+ def self.init(path_or_data)
8
+ case path_or_data
9
+ # when a pdf path passed as a Pathname
10
+ when Pathname
11
+ File.new(path_or_data)
12
+ when String
13
+ # when a pdf data passed
14
+ if path_or_data =~ /^%PDF-/
15
+ Tempfile.new(path_or_data)
16
+ # when a pdf path passed
17
+ else
18
+ File.new(path_or_data)
19
+ end
20
+ else
21
+ raise ArgumentError
22
+ end
23
+ end
24
+
25
+ class Base
26
+ attr_reader :data, :path
27
+
28
+ def initialize(data:, path:)
29
+ @data = data
30
+ @path = path
31
+ end
32
+
33
+ def open; end
34
+ def close; end
35
+ end
36
+
37
+ class File < Base
38
+ def initialize(path)
39
+ super(data: nil, path: Pathname(path))
40
+ end
41
+
42
+ def data
43
+ @data ||= path.binread
44
+ end
45
+ end
46
+
47
+ class Tempfile < Base
48
+ def initialize(data)
49
+ super(data: data, path: nil)
50
+ @io = nil
51
+ end
52
+
53
+ def open
54
+ @io ||= ::Tempfile.open { |f| f.puts data; f }
55
+ end
56
+
57
+ def path
58
+ path = @io&.path
59
+ path ? Pathname(path) : nil
60
+ end
61
+
62
+ def close
63
+ @io&.close!
64
+ @io = nil
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdfMatcher
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'lib/pdf_matcher/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'pdf_matcher'
5
+ spec.version = PdfMatcher::VERSION
6
+ spec.authors = ['Katsuya Hidaka']
7
+ spec.email = ['hidakatsuya@gmail.com']
8
+
9
+ spec.summary = 'A gem to compare two PDFs and output the differences using diff-pdf'
10
+ spec.description = 'PdfMatcher is a gem to compare two PDFs and output the differences using diff-pdf'
11
+ spec.homepage = 'https://github.com/hidakatsuya/pdf_matcher'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+ spec.metadata['changelog_uri'] = 'https://github.com/hidakatsuya/pdf_matcher/blob/main/CHANGELOG.md'
18
+
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^test/}) }
21
+ end
22
+ spec.require_paths = ['lib']
23
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdf_matcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Katsuya Hidaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: PdfMatcher is a gem to compare two PDFs and output the differences using
14
+ diff-pdf
15
+ email:
16
+ - hidakatsuya@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/test.yml"
22
+ - ".gitignore"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Dockerfile
26
+ - Gemfile
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - lib/pdf_matcher.rb
31
+ - lib/pdf_matcher/diff_pdf.rb
32
+ - lib/pdf_matcher/match_result.rb
33
+ - lib/pdf_matcher/matcher.rb
34
+ - lib/pdf_matcher/pdf_file.rb
35
+ - lib/pdf_matcher/version.rb
36
+ - pdf_matcher.gemspec
37
+ homepage: https://github.com/hidakatsuya/pdf_matcher
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/hidakatsuya/pdf_matcher
42
+ source_code_uri: https://github.com/hidakatsuya/pdf_matcher
43
+ changelog_uri: https://github.com/hidakatsuya/pdf_matcher/blob/main/CHANGELOG.md
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.6.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.1.6
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: A gem to compare two PDFs and output the differences using diff-pdf
63
+ test_files: []