Created
January 16, 2013 09:45
-
-
Save shlomizadok/4545954 to your computer and use it in GitHub Desktop.
Extract svg with Docsplit. ** You need inkscape (http://inkscape.org/) installed on your machine I use it like that:
::Docsplit.extract_svg(current_path, :format => :svg, output: output_path)
This file contains 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
module Docsplit | |
def self.extract_svg(pdfs, opts={}) | |
pdfs = ensure_pdfs(pdfs) | |
SvgExtractor.new.extract(pdfs, opts) | |
end | |
class SvgExtractor | |
DEFAULT_FORMAT = :svg | |
DEFAULT_DENSITY = '150' | |
def extract(pdfs, options) | |
@pdfs = [pdfs].flatten | |
extract_options(options) | |
@pdfs.each do |pdf| | |
previous = nil | |
@sizes.each_with_index do |size, i| | |
@formats.each {|format| convert(pdf, size, format, previous) } | |
previous = size if @rolling | |
end | |
end | |
end | |
def convert(pdf, size, format, previous=nil) | |
tempdir = Dir.mktmpdir | |
basename = File.basename(pdf, File.extname(pdf)) | |
directory = directory_for(size) | |
pages = @pages || '1-' + Docsplit.extract_length(pdf).to_s | |
escaped_pdf = ESCAPE[pdf] | |
FileUtils.mkdir_p(directory) #unless File.exists?(directory) | |
page_list(pages).each do |page| | |
out_file = ESCAPE[File.join(directory, "#{basename}_#{page}.#{format}")] | |
pre_cmd = "pdftk #{escaped_pdf} cat #{page} output #{escaped_pdf}[#{page}].pdf".chomp | |
cmd = "#{@inkscape} --without-gui --export-dpi=#{@density} --export-plain-svg=#{out_file} #{escaped_pdf}[#{page}].pdf".chomp | |
pre_res = `#{pre_cmd}` | |
result = `#{cmd}`.chomp | |
raise ExtractionFailed, pre_res if $? != 0 | |
raise ExtractionFailed, result if $? != 0 | |
end | |
ensure | |
FileUtils.remove_entry_secure tempdir if File.exists?(tempdir) | |
end | |
private | |
def extract_options(options) | |
@output = options[:output] || '.' | |
@pages = options[:pages] | |
@density = options[:density] || DEFAULT_DENSITY | |
@formats = [options[:format] || DEFAULT_FORMAT].flatten | |
@sizes = [options[:size]].flatten.compact | |
@sizes = [nil] if @sizes.empty? | |
@rolling = !!options[:rolling] | |
@inkscape = case RUBY_PLATFORM | |
when /darwin/ | |
"/Applications/Inkscape.app/Contents/Resources/bin/inkscape" | |
when /linux/ | |
# on Ubuntu | |
"/usr/bin/inkscape" | |
end | |
end | |
def directory_for(size) | |
path = @sizes.length == 1 ? @output : File.join(@output, size) | |
File.expand_path(path) | |
end | |
def page_list(pages) | |
pages.split(',').map { |range| | |
if range.include?('-') | |
range = range.split('-') | |
Range.new(range.first.to_i, range.last.to_i).to_a.map {|n| n.to_i } | |
else | |
range.to_i | |
end | |
}.flatten.uniq.sort | |
end | |
def resize_arg(size) | |
size.nil? ? '' : "-resize #{size}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment