Skip to content
Browse files

Moved to a Grim module with two classes, Pdf and Page. You can still …

…call Grim.new to create an instance of Grim::Pdf. Changed API for selecting a page to [1].
  • Loading branch information...
1 parent 046ff54 commit 2f6948d34394868800c5ad2ea834ae37afac3983 @jonmagic jonmagic committed Sep 6, 2011
Showing with 221 additions and 164 deletions.
  1. +16 −6 README.textile
  2. +17 −81 lib/grim.rb
  3. +44 −0 lib/grim/page.rb
  4. +44 −0 lib/grim/pdf.rb
  5. +55 −0 spec/lib/grim/page_spec.rb
  6. +36 −0 spec/lib/grim/pdf_spec.rb
  7. +9 −77 spec/lib/grim_spec.rb
View
22 README.textile
@@ -4,16 +4,26 @@ Grim is a simple gem for extracting (reaping) a page from a pdf and converting i
h2. Prerequisites
-You will need ghostscript, imagemagick, and xpdf installed. On the Mac (OSX) I highly recommend using "Homebrew":http://mxcl.github.com/homebrew/ to get them installed, its as simple as "brew install ghostscript imagemagick xpdf".
+You will need ghostscript, imagemagick, and xpdf installed. On the Mac (OSX) I highly recommend using "Homebrew":http://mxcl.github.com/homebrew/ to get them installed.
+
+<pre><code>
+ brew install ghostscript imagemagick xpdf
+</code></pre>
+
+h2. Installation
+
+<pre><code>
+ gem install grim
+</code></pre>
h2. Usage
<pre><code>
- instance = Grim.new("/path/to/pdf")
- page_count = instance.page_count # returns the number of pages in the pdf
- png = instance.page(1).to_image("/path/to/save/image.png") # saves png to path and returns File instance
- jpeg = instance.page(2).to_image("/path/to/save/image.jpeg") # saves jpeg to path and returns File instance
- text = instance.page(3).text # returns text as a string
+ pdf = Grim.new("/path/to/pdf")
+ count = pdf.count # returns the number of pages in the pdf
+ png = pdf[1].png # returns png as Tempfile instance
+ jpeg = pdf[2].jpeg("/path/...") # saves jpeg to path and returns File instance
+ text = pdf[3].text # returns text as a String
</pre></code>
h2. License
View
98 lib/grim.rb
@@ -3,13 +3,13 @@
#
# For example:
#
-# instance = Grim.new("/path/to/pdf")
-# page_count = instance.page_count
-# png = instance.page(1).to_image("/path/to/save/image.png")
-# jpeg = instance.page(2).to_image("/path/to/save/image.jpeg")
-# text = instance.page(3).text
+# pdf = Grim.new("/path/to/pdf")
+# count = pdf.count
+# png = pdf[1].image("/path/to/save/image.png")
+# jpeg = pdf[2].image("/path/to/save/image.jpeg")
+# text = pdf[3].text
#
-class Grim
+module Grim
# VERSION
VERSION = "0.1.0"
@@ -30,88 +30,24 @@ class Exception < ::StandardError
class PdfNotFound < Grim::Exception
end
- # be able to store what page instance should focus on
- attr_accessor :page_number
-
- # initialize is called when a new instance is created and accepts path.
- #
- # path - A String or Path to the pdf
- def initialize(path)
- raise Grim::PdfNotFound unless File.exists?(path)
- @page_number = 1
- @path = path
+ # Exception that is raised if pdf does not have page
+ class PageNotFound < Grim::Exception
end
- # Shells out to ghostscript to read the pdf with the pdf_info.ps script
- # as a filter, returning the number of pages in the pdf as an integer.
- #
- # For example:
+ # Creates and returns a new instance of Grim::Pdf
#
- # instance.page_count
- # # => 4
- #
- # Returns an Integer.
- def count
- @count ||= begin
- `gs -dNODISPLAY -q -sFile=#{@path} ./lib/pdf_info.ps`.to_i
- end
- end
-
- # Sets the page attribute on the instance.
- #
- # number - An Integer of the page number to extract
+ # path - a path string or object
#
# For example:
#
- # instance.page(1)
- # # => self
- #
- # Returns self.
- def page(number)
- @page_number = number
- self
- end
-
- # Page number minus 1
- #
- # For example:
+ # pdf = Grim.new(/path/to/pdf)
#
- # instance.page(2)
- # instance.index
- # # => 1
+ # Returns an instance of Grim::Pdf
#
- # Returns an Integer.
- def index
- @page_number - 1
+ def self.new(path)
+ Grim::Pdf.new(path)
end
+end
- # Extracts the selected page and turns it into an image.
- # Tested on png and jpeg.
- #
- # path - A String or Path to save image to
- #
- # For example:
- #
- # instance.page(2).to_image(/path/to/save/image)
- # # => File
- #
- # Returns a File.
- def to_image(path)
- `convert -resize #{Grim::WIDTH} -antialias -render -quality #{Grim::QUALITY} -colorspace RGB -interlace none -density #{Grim::DENSITY} #{@path}[#{index}] #{path}`
- file = File.open(path)
- file.rewind
- file
- end
-
- # Extracts the text from the selected page.
- #
- # For example:
- #
- # instance.page(2).text
- # # => "This is text from slide 2.\n\nAnd even more text from slide 2."
- #
- # Returns a String.
- def text
- `pdftotext -enc UTF-8 -f #{@page_number} -l #{@page_number} #{@path} -`
- end
-end
+require 'grim/pdf'
+require 'grim/page'
View
44 lib/grim/page.rb
@@ -0,0 +1,44 @@
+module Grim
+ class Page
+ # Sets up some instance variables on new instance.
+ #
+ # pdf - the pdf this page belongs to
+ # index - the index of the page in the array of pages
+ #
+ def initialize(pdf, index)
+ @pdf = pdf
+ @index = index
+ @page = index + 1
+ end
+
+ # Extracts the selected page and turns it into an image.
+ # Tested on png and jpeg.
+ #
+ # path - A String or Path to save image to
+ #
+ # For example:
+ #
+ # instance.page(2).image(/path/to/save/image)
+ # # => File
+ #
+ # Returns a File.
+ def image(path)
+ `convert -resize #{Grim::WIDTH} -antialias -render -quality #{Grim::QUALITY} -colorspace RGB -interlace none -density #{Grim::DENSITY} #{@pdf.path}[#{@index}] #{path}`
+ file = File.open(path)
+ file.rewind
+ file
+ end
+
+ # Extracts the text from the selected page.
+ #
+ # For example:
+ #
+ # instance.page(2).text
+ # # => "This is text from slide 2.\n\nAnd even more text from slide 2."
+ #
+ # Returns a String.
+ def text
+ `pdftotext -enc UTF-8 -f #{@page} -l #{@page} #{@pdf.path} -`
+ end
+ end
+end
View
44 lib/grim/pdf.rb
@@ -0,0 +1,44 @@
+module Grim
+ class Pdf
+ attr_accessor :path
+
+ # Raises an error if pdf not found and sets some instance
+ # variables if pdf is found.
+ #
+ # path - A String or Path to the pdf
+ def initialize(path)
+ raise Grim::PdfNotFound unless File.exists?(path)
+ @path = path
+ end
+
+ # Shells out to ghostscript to read the pdf with the pdf_info.ps script
+ # as a filter, returning the number of pages in the pdf as an integer.
+ #
+ # For example:
+ #
+ # pdf.count
+ # # => 4
+ #
+ # Returns an Integer.
+ def count
+ @count ||= begin
+ `gs -dNODISPLAY -q -sFile=#{@path} ./lib/pdf_info.ps`.to_i
+ end
+ end
+
+ # Creates an instance Grim::Page for the index passed in.
+ #
+ # index - accepts Integer for position in array
+ #
+ # For example:
+ #
+ # pdf[4] # returns 5th page
+ #
+ # Returns an instance of Grim::Page.
+ #
+ def [](index)
+ raise Grim::PageNotFound unless index >= 0 && index < count
+ Grim::Page.new(self, index)
+ end
+ end
+end
View
55 spec/lib/grim/page_spec.rb
@@ -0,0 +1,55 @@
+require 'fileutils'
+require 'spec_helper'
+
+describe Grim::Page do
+ after(:all) do
+ FileUtils.rm_rf(tmp_dir)
+ end
+
+ describe "#image" do
+ describe "output png" do
+ before(:all) do
+ pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
+ @png = pdf[0].image(tmp_path("to_png_spec.png"))
+ end
+
+ it "should create the file" do
+ File.exist?(tmp_path("to_png_spec.png")).should be_true
+ end
+
+ it "should return an pdf of File" do
+ @png.class.should == File
+ end
+
+ it "should have the right file size" do
+ @png.stat.size.should == 188515
+ end
+ end
+
+ describe "output jpeg" do
+ before(:all) do
+ pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
+ @jpeg = pdf[0].image(tmp_path("to_jpeg_spec.jpeg"))
+ end
+
+ it "should create the file" do
+ File.exist?(tmp_path("to_jpeg_spec.jpeg")).should be_true
+ end
+
+ it "should return an pdf of File" do
+ @jpeg.class.should == File
+ end
+
+ it "should have the right file size" do
+ @jpeg.stat.size.should == 53980
+ end
+ end
+ end
+
+ describe "#text" do
+ it "should return the text from the selected page" do
+ pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
+ pdf[1].text.should == "Step 1: get someone to print this curve for you to scale, 72\342\200\235 wide\n\nStep 2: Get a couple 55 gallon drums\n\n\f"
+ end
+ end
+end
View
36 spec/lib/grim/pdf_spec.rb
@@ -0,0 +1,36 @@
+require 'spec_helper'
+
+describe Grim::Pdf do
+ describe "#initialize" do
+ it "should raise an error if pdf does not exist" do
+ lambda { Grim.new(fixture_path("booboo.pdf")) }.should raise_error(Grim::PdfNotFound)
+ end
+
+ it "should set path on pdf" do
+ pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
+ pdf.path.should == fixture_path("smoker.pdf")
+ end
+ end
+
+ describe "#count" do
+ it "should return 25" do
+ pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
+ pdf.count.should == 25
+ end
+ end
+
+ describe "#[]" do
+ before(:each) do
+ @pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
+ end
+
+ it "should raise Grim::PageDoesNotExist if page doesn't exist" do
+ lambda { @pdf[25] }.should raise_error(Grim::PageNotFound)
+ end
+
+ it "should return an instance of Grim::Page if page exists" do
+ @pdf[24].class.should == Grim::Page
+ end
+ end
+
+end
View
86 spec/lib/grim_spec.rb
@@ -1,93 +1,25 @@
-require 'fileutils'
require 'spec_helper'
describe Grim do
- after(:all) do
- FileUtils.rm_rf(tmp_dir)
- end
-
it "should have a VERSION constant" do
Grim.const_defined?('VERSION').should be_true
end
- describe "#initialize" do
- it "should raise an error if pdf does not exist" do
- lambda { Grim.new(fixture_path("booboo.pdf")) }.should raise_error(Grim::PdfNotFound)
- end
+ it "should have WIDTH constant set to 1024" do
+ Grim::WIDTH.should == 1024
end
- describe "#count" do
- it "should return 25" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.count.should == 25
- end
+ it "should have QUALITY constant set to 90" do
+ Grim::QUALITY.should == 90
end
- describe "#page" do
- it "should be set to 1 by default" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page_number.should == 1
- end
-
- it "should set page attribute and return instance" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page(2).should == instance
- instance.page_number.should == 2
- end
- end
-
- describe "#index" do
- it "should return page minus 1" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page(2)
- instance.index.should == 1
- end
- end
-
- describe "#to_image" do
- describe "output png" do
- before(:all) do
- instance = Grim.new(fixture_path("smoker.pdf"))
- @png = instance.to_image(tmp_path("to_png_spec.png"))
- end
-
- it "should create the file" do
- File.exist?(tmp_path("to_png_spec.png")).should be_true
- end
-
- it "should return an instance of File" do
- @png.class.should == File
- end
-
- it "should have the right file size" do
- @png.stat.size.should == 188515
- end
- end
-
- describe "output jpeg" do
- before(:all) do
- instance = Grim.new(fixture_path("smoker.pdf"))
- @jpeg = instance.to_image(tmp_path("to_jpeg_spec.jpeg"))
- end
-
- it "should create the file" do
- File.exist?(tmp_path("to_jpeg_spec.jpeg")).should be_true
- end
-
- it "should return an instance of File" do
- @jpeg.class.should == File
- end
-
- it "should have the right file size" do
- @jpeg.stat.size.should == 53980
- end
- end
+ it "should have DENSITY constant set to 300" do
+ Grim::DENSITY.should == 300
end
- describe "#text" do
- it "should return the text from the selected page" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page(2).text.should == "Step 1: get someone to print this curve for you to scale, 72\342\200\235 wide\n\nStep 2: Get a couple 55 gallon drums\n\n\f"
+ describe "#new" do
+ it "should return an instance of Grim::Pdf" do
+ Grim.new(fixture_path("smoker.pdf")).class.should == Grim::Pdf
end
end
end

0 comments on commit 2f6948d

Please sign in to comment.
Something went wrong with that request. Please try again.