gridify_pdf 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5aa5788c76c75bdb565c4cca84180995eac449b3df985f863ba3f3b4c1dd43da
4
+ data.tar.gz: f3f8c87877bc1b6247695a912c3e8f58c96a411383c46205a99897070e09c916
5
+ SHA512:
6
+ metadata.gz: 18cd69f39291031d2ea21dc5937a2677eaeb8be2d2b94b28c18072a909ae77e396349729c4db20e1501f99aa2cc37db5c89f9657e3ac0e9e58950c173d966df4
7
+ data.tar.gz: f6bf1c0f0890fd552a06dd94be2ba3e7582823937b0709e8962cfc9e899ed9edbe27080f2a0fd15c3ab27901181b57b454528bc02758e4e4dbeb298a885d8c92
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gridify_pdf (0.1.0)
5
+ combine_pdf (~> 1.0.0)
6
+ prawn (~> 2.5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ bigdecimal (3.1.8)
12
+ combine_pdf (1.0.26)
13
+ matrix
14
+ ruby-rc4 (>= 0.1.5)
15
+ diff-lcs (1.5.1)
16
+ matrix (0.4.2)
17
+ pdf-core (0.10.0)
18
+ prawn (2.5.0)
19
+ matrix (~> 0.4)
20
+ pdf-core (~> 0.10.0)
21
+ ttfunk (~> 1.8)
22
+ rspec (3.13.0)
23
+ rspec-core (~> 3.13.0)
24
+ rspec-expectations (~> 3.13.0)
25
+ rspec-mocks (~> 3.13.0)
26
+ rspec-core (3.13.1)
27
+ rspec-support (~> 3.13.0)
28
+ rspec-expectations (3.13.3)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.13.0)
31
+ rspec-mocks (3.13.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.13.0)
34
+ rspec-support (3.13.1)
35
+ ruby-rc4 (0.1.5)
36
+ ttfunk (1.8.0)
37
+ bigdecimal (~> 3.1)
38
+
39
+ PLATFORMS
40
+ arm64-darwin-20
41
+
42
+ DEPENDENCIES
43
+ gridify_pdf!
44
+ rspec (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 2.4.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Jonathan Hooper
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,49 @@
1
+ # gridify_pdf
2
+
3
+ This is a tool that I wrote to lay a grid over PDF files.
4
+
5
+ I was working on an app that adds text boxes to PDFs using [Prawn](https://github.com/prawnpdf/prawn) and [CombinePDF](https://github.com/boazsegev/combine_pdf). I got frustrated with the guess-and-check method to find the right coordinates.
6
+
7
+ Given a PDF that looks like this:
8
+
9
+ ![A PDF without a grid overlay](https://raw.githubusercontent.com/jmhooper/gridify_pdf/refs/heads/main/docs/before.png)
10
+
11
+ It will make this:
12
+
13
+ ![A PDF with a grid overlay](https://raw.githubusercontent.com/jmhooper/gridify_pdf/refs/heads/main/docs/after.png)
14
+
15
+ ## Installation
16
+
17
+ This can be installed with RubyGems:
18
+
19
+ ```
20
+ gem install gridify_pdf
21
+ ```
22
+
23
+ ## CLI
24
+
25
+ This is primarily intended to be used as a CLI tool
26
+
27
+ This will read the file at `document.pdf` and output a version with a grid overlay at `gridified_document.pdf`.
28
+
29
+ ```shell
30
+ gridify_pdf document.pdf gridified_document.pdf
31
+ ```
32
+
33
+ You can also have the PDF written to a temporary file and opened in your default PDF viewer:
34
+
35
+ ```shell
36
+ gridify_pdf document.pdf --open
37
+ ```
38
+
39
+ ## Usage in code
40
+
41
+ If, for some reason, you want to use this tool in your own code, that is also possible:
42
+
43
+ ```ruby
44
+ require 'gridify_pdf'
45
+
46
+ input = File.read('input.pdf')
47
+ result = GridifyPdf::Gridifier.new(input).gridify
48
+ File.write('output.pdf', result)
49
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/docs/after.png ADDED
Binary file
data/docs/before.png ADDED
Binary file
data/exe/gridify_pdf ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'tempfile'
5
+
6
+ require_relative '../lib/gridify_pdf'
7
+
8
+ options = GridifyPdf::Options.build_options_with_defaults
9
+
10
+ OptionParser.new do |opts|
11
+ opts.banner = <<~BANNER
12
+ Usage: gridify_pdf [input_file] [output_file] [options]"
13
+
14
+ If no input file is provided input is read from STDIN.
15
+ If no output file is provide output is written to STDOUT.
16
+ BANNER
17
+
18
+ opts.on('--cell-width=CELL_WIDTH', 'Specify the cell width for the overlay grid') do |cell_width|
19
+ options.cell_width = cell_width.to_i
20
+ end
21
+
22
+ opts.on('--open', 'Open the overlay PDF once done') do
23
+ options.open_after_gridification = true
24
+ end
25
+
26
+ opts.on('-h', '--help', 'Print this help') do
27
+ puts opts
28
+ exit
29
+ end
30
+ end.parse!
31
+
32
+ if ARGV[0]
33
+ options.input = File.open(ARGV[0], 'r')
34
+ end
35
+
36
+ if ARGV[1]
37
+ options.output = File.open(ARGV[1], 'w+')
38
+ elsif options.open_after_gridification
39
+ require 'securerandom'
40
+ tmp_file_path = File.join('/tmp', "gridified_pdf_#{SecureRandom.alphanumeric(24)}.pdf")
41
+ options.output = File.open(tmp_file_path, 'w+')
42
+ end
43
+
44
+ result = GridifyPdf::Gridifier.new(
45
+ options.input.read,
46
+ cell_width: options.cell_width,
47
+ ).gridify
48
+ options.output.write(result)
49
+
50
+ if options.open_after_gridification
51
+ case RUBY_PLATFORM
52
+ when /darwin/
53
+ # macOS
54
+ system("open", options.output.path)
55
+ when /linux/
56
+ # Linux
57
+ system("xdg-open", options.output.path)
58
+ when /win32|mingw32|cygwin/
59
+ # Windows
60
+ system("start", options.output.path)
61
+ else
62
+ raise "Unsupported platform: #{RUBY_PLATFORM}"
63
+ end
64
+ end
65
+
66
+ options.input.close unless options.input == STDIN
67
+ options.output.close unless options.output == STDOUT
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GridifyPdf
4
+ class Gridifier
5
+ attr_reader :input, :cell_width
6
+
7
+ def initialize(input, cell_width: 50)
8
+ @input = input
9
+ @cell_width = cell_width
10
+ end
11
+
12
+ def gridify
13
+ output_pdf = CombinePDF.new
14
+ parsed_input.pages.each do |input_page|
15
+ output_pdf << input_page
16
+ end
17
+
18
+ parsed_overlay = CombinePDF.parse(overlay_document.render)
19
+ parsed_overlay.pages.slice(1..).each_with_index do |overlay_page, index|
20
+ output_pdf.pages[index] << overlay_page
21
+ end
22
+
23
+ output_pdf.to_pdf
24
+ end
25
+
26
+ private
27
+
28
+ def overlay_document
29
+ @overlay_document ||= Prawn::Document.new(margin: 0).tap do |pdf|
30
+ parsed_input.pages.each do |input_page|
31
+ media_box = input_page[:MediaBox]
32
+ page_width = media_box[2] - media_box[0]
33
+ page_height = media_box[3] - media_box[1]
34
+
35
+ pdf.start_new_page(size: [page_width, page_height])
36
+
37
+ (0..(page_height + cell_width)).step(cell_width) do |y|
38
+ (0..(page_width + cell_width)).step(cell_width) do |x|
39
+ pdf.dash(1, space: 9)
40
+ pdf.stroke_rectangle [x, y], cell_width, cell_width
41
+ pdf.draw_text "(#{x}, #{y})", at: [x + 5, y - 10], size: 5
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def parsed_input
49
+ @parsed_input ||= CombinePDF.parse(input)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GridifyPdf
4
+ Options = Struct.new(
5
+ :cell_width,
6
+ :input,
7
+ :output,
8
+ :open_after_gridification,
9
+ keyword_init: true,
10
+ ) do
11
+ def self.build_options_with_defaults(
12
+ input: STDIN,
13
+ output: STDOUT,
14
+ cell_width: 50,
15
+ open_after_gridification: false
16
+ )
17
+ self.new(input:, output:, cell_width:, open_after_gridification:)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GridifyPdf
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gridify_pdf/version"
4
+
5
+ require 'prawn'
6
+ require 'combine_pdf'
7
+
8
+ require_relative 'gridify_pdf/gridifier'
9
+ require_relative 'gridify_pdf/options'
10
+
11
+ module GridifyPdf
12
+ end
@@ -0,0 +1,4 @@
1
+ module GridifyPdf
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/tmp/.keep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gridify_pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Hooper
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-24 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.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: combine_pdf
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ description:
42
+ email:
43
+ - jon9820@gmail.com
44
+ executables:
45
+ - gridify_pdf
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".rspec"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - docs/after.png
56
+ - docs/before.png
57
+ - exe/gridify_pdf
58
+ - lib/gridify_pdf.rb
59
+ - lib/gridify_pdf/gridifier.rb
60
+ - lib/gridify_pdf/options.rb
61
+ - lib/gridify_pdf/version.rb
62
+ - sig/gridify_pdf.rbs
63
+ - tmp/.keep
64
+ homepage: https://gihub.com/jmhooper/gridify_pdf
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ homepage_uri: https://gihub.com/jmhooper/gridify_pdf
69
+ source_code_uri: https://gihub.com/jmhooper/gridify_pdf
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 3.0.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.4.5
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Add gridlines to PDF documents
89
+ test_files: []