Skip to content

Instantly share code, notes, and snippets.

@redfield
Created January 3, 2014 10:46
Show Gist options
  • Save redfield/8236061 to your computer and use it in GitHub Desktop.
Save redfield/8236061 to your computer and use it in GitHub Desktop.
Converting SVG to PDF using HyPDF
require 'hypdf'
require 'open-uri'
require 'base64'
# SVG file from wikipedia article
svg = open('http://upload.wikimedia.org/wikipedia/commons/1/12/%D0%9F%D1%80%D0%B8%D0%BC%D0%B5%D1%80_%D1%87%D0%B5%D1%80%D1%82%D0%B5%D0%B6%D0%B0_%D0%B2_SVG_%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%82%D0%B5.svg').read
# Embedding SVG to HTML via <img> tag
html = "<html><img src='data:image/svg+xml;base64,#{Base64.encode64(svg)}'></html>"
# Converting to PDF
hypdf = HyPDF.htmltopdf(html, test: true, orientation: 'Landscape')
# Writing PDF to file
File.open('file_name.pdf', 'wb').write(hypdf[:pdf])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment