pdf_margins 0.0.1
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 +15 -0
- data/README.md +19 -0
- data/Rakefile +20 -0
- data/lib/pdf/margins/checker.rb +102 -0
- data/lib/pdf/margins/issue.rb +5 -0
- data/lib/pdf/margins/version.rb +5 -0
- data/lib/pdf/margins.rb +1 -0
- data/test/checker_test.rb +35 -0
- data/test/pdfs/bottom-margin-only.pdf +0 -0
- data/test/pdfs/clear-margins.pdf +0 -0
- data/test/pdfs/left-margin-only.pdf +0 -0
- data/test/pdfs/p2-left-margin-only.pdf +0 -0
- data/test/pdfs/right-margin-only.pdf +0 -0
- data/test/pdfs/top-margin-only.pdf +0 -0
- data/test/test_helper.rb +10 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
M2NiOWYxMjI0NzQ3ZmMxY2VjYTE3NGIzYTdjOTUyODgxZGMwZWI4ZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWE2NTdkODkxNzc5OTI2NGMwZDMyMjc4NzM1MGJlODFlNmE0OWFkYg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ODY4NjBlYTRjOTA1NGY2MWU5YTdjMjAxNmU4Mzg1ZjNmYzFhODgwZDkyMDJl
|
10
|
+
MDgwZmFmYTc1YTI1N2RkOTZjMzNlNmU2ODBiMGIyZjlhMmJkZTA4NmU1YmVh
|
11
|
+
ZDg3ZDliMzkwNGM4NzQ1OWMyM2U3NGVhMDdhNGZmZDQ1ZGNkYmI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OTM4YzMxYzdhNjZmY2FkOGQ0MTZhMGI5NGVmZDM3ZWQwNzI0NzQ0Y2FkZjVk
|
14
|
+
NzY4NjQ3NmJmY2VlNGViMjc4MDFiNzUzY2FkOTViZDc1MmVlZDI4YWMwYWI1
|
15
|
+
NjU4OWUxZGFiYjUwNDRlZGM2OWRmMTlmNDUwY2U2NWYyMzM5YWU=
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
pdf/margins
|
2
|
+
==
|
3
|
+
|
4
|
+
Simple Ruby library to check a PDF for clear margin areas, by rendering to
|
5
|
+
a CMYK bitmap, ensuring that the pixel area defined by each margin is empty.
|
6
|
+
It's pretty slow, but this is a lot easier than attempting to work out which
|
7
|
+
object in a PDF is rendering into the margin area.
|
8
|
+
|
9
|
+
Depends on RMagick, and thus requires a Ruby interpreter that supports
|
10
|
+
C extensions, such as MRI.
|
11
|
+
|
12
|
+
Written by [Tom Taylor](http://scraplab.net), [Newspaper Club](http://www.newspaperclub.com).
|
13
|
+
|
14
|
+
Example
|
15
|
+
--
|
16
|
+
|
17
|
+
# measurements in mm, ordered top, right, bottom, left (same as CSS)
|
18
|
+
checker = PDF::Margins::Checker.new('example.pdf', 10, 10, 10, 10)
|
19
|
+
puts checker.issues.inspect
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
Bundler::GemHelper.install_tasks
|
10
|
+
|
11
|
+
require 'rake/testtask'
|
12
|
+
|
13
|
+
Rake::TestTask.new(:test) do |t|
|
14
|
+
t.libs << 'lib'
|
15
|
+
t.libs << 'test'
|
16
|
+
t.pattern = 'test/**/*_test.rb'
|
17
|
+
t.verbose = false
|
18
|
+
end
|
19
|
+
|
20
|
+
task :default => :test
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'pdf/margins/issue'
|
2
|
+
require 'RMagick'
|
3
|
+
|
4
|
+
module PDF
|
5
|
+
module Margins
|
6
|
+
class Checker
|
7
|
+
|
8
|
+
MM_TO_PTS = 2.83464567
|
9
|
+
DEFAULT_RESOLUTION = 72
|
10
|
+
# If we find that we need higher resolution for better precision then we
|
11
|
+
# can adjust the SCALE_MULTIPLIER at the cost of speed.
|
12
|
+
SCALE_MULTIPLIER = 1
|
13
|
+
RESOLUTION = DEFAULT_RESOLUTION * SCALE_MULTIPLIER
|
14
|
+
|
15
|
+
attr_reader :file, :top_margin, :right_margin, :bottom_margin, :left_margin
|
16
|
+
|
17
|
+
# Dimensions are in mm, to be converted to PDF points later.
|
18
|
+
def initialize(file, top_margin, right_margin, bottom_margin, left_margin)
|
19
|
+
@file = file
|
20
|
+
@top_margin = top_margin
|
21
|
+
@right_margin = right_margin
|
22
|
+
@bottom_margin = bottom_margin
|
23
|
+
@left_margin = left_margin
|
24
|
+
end
|
25
|
+
|
26
|
+
def issues
|
27
|
+
image_list = Magick::Image.read(file) do
|
28
|
+
# Force CMYK colorspace, because ImageMagick autodetects PDF colorspace
|
29
|
+
# depending on the contents of the file. By forcing the colorspace we
|
30
|
+
# ensure that the subsequent checks in the `dirty_pixels?` method work.
|
31
|
+
self.colorspace = Magick::CMYKColorspace
|
32
|
+
self.density = RESOLUTION
|
33
|
+
self.antialias = false
|
34
|
+
end
|
35
|
+
|
36
|
+
image_list.each_with_index.map do |image, index|
|
37
|
+
|
38
|
+
page_number = index + 1
|
39
|
+
[].tap do |page_issues|
|
40
|
+
if dirty_pixels?(top_pixels(image, top_margin))
|
41
|
+
page_issues << Issue.new(page_number, :top)
|
42
|
+
end
|
43
|
+
|
44
|
+
if dirty_pixels?(bottom_pixels(image, bottom_margin))
|
45
|
+
page_issues << Issue.new(page_number, :bottom)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Newspaper Club assumes that pages are two up, printed as spreads,
|
49
|
+
# so we only check right margins on odd numbered pages, and left
|
50
|
+
# margins on even numbered pages. This should probably be
|
51
|
+
# a configurable option for other types of printing.
|
52
|
+
if page_number % 2 == 0
|
53
|
+
if dirty_pixels?(left_pixels(image, left_margin))
|
54
|
+
page_issues << Issue.new(page_number, :left)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
if dirty_pixels?(right_pixels(image, right_margin))
|
58
|
+
page_issues << Issue.new(page_number, :right)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end.flatten
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def mm_to_pixels(mm)
|
68
|
+
(mm * MM_TO_PTS * SCALE_MULTIPLIER).floor
|
69
|
+
end
|
70
|
+
|
71
|
+
def top_pixels(image, mm)
|
72
|
+
width_px = mm_to_pixels(mm)
|
73
|
+
image.get_pixels(0, 0, image.columns, width_px)
|
74
|
+
end
|
75
|
+
|
76
|
+
def left_pixels(image, mm)
|
77
|
+
width_px = mm_to_pixels(mm)
|
78
|
+
image.get_pixels(0, 0, width_px, image.rows)
|
79
|
+
end
|
80
|
+
|
81
|
+
def right_pixels(image, mm)
|
82
|
+
width_px = mm_to_pixels(mm)
|
83
|
+
x = image.columns - width_px
|
84
|
+
image.get_pixels(x, 0, width_px, image.rows)
|
85
|
+
end
|
86
|
+
|
87
|
+
def bottom_pixels(image, mm)
|
88
|
+
width_px = mm_to_pixels(mm)
|
89
|
+
y = image.rows - width_px
|
90
|
+
image.get_pixels(0, y, image.columns, width_px)
|
91
|
+
end
|
92
|
+
|
93
|
+
def dirty_pixels?(pixels)
|
94
|
+
pixels.any? do |p|
|
95
|
+
(p.cyan | p.magenta | p.yellow | p.black) > 0
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
data/lib/pdf/margins.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'pdf/margins/checker'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CheckerTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
test "PDF with clear margins" do
|
6
|
+
checker = PDF::Margins::Checker.new(pdf_path('clear-margins.pdf'), 10, 10, 10, 10)
|
7
|
+
assert_equal [], checker.issues
|
8
|
+
end
|
9
|
+
|
10
|
+
test "PDF with dirty left margin on page 1" do
|
11
|
+
checker = PDF::Margins::Checker.new(pdf_path('left-margin-only.pdf'), 10, 10, 10, 10)
|
12
|
+
assert_equal [], checker.issues
|
13
|
+
end
|
14
|
+
|
15
|
+
test "PDF with dirty left margin on page 2" do
|
16
|
+
checker = PDF::Margins::Checker.new(pdf_path('p2-left-margin-only.pdf'), 10, 10, 10, 10)
|
17
|
+
assert_equal [PDF::Margins::Issue.new(2, :left)], checker.issues
|
18
|
+
end
|
19
|
+
|
20
|
+
test "PDF with dirty right margin on page 1" do
|
21
|
+
checker = PDF::Margins::Checker.new(pdf_path('right-margin-only.pdf'), 10, 10, 10, 10)
|
22
|
+
assert_equal [PDF::Margins::Issue.new(1, :right)], checker.issues
|
23
|
+
end
|
24
|
+
|
25
|
+
test "PDF with dirty top margin on page 1" do
|
26
|
+
checker = PDF::Margins::Checker.new(pdf_path('top-margin-only.pdf'), 10, 10, 10, 10)
|
27
|
+
assert_equal [PDF::Margins::Issue.new(1, :top)], checker.issues
|
28
|
+
end
|
29
|
+
|
30
|
+
test "PDF with dirty bottom margin on page 1" do
|
31
|
+
checker = PDF::Margins::Checker.new(pdf_path('bottom-margin-only.pdf'), 10, 10, 10, 10)
|
32
|
+
assert_equal [PDF::Margins::Issue.new(1, :bottom)], checker.issues
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pdf_margins
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Taylor
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rmagick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.13.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.13.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- tom@newspaperclub.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- lib/pdf/margins/checker.rb
|
63
|
+
- lib/pdf/margins/issue.rb
|
64
|
+
- lib/pdf/margins/version.rb
|
65
|
+
- lib/pdf/margins.rb
|
66
|
+
- Rakefile
|
67
|
+
- README.md
|
68
|
+
- test/checker_test.rb
|
69
|
+
- test/pdfs/bottom-margin-only.pdf
|
70
|
+
- test/pdfs/clear-margins.pdf
|
71
|
+
- test/pdfs/left-margin-only.pdf
|
72
|
+
- test/pdfs/p2-left-margin-only.pdf
|
73
|
+
- test/pdfs/right-margin-only.pdf
|
74
|
+
- test/pdfs/top-margin-only.pdf
|
75
|
+
- test/test_helper.rb
|
76
|
+
homepage: http://www.newspaperclub.com
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.0.3
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Simple library to checks whether the margins are clear in a PDF
|
100
|
+
test_files:
|
101
|
+
- test/checker_test.rb
|
102
|
+
- test/pdfs/bottom-margin-only.pdf
|
103
|
+
- test/pdfs/clear-margins.pdf
|
104
|
+
- test/pdfs/left-margin-only.pdf
|
105
|
+
- test/pdfs/p2-left-margin-only.pdf
|
106
|
+
- test/pdfs/right-margin-only.pdf
|
107
|
+
- test/pdfs/top-margin-only.pdf
|
108
|
+
- test/test_helper.rb
|