Permalink
Browse files

init commit

  • Loading branch information...
0 parents commit 68470112c03ace5350f0f00799aca52c3fd09318 @siuying committed Jun 27, 2010
Showing with 186 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +5 −0 LICENSE
  3. +8 −0 Manifest
  4. +45 −0 README.md
  5. +9 −0 Rakefile
  6. BIN bin/wkhtmltoimage-linux-i386-0.10.0
  7. +21 −0 bin/wkhtmltoimage-proxy
  8. +2 −0 lib/websnap.rb
  9. +94 −0 lib/websnap/websnap.rb
@@ -0,0 +1,2 @@
+pkg
+.DS_Store
@@ -0,0 +1,5 @@
+Copyright (C) 2010 Francis Chong
+
+GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
+This is free software: you are free to change and redistribute it. There is NO
+WARRANTY, to the extent permitted by law.
@@ -0,0 +1,8 @@
+LICENSE
+Manifest
+README.md
+Rakefile
+bin/wkhtmltoimage-linux-i386-0.10.0
+bin/wkhtmltoimage-proxy
+lib/websnap.rb
+lib/websnap/websnap.rb
@@ -0,0 +1,45 @@
+# WebSnap
+
+Create snapshot of webpage. Uses [wkhtmltoimage](http://github.com/antialize/wkhtmltopdf) on the backend which renders HTML using Webkit.
+
+## Installation
+
+1. Install wkhtmltoimage (optional)
+** WebSnap comes bundled with wkhtmltoimage binaries for Linux i386 and OS X i386
+** WebSnap defaults to user installed versions of wkhtmltoimage if found
+** Installing wkhtmltoimage binary
+*** Download the latest binary from http://code.google.com/p/wkhtmltopdf/downloads/list
+*** Place the binary somewhere on your path (e.g /usr/local/bin)
+2. Install WebSnap
+
+ $ gem install websnap
+
+## Usage
+
+ # WebSnap.new takes the HTML and any options for wkhtmltoimage
+ # run `wkhtmltoimage -H` for a full list of options
+ snap = WebSnap.new('http://google.com', :format => 'png')
+
+ # Get the binary image data
+ png = snap.bytes
+
+ # Save the PDF to a file
+ file = snap.to_file('/path/to/save/png')
+
+ # WebSnap.new can optionally accept a URL or a File.
+ kit = WebSnap.new('http://google.com')
+ kit = WebSnap.new(File.new('/path/to/html'))
+
+## Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+## Copyright
+
+Copyright (c) 2010 Francis Chong. See LICENSE for details.
@@ -0,0 +1,9 @@
+require 'echoe'
+
+Echoe.new("websnap", "0.1.0") do |p|
+ p.author = "Francis Chong"
+ p.description = "Create snapshot of webpage"
+ p.url = "http://github.com/siuying/websnap"
+ p.ignore_pattern = []
+ p.runtime_dependencies = []
+end
Binary file not shown.
@@ -0,0 +1,21 @@
+#!/usr/bin/env ruby
+
+executable = `which wkhtmltoimage`.chomp
+
+if executable.empty?
+
+ binary = case RUBY_PLATFORM
+ when /linux/
+ 'wkhtmltoimage-linux-i386-0.10.0'
+ when /darwin/
+ 'wkhtmltoimage-osx-i386-0.10.0'
+ else
+ raise "No bundled binaries found for your system. Please install wkhtmltopdf."
+ end
+ executable = File.join(File.dirname(__FILE__), binary)
+
+end
+
+arguments = $*.map { |arg| arg.include?(' ') ? %Q("#{arg}") : arg }
+
+system(executable + " " + arguments.join(" "))
@@ -0,0 +1,2 @@
+require 'rubygems'
+require 'websnap/websnap'
@@ -0,0 +1,94 @@
+class WebSnap
+
+ class NoExecutableError < StandardError
+ def initialize
+ super('Could not locate wkhtmltoimage-proxy executable')
+ end
+ end
+
+ class ImproperSourceError < StandardError
+ def initialize(msg)
+ super("Improper Source: #{msg}")
+ end
+ end
+
+ attr_accessor :source, :stylesheets
+ attr_reader :options
+
+ def initialize(url_file_or_html, options={})
+ @source = Source.new(url_file_or_html)
+
+ @stylesheets = []
+
+ default_options = {
+ :'crop-w' => '1024',
+ :'crop-h' => '768',
+ :'scale-w' => '120',
+ :'scale-h' => '90',
+ :'format' => 'png'
+ }
+ @options = normalize_options(default_options.merge(options))
+
+ raise NoExecutableError.new if wkhtmltoimage.nil? || wkhtmltoimage == ''
+ end
+
+ def command
+ args = [wkhtmltoimage]
+ args += @options.to_a.flatten.compact
+ args << '--quiet'
+
+ if @source.html?
+ args << '-' # Get HTML from stdin
+ else
+ args << @source.to_s
+ end
+
+ args << '-' # Read PDF from stdout
+ args.join(' ')
+ end
+
+ def to_bytes
+ img = IO.popen(command, "w+")
+ img.puts(@source.to_s) if @source.html?
+ img.close_write
+ result = img.gets(nil)
+ img.close_read
+ return result
+ end
+
+ def to_file(path)
+ File.open(path,'w') {|file| file << self.to_pdf}
+ end
+
+ protected
+
+ def wkhtmltopdf
+ @wkhtmltoimage ||= `which wkhtmltoimage-proxy`.chomp
+ end
+
+ def normalize_options(options)
+ normalized_options = {}
+ options.each do |key, value|
+ next if !value
+ normalized_key = "--#{normalize_arg key}"
+ normalized_options[normalized_key] = normalize_value(value)
+ end
+ normalized_options
+ end
+
+ def normalize_arg(arg)
+ arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
+ end
+
+ def normalize_value(value)
+ case value
+ when TrueClass
+ nil
+ when String
+ value.match(/\s/) ? "\"#{value}\"" : value
+ else
+ value
+ end
+ end
+
+end

0 comments on commit 6847011

Please sign in to comment.