Skip to content

Instantly share code, notes, and snippets.

@zgohr
Created August 20, 2012 01:44
Show Gist options
  • Save zgohr/3399170 to your computer and use it in GitHub Desktop.
Save zgohr/3399170 to your computer and use it in GitHub Desktop.
SVG vector data to PNG
brew upgrade
brew update
brew install cairo
brew install py2cairo
ln -s ~/Developer/lib/python2.7/site-packages/cairo ~/.virtualenvs/wpd/lib/python2.7/site-packages/cairo
# I needed to specify that my virtual environment uses python 2.7.3 because by default it used 2.7.2
# but brew compiled py2cairo with 2.7.3
mkvirtualenv --no-site-packages -p ~/Developer/bin/python wpd
pip install cairosvg
> from cairosvg import svg2png
> import base64
> binary = svg2png(<svg data>)
> b64 = base64.b64encode(binary)
> print "data:image/png;base64,%s" % b64
# The next steps are to pass this data to an ```<img>``` in an HTML template and render via [xhtml2pdf](https://github.com/chrisglass/xhtml2pdf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment