Created
November 8, 2013 16:21
-
-
Save nathancolgate/7373557 to your computer and use it in GitHub Desktop.
Converting SVG content from Highcharts into PNGs to be embedded into a PDF. Eventually I settled on using prawn-svg (thanks, Matt!). But I thought this solution was interesting enough to retain.
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
@pngs = {} | |
timestamp = Time.now.to_i | |
Dir.mkdir("#{Rails.root}/tmp/rmagick") unless File.exists?("#{Rails.root}/tmp/rmagick") | |
Dir.mkdir("#{Rails.root}/tmp/rmagick/reports") unless File.exists?("#{Rails.root}/tmp/rmagick/reports") | |
Dir.mkdir("#{Rails.root}/tmp/rmagick/reports/#{timestamp}") unless File.exists?("#{Rails.root}/tmp/rmagick/reports/#{timestamp}") | |
params[:svgs].each do |key,svg| | |
string = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'+URI.unescape(svg) | |
string.gsub!("'lucida grande', 'lucida sans unicode', verdana, arial, helvetica, sans-serif",'') # classes_observed_by_grade_level | |
string.gsub!(/clip-path="url\(#highcharts-[0-9]*\)"/,'clip-path="none"') # classes_observed_by_grade_level | |
string.gsub!(' opacity="1"','') # classes_observed_by_grade_level | |
string.gsub!('translate(0,0) rotate(270 24.03125 156)','translate(0,0) rotate(270 -130 156)') # classes_observed_by_grade_level | |
string.gsub!('scale(-1,1) scale(1 1)', 'scale(1,-1)') # content_and_materials, training_attendance | |
img = Magick::Image::from_blob(string) { self.format = 'SVG'; }.first | |
img.format = 'JPG' | |
path = "#{Rails.root}/tmp/rmagick/reports/#{timestamp}/#{key}.jpg" | |
img.write(path) | |
@pngs[key] = path # , :type => 'image/png',:disposition => 'inline' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment