Created
June 4, 2011 17:49
-
-
Save ngty/1008129 to your computer and use it in GitHub Desktop.
Showoff's (https://github.com/schacon/showoff) builtin pdf conversion doesn't work well for me, so i hacked up my own version ... & it works !!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'RMagick' | |
require 'capybara' | |
require 'capybara/dsl' | |
# ================================================================ | |
# Collect input args | |
# ================================================================ | |
begin | |
URL = ARGV[0] | |
PAGES = ARGV[1].split('~').map(&:to_i) | |
DIMENSION = ARGV[2].split('x').map(&:to_i) | |
OUTPUT = ARGV[3] | |
rescue | |
puts [ | |
"= USAGE:", | |
"", | |
" $ #{$0} url pages dimension output", | |
"", | |
"= WHERE:", | |
"* url ... url to access the showoff preso (eg. http://0.0.0.0:9090)", | |
"* pages ... range of pages to cover (eg. 1~3)", | |
"* dimension ... the width x height of the slide area (eg. 1024x768)", | |
"* output ... the path for the generated pdf (eg. /tmp/final.pdf)", | |
"", | |
"= EXAMPLE:", | |
"", | |
" $ #{$0} http://localhost:9090 1~3 1024x768 /tmp/final.pdf", | |
"", | |
].join("\n") | |
exit | |
end | |
# ================================================================ | |
# Screen shooter | |
# ================================================================ | |
class Shooter < Struct.new(:dimension, :shoot) | |
include Magick | |
DIR = '/tmp' | |
def initialize(dimension, &shoot) | |
super(dimension, shoot) | |
end | |
def take(i) | |
pdf = pdf_path(i) | |
(@pdfs ||= []) << pdf | |
Image.new(*dimension).composite(area(i), 0, 0, OverCompositeOp).write(pdf) | |
end | |
def dump(output) | |
ImageList.new(*@pdfs).write(OUTPUT) | |
end | |
private | |
def pdf_path(i) | |
png_path(i).sub(/png$/, 'pdf') | |
end | |
def png_path(i) | |
DIR + '/' + "#{i}".rjust(4,'0') + '.png' | |
end | |
def area(i) | |
png = png_path(i) | |
shoot.call(png) | |
Image.read(png).first.crop(NorthGravity, *dimension) | |
end | |
end | |
# ================================================================ | |
# Loop through the pages & take screenshots | |
# ================================================================ | |
Capybara.using_driver(:selenium) do |c| | |
include Capybara | |
screen = Shooter.new(DIMENSION) do |path| | |
page.driver.browser.save_screenshot(path) | |
end | |
PAGES[0].upto(PAGES[-1]).each do |i| | |
visit [URL, "\##{i}"].join('/') | |
sleep 0.5 | |
screen.take(i) | |
end | |
screen.dump(OUTPUT) | |
end | |
# __END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
can you tell me how to use your script ? when I run
showoff pdf
, it generate a 2k empty pdf file