Skip to content

Instantly share code, notes, and snippets.

@dcrosby42
Created September 19, 2012 03:18
Show Gist options
  • Save dcrosby42/3747456 to your computer and use it in GitHub Desktop.
Save dcrosby42/3747456 to your computer and use it in GitHub Desktop.
Rake tasks to use Inkscape to convert SVGs into PDFs.
cd /Applications/Inkscape.app/Contents/Resources/bin && ./inkscape -z -D --file=your_drawing.svg --export-pdf=your_drawing.pdf
#
# This Rakefile provides convenience tasks for converting SVG documents to PDF on OS X using Inkscape.
#
# ASSUMES Inkscape IS INSTALLED IN /Applications (see below to modify this assumption).
#
# Examples:
# $ rake convert file=my_drawing.svg # => my_drawing.pdf
# $ rake all # => *.svg now accompanied by *.pdf
#
# David Crosby
# [email protected]
# Sep 2012
#
task :default => :convert
desc "Export a PDF version for every SVG file in the current directory."
task :all do
batch_convert_svg_to_pdf Dir["*.svg"]
end
desc "Given an SVG file, export a PDF verison. Indicate the SVG like this: file=myfile.svg"
task :convert do
file = ENV["file"] || ENV["svg"]
batch_convert_svg_to_pdf [file]
end
def batch_convert_svg_to_pdf(svg_names)
puts "Converting #{svg_names.length} SVG files to PDF"
puts "-"*svg_names.length
svg_names.each do |svg_name|
base = File.basename(svg_name,".svg")
pdf_name = "#{base}.pdf"
svg_to_pdf svg_name, pdf_name
print "."
end
puts "\n"
end
def svg_to_pdf(svg_name, pdf_name)
svg_name = File.expand_path(svg_name)
pdf_name = File.expand_path(pdf_name)
# puts "Convert #{svg_name} to #{pdf_name}"
dir = '/Applications/Inkscape.app/Contents/Resources/bin'
`cd #{dir} && ./inkscape -z -D --file=#{svg_name} --export-pdf=#{pdf_name} > /dev/null 2>&1 && cd .`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment